summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tinyff/thread/thread.h5
-rw-r--r--src/thread/thread_submit.c27
2 files changed, 32 insertions, 0 deletions
diff --git a/include/tinyff/thread/thread.h b/include/tinyff/thread/thread.h
index 1d0a644..252f044 100644
--- a/include/tinyff/thread/thread.h
+++ b/include/tinyff/thread/thread.h
@@ -4,6 +4,7 @@
#define TINYFF_THREAD_H
#include <tinyff/common.h>
+#include <tinyff/result.h>
typedef void (*ff_job_cb)(void *arg);
@@ -15,5 +16,9 @@ typedef struct {
void *ff_thread_wrapper(void *ptr);
+// High level helper: allocate a job using ctx->allocator and schedule it.
+// Returns FF_RESULT_OK on success, or an error ff_result on failure.
+ff_result ff_thread_submit(ff_ctx *ctx, ff_job_cb callback, void *arg);
+
#endif
#endif \ No newline at end of file
diff --git a/src/thread/thread_submit.c b/src/thread/thread_submit.c
new file mode 100644
index 0000000..fd332b6
--- /dev/null
+++ b/src/thread/thread_submit.c
@@ -0,0 +1,27 @@
+#ifdef USE_THREAD
+
+#include <tinyff/thread/thread.h>
+#include <bridges/pthread/thread_bridge.h>
+#include <tinyff/result.h>
+
+ff_result ff_thread_submit(ff_ctx *ctx, ff_job_cb callback, void *arg) {
+ if (!ctx || !callback) return FF_RESULT_ERROR_NULL_PTR;
+
+ ff_thread_job *job = ctx->allocator.ff_alloc(sizeof(ff_thread_job));
+ if (!job) return FF_RESULT_ERROR_MEMORY_ALLOCATION;
+
+ job->callback = callback;
+ job->arg = arg;
+ job->ctx = ctx;
+
+ pthread_t t = run_job_in_thread(job);
+ if ((pthread_t)0 == t) {
+ // failed to create thread
+ ctx->allocator.ff_free(job);
+ return FF_RESULT_WARN_NO_IMPL; // thread creation failed
+ }
+
+ return FF_RESULT_OK;
+}
+
+#endif \ No newline at end of file