Lely core libraries  2.2.5
timer.c
Go to the documentation of this file.
1 
24 #include "io2.h"
25 #define LELY_IO_TIMER_INLINE extern inline
26 #include <lely/io2/timer.h>
27 #include <lely/util/util.h>
28 
29 #include <assert.h>
30 
32  ev_promise_t *promise;
33  struct io_timer_wait wait;
34 };
35 
36 static void io_timer_async_wait_func(struct ev_task *task);
37 
40  struct io_timer_wait **pwait)
41 {
43  sizeof(struct io_timer_async_wait), NULL);
44  if (!promise)
45  return NULL;
46 
47  struct io_timer_async_wait *async_wait = ev_promise_data(promise);
48  async_wait->promise = promise;
49  async_wait->wait = (struct io_timer_wait)IO_TIMER_WAIT_INIT(
50  exec, &io_timer_async_wait_func);
51 
52  io_timer_submit_wait(timer, &async_wait->wait);
53 
54  if (pwait)
55  *pwait = &async_wait->wait;
56 
57  return ev_promise_get_future(promise);
58 }
59 
60 struct io_timer_wait *
62 {
63  return task ? structof(task, struct io_timer_wait, task) : NULL;
64 }
65 
66 static void
67 io_timer_async_wait_func(struct ev_task *task)
68 {
69  assert(task);
70  struct io_timer_wait *wait = io_timer_wait_from_task(task);
71  struct io_timer_async_wait *async_wait =
72  structof(wait, struct io_timer_async_wait, wait);
73 
74  ev_promise_set(async_wait->promise, &wait->r);
75  ev_promise_release(async_wait->promise);
76 }
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
void * ev_promise_data(const ev_promise_t *promise)
Returns a pointer to the shared state of a promise.
Definition: future.c:233
void ev_promise_release(ev_promise_t *promise)
Releases a reference to a promise.
Definition: future.c:195
struct io_timer_wait_result r
The result of the wait operation.
Definition: timer.h:61
ev_promise_t * ev_promise_create(size_t size, ev_promise_dtor_t *dtor)
Constructs a new promise with an optional empty shared state.
Definition: future.c:151
This header file is part of the I/O library; it contains the abstract timer interface.
A future.
Definition: future.c:63
struct io_timer_wait * io_timer_wait_from_task(struct ev_task *task)
Obtains a pointer to an I/O timer wait operation from a pointer to its completion task...
Definition: timer.c:61
ev_future_t * ev_promise_get_future(ev_promise_t *promise)
Returns (a reference to) a future associated with the specified promise.
Definition: future.c:309
void io_timer_submit_wait(io_timer_t *timer, struct io_timer_wait *wait)
Submits a wait operation to an I/O timer.
Definition: timer.h:263
ev_future_t * io_timer_async_wait(io_timer_t *timer, ev_exec_t *exec, struct io_timer_wait **pwait)
Submits an asynchronous wait operation to an I/O timer and creates a future which becomes ready once ...
Definition: timer.c:39
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the wait operation.
Definition: timer.h:59
An executable task.
Definition: task.h:41
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
Definition: util.h:93
int ev_promise_set(ev_promise_t *promise, void *value)
Satiesfies a promise, if it was not aready satisfied, and stores the specified value for retrieval by...
Definition: future.c:239
A wait operation suitable for use with an I/O timer.
Definition: timer.h:54
const struct io_timer_vtbl *const io_timer_t
An abstract timer.
Definition: timer.h:38
A promise.
Definition: future.c:92
This is the internal header file of the I/O library.
This is the public header file of the utilities library.