Skip to content

feat(examples): the timer example for the poll model - #279

Draft
Ivansete-status wants to merge 2 commits into
feat/poll-cpp-reversefrom
feat/poll-examples
Draft

Ivansete-status wants to merge 2 commits into
feat/poll-cpp-reversefrom
feat/poll-examples

Conversation

@Ivansete-status

@Ivansete-status Ivansete-status commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #278 (base is feat/poll-cpp-reverse; retarget to master as the stack merges). Last of the four PRs that complete the first poll-mode version: #271 (C++ host), #276 (typed client), #278 (typed reverse-call handlers), this one. Part of #266.

The timer example, for the poll model, so the two can be compared side by side:

  • examples/timer/cpp_poll_bindings/main.cpp is cpp_bindings/main.cpp step for step ([1]–[7], same checks), built from the same timer.nim with -d:ffiPollMode. Comments marked "poll:" explain what differs: no ...Async variants (a call serves the queue while it waits), events arrive on the thread that serves, errors name the call. Both programs make a reverse call in [8] (the callback one answers with setFetchHostClockImpl, the poll one through a typed handler and a Responder); the poll one adds [9], an event served while a runLoop() thread runs.
  • Bindings are regenerated by nimble genbindings_cpp_poll, not checked in. nimble run_examples_poll builds and runs it, and nimble run_examples (CI's "Run C and C++ examples") includes it. examples/timer/README.md has a short section on it.

What it needed:

  • The generated CMakeLists.txt of poll-model bindings builds the library with -d:ffiPollMode, in its own build directory: the callback build of the same library has the same file name (libmy_timer), so a shared output would be overwritten and later callback runs could link the wrong one.
  • nim_ffi_lib.cmake gives executables an rpath to the dylib. On macOS CMake adds none for a dylib that does not exist at configure time, which a first build in a clean tree hit; the existing examples only worked because something had built the dylib earlier.

This is the first run of the generated poll client against a real library, and it found two bugs in #278 (a callback-mode compile error, a helper returning an empty reverse registry) and one gap (a doc-only {.ffiReverse.} body), all fixed there.

Measured on macOS (a pthread_create interposer): the callback program creates 8 threads (peak 5 alive plus main), the poll one 2 (peak 2 plus main).

Verified locally on macOS from a clean tree: nimble run_examples (C, C++ callback, C++ poll) exits 0 and the checked-in callback bindings regenerate unchanged. Linux and Windows runs are CI's first.

🤖 Generated with Claude Code

…un_examples

The poll mode had no example, and nothing had run its generated C++ client
against a real library. cpp_poll_bindings/main.cpp is the timer example's
C++ program step for step, built from the same timer.nim with
-d:ffiPollMode, so the two models can be compared side by side; it adds a
reverse call answered through a Responder and a runLoop() thread. The
generated CMakeLists of poll-model bindings builds the library in poll
mode and in its own build directory, since the callback model's build of
the same library has the same file name and would be overwritten. A
dylib built by nim_ffi_lib.cmake also gets an rpath for the executables
linked against it, since CMake adds none on macOS for a dylib that does not
exist yet at configure time, which a first build in a clean tree hits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Ivansete-status Ivansete-status changed the title feat(examples): a poll-mode example, run by nimble run_examples feat(examples): the timer example for the poll model Nov 21, 2026
Comment thread ffi.nimble
runOrQuit genBindingsCmd(nimFlagsOrc, timerSrc, "cpp")
runOrQuit genBindingsCmd(nimFlagsRefc, timerSrc, "cpp")

task genbindings_cpp_poll, "Generate the timer example's C++ bindings for the poll model":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nph] reported by reviewdog 🐶

Suggested change
task genbindings_cpp_poll, "Generate the timer example's C++ bindings for the poll model":
task genbindings_cpp_poll,
"Generate the timer example's C++ bindings for the poll model":

Comment thread ffi/codegen/cpp.nim
Comment on lines +1022 to +1026
let pollBuild =
when defined(ffiPollMode):
PollBuildCMake
else:
""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nph] reported by reviewdog 🐶

Suggested change
let pollBuild =
when defined(ffiPollMode):
PollBuildCMake
else:
""
let pollBuild = when defined(ffiPollMode): PollBuildCMake else: ""

The poll example had a reverse-call step the callback example lacked, so
the two could not be compared step for step. Add it: setFetchHostClockImpl
answers the library's question with call.reply from the worker thread the
library calls it on, and the program expects the same "UTC@1234567" as the
poll example.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

1 participant