Lely core libraries  2.3.4
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  ev_future_t *future = ev_promise_get_future(promise);
47 
48  struct io_timer_async_wait *async_wait = ev_promise_data(promise);
49  async_wait->promise = promise;
50  async_wait->wait = (struct io_timer_wait)IO_TIMER_WAIT_INIT(
51  exec, &io_timer_async_wait_func);
52 
53  io_timer_submit_wait(timer, &async_wait->wait);
54 
55  if (pwait)
56  *pwait = &async_wait->wait;
57 
58  return future;
59 }
60 
61 struct io_timer_wait *
63 {
64  return task ? structof(task, struct io_timer_wait, task) : NULL;
65 }
66 
67 static void
68 io_timer_async_wait_func(struct ev_task *task)
69 {
70  assert(task);
72  struct io_timer_async_wait *async_wait =
73  structof(wait, struct io_timer_async_wait, wait);
74 
75  ev_promise_set(async_wait->promise, &wait->r);
76  ev_promise_release(async_wait->promise);
77 }
ev_future
A future.
Definition: future.c:66
io2.h
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
timer.h
ev_promise_data
void * ev_promise_data(const ev_promise_t *promise)
Returns a pointer to the shared state of a promise.
Definition: future.c:236
util.h
ev_promise_create
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:154
ev_promise
A promise.
Definition: future.c:95
io_timer_wait::r
struct io_timer_wait_result r
The result of the wait operation.
Definition: timer.h:61
io_timer_wait
A wait operation suitable for use with an I/O timer.
Definition: timer.h:54
ev_promise_set
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:242
IO_TIMER_WAIT_INIT
#define IO_TIMER_WAIT_INIT(exec, func)
The static initializer for io_timer_wait.
Definition: timer.h:65
io_timer_submit_wait
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
io_timer_async_wait
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
io_timer_wait::task
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the wait operation.
Definition: timer.h:59
io_timer_wait_from_task
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:62
structof
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
Definition: util.h:93
ev_task
An executable task.
Definition: task.h:41
ev_promise_release
void ev_promise_release(ev_promise_t *promise)
Releases a reference to a promise.
Definition: future.c:198
io_timer_async_wait
Definition: timer.c:31
io_timer_t
const struct io_timer_vtbl *const io_timer_t
An abstract timer.
Definition: timer.h:38
ev_promise_get_future
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:312