11#ifndef PythonMonkey_PyEventLoop_
12#define PythonMonkey_PyEventLoop_
41 if (Py_IsInitialized()) {
75 _timeoutIdMap.push_back(std::move(handle));
76 return _timeoutIdMap.size() - 1;
80 return &_timeoutIdMap.at(timeoutID);
104 inline PyObject *
swap(PyObject *newHandleObject) {
105 return std::exchange(
_handle, newHandleObject);
151 return _timeoutIdMap;
267 PyObject *asyncio = PyImport_ImportModule(
"asyncio");
283 Py_XDECREF(PyObject_CallMethod(
_queueIsEmpty,
"clear", NULL));
295 PyErr_SetString(PyExc_RuntimeError,
"Event-loop job counter went below zero.");
325 static PyEventLoop _getLoopOnThread(PyThreadState *tstate);
327 static PyThreadState *_getMainThread();
328 static inline PyThreadState *_getCurrentThread();
331 static inline std::vector<AsyncHandle> _timeoutIdMap;
C++ wrapper for Python asyncio.Handle class.
Definition PyEventLoop.hh:34
uint32_t id_t
Definition PyEventLoop.hh:35
bool hasRef()
Getter for if the timer has been ref'ed.
Definition PyEventLoop.hh:111
AsyncHandle(AsyncHandle &&old)
Definition PyEventLoop.hh:39
static id_t newEmpty()
Create a new AsyncHandle without an associated asyncio.Handle Python object.
Definition PyEventLoop.hh:50
void addRef()
Ref the timer so that the event-loop won't exit as long as the timer is active.
Definition PyEventLoop.hh:118
PyObject * _handle
Definition PyEventLoop.hh:154
~AsyncHandle()
Definition PyEventLoop.hh:40
std::atomic_bool _refed
Definition PyEventLoop.hh:155
PyObject * _debugInfo
Definition PyEventLoop.hh:156
bool _finishedOrCancelled()
Definition PyEventLoop.cc:234
AsyncHandle(const AsyncHandle &old)=delete
void setDebugInfo(PyObject *obj)
Set the debug info object for WTFPythonMonkey tool.
Definition PyEventLoop.hh:140
PyObject * getDebugInfo()
Definition PyEventLoop.hh:143
void cancel()
Cancel the scheduled event-loop job. If the job has already been canceled or executed,...
Definition PyEventLoop.cc:208
static AsyncHandle * fromId(id_t timeoutID)
Definition PyEventLoop.hh:78
AsyncHandle(PyObject *handle)
Definition PyEventLoop.hh:37
PyObject * getHandleObject() const
Get the underlying asyncio.Handle Python object.
Definition PyEventLoop.hh:95
static bool cancelAll()
Cancel all pending event-loop jobs.
Definition PyEventLoop.cc:219
void removeRef()
Unref the timer so that the event-loop can exit.
Definition PyEventLoop.hh:130
static auto & getAllTimers()
Get an iterator for the AsyncHandles of all timers.
Definition PyEventLoop.hh:150
bool cancelled()
Definition PyEventLoop.cc:226
PyObject * swap(PyObject *newHandleObject)
Replace the underlying asyncio.Handle Python object with the provided value.
Definition PyEventLoop.hh:104
static id_t getUniqueId(AsyncHandle &&handle)
Get the unique timeoutID for JS setTimeout/clearTimeout methods.
Definition PyEventLoop.hh:73
C++ wrapper for Python asyncio.Future class.
Definition PyEventLoop.hh:178
PyObject * getFutureObject() const
Get the underlying asyncio.Future Python object.
Definition PyEventLoop.hh:228
PyObject * getException()
Get the exception object that was set on this Future, or Py_None if no exception was set....
Definition PyEventLoop.cc:273
Future(PyObject *future)
Definition PyEventLoop.hh:180
void addDoneCallback(PyObject *cb)
Add a callback to be run when the Future is done.
Definition PyEventLoop.cc:254
void setException(PyObject *exception)
Mark the Future as done and set an exception.
Definition PyEventLoop.cc:248
~Future()
Definition PyEventLoop.hh:183
PyObject * _future
Definition PyEventLoop.hh:233
void setResult(PyObject *result)
Mark the Future as done and set its result.
Definition PyEventLoop.cc:242
Future(const Future &old)=delete
bool isCancelled()
Return True if the Future is cancelled.
Definition PyEventLoop.cc:260
Future(Future &&old)
Definition PyEventLoop.hh:182
PyObject * getResult()
Get the result of the Future. Would raise exception if the Future is pending, cancelled,...
Definition PyEventLoop.cc:268
Definition PyEventLoop.hh:264
PyObject * _queueIsEmpty
An asyncio.Event instance to notify that there are no queued asynchronous jobs.
Definition PyEventLoop.hh:303
~Lock()
Definition PyEventLoop.hh:274
void incCounter()
Increment the counter for the number of our job functions in the Python event-loop.
Definition PyEventLoop.hh:281
std::atomic_int _counter
Definition PyEventLoop.hh:305
Lock()
Definition PyEventLoop.hh:266
void decCounter()
Decrement the counter for the number of our job functions in the Python event-loop.
Definition PyEventLoop.hh:289
Definition PyEventLoop.hh:20
PyObject * _loop
Definition PyEventLoop.hh:310
bool initialized() const
Definition PyEventLoop.hh:26
Future createFuture()
Create a Python asyncio.Future object attached to this Python event-loop.
Definition PyEventLoop.cc:105
PyEventLoop(PyObject *loop)
Definition PyEventLoop.hh:313
AsyncHandle::id_t enqueueWithDelay(PyObject *jobFn, double delaySeconds, bool repeat)
Schedule a job to the Python event-loop, with the given delay.
Definition PyEventLoop.cc:95
static PyEventLoop getMainLoop()
Get the running Python event-loop on main thread, or raise a Python RuntimeError if no event-loop run...
Definition PyEventLoop.cc:199
AsyncHandle enqueue(PyObject *jobFn)
Send job to the Python event-loop.
Definition PyEventLoop.cc:71
static PyEventLoop getRunningLoop()
Get the running Python event-loop on the current thread, or raise a Python RuntimeError if no event-l...
Definition PyEventLoop.cc:204
Future ensureFuture(PyObject *awaitable)
Convert a Python awaitable to asyncio.Future attached to this Python event-loop.
Definition PyEventLoop.cc:111
~PyEventLoop()
Definition PyEventLoop.hh:22
static PyEventLoop::Lock * _locker
Definition PyEventLoop.hh:308