Skip to content

cgen, coroutines: fix go expr handle referencing undeclared thread__tN#27379

Open
medvednikov wants to merge 1 commit into
masterfrom
fix-27338-coroutine-go-handle
Open

cgen, coroutines: fix go expr handle referencing undeclared thread__tN#27379
medvednikov wants to merge 1 commit into
masterfrom
fix-27338-coroutine-go-handle

Conversation

@medvednikov

Copy link
Copy Markdown
Member

Description

Fixes #27338.

With -use-coroutines, a go expr whose handle is stored/used (e.g. a.worker = go a.loop()) made cgen reference a C variable thread__tN that it never declared:

error: use of undeclared identifier 'thread__t2'
 a.worker = /*go (coroutine) */ thread__t2;

The coroutine branch in spawn_and_go_expr only emitted the photon_thread_create* call (which returns void and yields no joinable handle), yet the handle thread_<tmp> was still written back as the expression's value.

Fix

The Photon wrapper exposes no waitable handle, so a go whose handle is actually used (node.is_expr) is now lowered to the regular spawn (pthread) path, which declares and assigns thread_<tmp>. The generated C now contains:

pthread_t thread__t2;
...
int _t2_thr_res = pthread_create(&thread__t2, ..., main__App_loop_thread_wrapper, arg__t2);
a.worker = thread__t2;

so it compiles and the handle is joinable via .wait(), matching the non-coroutine spawn path. Plain statement-form go f() (no handle used) is unchanged and keeps using the Photon work-pool path.

A companion change in markused is required: a go handle that is used must mark the same thread-handle type and pthread error helpers (panic_error_number, c_error_number_str, …) that spawn needs — otherwise -skip-unused (the default) prunes them and the rerouted spawn path fails to link with undefined symbol 'builtin__panic_error_number'.

Verification

  • The reproducer now generates a properly-declared pthread_t handle (was: undeclared identifier).
  • panic_error_number / c_error_number_str are emitted (no longer pruned).
  • The compiler self-compiles cleanly, and existing spawn/thread tests pass (array_of_threads_wait_test.v, spawn_with_cond_fncall_test.v, generic_spawn_test.v).
  • Added examples/coroutines/go_handle.v documenting the now-working pattern.

Note: an automated CI test isn't included because -use-coroutines requires the Photon .so (downloaded at build time, not available on CI) — consistent with the existing coroutines examples being marked vtest build: false.

…__tN`

With `-use-coroutines`, a `go expr` whose handle is stored/used (e.g.
`a.worker = go a.loop()`) made cgen reference a C variable `thread__tN`
that it never declared, so the generated C failed to compile:

    error: use of undeclared identifier 'thread__t2'

The coroutine branch in `spawn_and_go_expr` only emitted the
`photon_thread_create*` call, which returns void and yields no joinable
handle, yet the handle `thread_<tmp>` was still written back as the
expression's value. The Photon wrapper exposes no waitable handle, so a
`go` whose handle is actually used is now lowered to the regular `spawn`
(pthread) path, which declares and assigns `thread_<tmp>`. Plain
statement-form `go f()` keeps using the Photon work-pool path.

markused: a `go` handle that is used must mark the same thread-handle
type and pthread error helpers (`panic_error_number`,
`c_error_number_str`, ...) that `spawn` needs, otherwise `-skip-unused`
prunes them and the rerouted spawn path fails to link.

Fixes #27338
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cgen, coroutines: go expr handle references undeclared thread__tN C variable

1 participant