Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
run_scope.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
18
19#include <string>
20#include <utility>
21
22namespace entropic {
23
24namespace {
25
27thread_local std::atomic<bool>* t_run_cancel = nullptr;
28
29} // namespace
30
37const std::atomic<bool>* current_run_cancel() { return t_run_cancel; }
38
46 return t_run_cancel != nullptr
47 && t_run_cancel->load(std::memory_order_acquire);
48}
49
57RunCancelScope::RunCancelScope(std::atomic<bool>* token)
58 : previous_(t_run_cancel) {
59 t_run_cancel = token;
60}
61
69 if (t_run_cancel == nullptr) {
70 return false;
71 }
72 t_run_cancel->store(true, std::memory_order_release);
73 return true;
74}
75
83
85namespace {
86thread_local std::string t_run_session;
87} // namespace
88
95std::string current_run_session() { return t_run_session; }
96
105 : previous_(t_run_session) {
106 t_run_session = std::move(key);
107}
108
116 t_run_session = std::move(previous_);
117}
118
119namespace {
120
122thread_local RunStreamFn t_run_stream_fn = nullptr;
124thread_local void* t_run_stream_ud = nullptr;
125
126} // namespace
127
137bool emit_to_current_run_stream(const char* chunk, size_t len) {
138 const bool installed = t_run_stream_fn != nullptr;
140 return installed;
141}
142
151 : previous_fn_(t_run_stream_fn), previous_ud_(t_run_stream_ud) {
153 t_run_stream_ud = user_data;
154}
155
162 t_run_stream_fn = previous_fn_;
163 t_run_stream_ud = previous_ud_;
164}
165
166} // namespace entropic
RunCancelScope(std::atomic< bool > *token)
Publish token as this thread's run cancel token.
Definition run_scope.cpp:57
~RunCancelScope()
Restore the previously installed token.
Definition run_scope.cpp:82
~RunSessionScope()
Restore the previously published key.
RunSessionScope(std::string key)
Publish key as this thread's run session key.
~RunStreamScope()
Restore the previously published sink.
RunStreamScope(RunStreamFn fn, void *user_data)
Publish (fn, user_data) as this thread's stream sink.
Activate model on GPU (WARM → ACTIVE).
ENTROPIC_EXPORT const std::atomic< bool > * current_run_cancel()
The cancel token of the run executing on this thread, if any.
Definition run_scope.cpp:37
@ optional
optional: every escape asks the host's path approver
ENTROPIC_EXPORT bool emit_to_current_run_stream(const char *chunk, size_t len)
Deliver a chunk to the stream sink published on THIS thread.
ENTROPIC_EXPORT bool current_run_cancelled()
Whether THIS thread's run has been cancelled.
Definition run_scope.cpp:45
ENTROPIC_EXPORT std::string current_run_session()
The session key of the run executing on this thread (gh#166).
Definition run_scope.cpp:95
ENTROPIC_EXPORT bool cancel_current_run()
Cancel the run executing on this thread (gh#158).
Definition run_scope.cpp:68
void(*)(const char *chunk, size_t len, void *user_data) RunStreamFn
Signature of a run's stream-chunk sink (gh#208, v2.14.2).
Definition run_scope.h:185
gh#158: the cancel token of the run executing on THIS thread.