diff options
author | Valentin Bartenev <vbart@nginx.com> | 2018-10-22 16:01:10 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2018-10-22 16:01:10 +0300 |
commit | a4aaf906794d9f82710614ba368a36500e8a8254 (patch) | |
tree | 3ce7fb87345badc3ae86f2b4822ce7464f10111d /src/nxt_timer.h | |
parent | 161e1839a5e3d9d906c047c2464a239a632d42c2 (diff) | |
download | unit-a4aaf906794d9f82710614ba368a36500e8a8254.tar.gz unit-a4aaf906794d9f82710614ba368a36500e8a8254.tar.bz2 |
Re-engineered timers.
To optimize rbtree operations, all changes are stored in array and later
processed in batches.
The previous implementation of this mechanics had a number of design flaws.
Each change was saved in a new array entry; until the changes were applied,
the timer remained in an intermediate state (NXT_TIMER_CHANGING).
This intermediate state didn't allow to identify if time was going to be
disabled or enabled. However, the nxt_conn_io_read() function relied on
this information; as a result, in some cases the read timeout wasn't set.
Also, the nxt_timer_delete() function did not reliably track whether a timer
was added to the work queue. It checked the NXT_TIMER_ENQUEUED state of
a timer, but this state could be reset to NXT_TIMER_DISABLED by a
nxt_timer_disable() call or another nxt_timer_delete() call.
Now, instead of keeping the whole history of the timer's changes, the new
implementation updates the timer state immediately, and only one operation
is added to the array to add or delete timer in the rbtree according
to its final state.
Diffstat (limited to 'src/nxt_timer.h')
-rw-r--r-- | src/nxt_timer.h | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/src/nxt_timer.h b/src/nxt_timer.h index 6531657d..211564d6 100644 --- a/src/nxt_timer.h +++ b/src/nxt_timer.h @@ -13,20 +13,24 @@ //#define NXT_TIMER_DEFAULT_PRECISION 1 -typedef enum { - NXT_TIMER_DISABLED = 0, - NXT_TIMER_CHANGING, - NXT_TIMER_WAITING, - NXT_TIMER_ENQUEUED, -} nxt_timer_state_t; +/* + * The nxt_timer_t structure can hold up to 14 bits of change index, + * but 0 reserved for NXT_TIMER_NO_CHANGE. + */ +#define NXT_TIMER_MAX_CHANGES 16383 +#define NXT_TIMER_NO_CHANGE 0 typedef struct { /* The rbtree node must be the first field. */ NXT_RBTREE_NODE (node); - nxt_timer_state_t state:8; uint8_t precision; + + uint16_t change:14; + uint16_t enabled:1; + uint16_t queued:1; + nxt_msec_t time; nxt_work_queue_t *work_queue; @@ -37,13 +41,13 @@ typedef struct { } nxt_timer_t; -#define NXT_TIMER { NXT_RBTREE_NODE_INIT, NXT_TIMER_DISABLED, \ - 0, 0, NULL, NULL, NULL, NULL } +#define NXT_TIMER { NXT_RBTREE_NODE_INIT, 0, NXT_TIMER_NO_CHANGE, \ + 0, 0, 0, NULL, NULL, NULL, NULL } typedef enum { - NXT_TIMER_ADD = 0, - NXT_TIMER_DISABLE, + NXT_TIMER_NOPE = 0, + NXT_TIMER_ADD, NXT_TIMER_DELETE, } nxt_timer_operation_t; @@ -94,10 +98,16 @@ void nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now); NXT_EXPORT void nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, nxt_msec_t timeout); -NXT_EXPORT void nxt_timer_disable(nxt_event_engine_t *engine, - nxt_timer_t *timer); NXT_EXPORT nxt_bool_t nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer); +nxt_inline void +nxt_timer_disable(nxt_event_engine_t *engine, nxt_timer_t *timer) +{ + nxt_debug(timer->task, "timer disable: %M", timer->time); + + timer->enabled = 0; +} + #endif /* _NXT_TIMER_H_INCLUDED_ */ |