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
36static 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
61struct io_timer_wait *
63{
64 return task ? structof(task, struct io_timer_wait, task) : NULL;
65}
66
67static void
68io_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}
void ev_promise_release(ev_promise_t *promise)
Releases a reference to a promise.
Definition future.c:198
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
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
void * ev_promise_data(const ev_promise_t *promise)
Returns a pointer to the shared state of a promise.
Definition future.c:236
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
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition ev.h:29
This header file is part of the I/O library; it contains the abstract timer interface.
const struct io_timer_vtbl *const io_timer_t
An abstract timer.
Definition timer.h:38
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
#define IO_TIMER_WAIT_INIT(exec, func)
The static initializer for io_timer_wait.
Definition timer.h:65
This is the public header file of the utilities library.
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
Definition util.h:93
This is the internal header file of the I/O library.
A future.
Definition future.c:66
A promise.
Definition future.c:95
An executable task.
Definition task.h:41
A wait operation suitable for use with an I/O timer.
Definition timer.h:54
struct io_timer_wait_result r
The result of the wait operation.
Definition timer.h:61
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the wait operation.
Definition timer.h:59
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