From 55a4f1fc869e41aca748c63d3018f0448b1606e0 Mon Sep 17 00:00:00 2001 From: pack Date: Sun, 9 Aug 2026 10:37:07 +0000 Subject: first commit --- node_modules/sqlite3/src/async.h | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 node_modules/sqlite3/src/async.h (limited to 'node_modules/sqlite3/src/async.h') diff --git a/node_modules/sqlite3/src/async.h b/node_modules/sqlite3/src/async.h new file mode 100644 index 0000000..f36e4cb --- /dev/null +++ b/node_modules/sqlite3/src/async.h @@ -0,0 +1,76 @@ +#ifndef NODE_SQLITE3_SRC_ASYNC_H +#define NODE_SQLITE3_SRC_ASYNC_H + +#include +#include + +#include "threading.h" + +// Generic uv_async handler. +template class Async { + typedef void (*Callback)(Parent* parent, Item* item); + +protected: + uv_async_t watcher; + NODE_SQLITE3_MUTEX_t + std::vector data; + Callback callback; +public: + Parent* parent; + +public: + Async(Parent* parent_, Callback cb_) + : callback(cb_), parent(parent_) { + watcher.data = this; + NODE_SQLITE3_MUTEX_INIT + uv_loop_t *loop; + napi_get_uv_event_loop(parent_->Env(), &loop); + uv_async_init(loop, &watcher, reinterpret_cast(listener)); + } + + static void listener(uv_async_t* handle) { + auto* async = static_cast(handle->data); + std::vector rows; + NODE_SQLITE3_MUTEX_LOCK(&async->mutex) + rows.swap(async->data); + NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex) + for(auto row : rows) + async->callback(async->parent, row); + } + + static void close(uv_handle_t* handle) { + assert(handle != NULL); + assert(handle->data != NULL); + auto* async = static_cast(handle->data); + delete async; + } + + void finish() { + // Need to call the listener again to ensure all items have been + // processed. Is this a bug in uv_async? Feels like uv_close + // should handle that. + listener(&watcher); + uv_close((uv_handle_t*)&watcher, close); + } + + void add(Item* item) { + NODE_SQLITE3_MUTEX_LOCK(&mutex); + data.emplace_back(item); + NODE_SQLITE3_MUTEX_UNLOCK(&mutex) + } + + void send() { + uv_async_send(&watcher); + } + + void send(Item* item) { + add(item); + send(); + } + + ~Async() { + NODE_SQLITE3_MUTEX_DESTROY + } +}; + +#endif -- cgit v1.2.3