xref: /unit/src/nxt_timer.h (revision 1008:84f2370bd642)
16Sigor@sysoev.ru 
26Sigor@sysoev.ru /*
36Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
46Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
56Sigor@sysoev.ru  */
66Sigor@sysoev.ru 
76Sigor@sysoev.ru #ifndef _NXT_TIMER_H_INCLUDED_
86Sigor@sysoev.ru #define _NXT_TIMER_H_INCLUDED_
96Sigor@sysoev.ru 
106Sigor@sysoev.ru 
11811Svbart@nginx.com /* Valid values are between 0ms to 255ms. */
12811Svbart@nginx.com #define NXT_TIMER_DEFAULT_BIAS  50
13811Svbart@nginx.com //#define NXT_TIMER_DEFAULT_BIAS  0
146Sigor@sysoev.ru 
156Sigor@sysoev.ru 
16809Svbart@nginx.com /*
17809Svbart@nginx.com  * The nxt_timer_t structure can hold up to 14 bits of change index,
18809Svbart@nginx.com  * but 0 reserved for NXT_TIMER_NO_CHANGE.
19809Svbart@nginx.com  */
20809Svbart@nginx.com #define NXT_TIMER_MAX_CHANGES  16383
21809Svbart@nginx.com #define NXT_TIMER_NO_CHANGE    0
226Sigor@sysoev.ru 
236Sigor@sysoev.ru 
246Sigor@sysoev.ru typedef struct {
256Sigor@sysoev.ru     /* The rbtree node must be the first field. */
266Sigor@sysoev.ru     NXT_RBTREE_NODE           (node);
276Sigor@sysoev.ru 
28811Svbart@nginx.com     uint8_t                   bias;
29809Svbart@nginx.com 
30809Svbart@nginx.com     uint16_t                  change:14;
31809Svbart@nginx.com     uint16_t                  enabled:1;
32809Svbart@nginx.com     uint16_t                  queued:1;
33809Svbart@nginx.com 
346Sigor@sysoev.ru     nxt_msec_t                time;
356Sigor@sysoev.ru 
366Sigor@sysoev.ru     nxt_work_queue_t          *work_queue;
376Sigor@sysoev.ru     nxt_work_handler_t        handler;
386Sigor@sysoev.ru 
396Sigor@sysoev.ru     nxt_task_t                *task;
406Sigor@sysoev.ru     nxt_log_t                 *log;
416Sigor@sysoev.ru } nxt_timer_t;
426Sigor@sysoev.ru 
436Sigor@sysoev.ru 
44809Svbart@nginx.com #define NXT_TIMER             { NXT_RBTREE_NODE_INIT, 0, NXT_TIMER_NO_CHANGE, \
45809Svbart@nginx.com                                 0, 0, 0, NULL, NULL, NULL, NULL }
467Sigor@sysoev.ru 
477Sigor@sysoev.ru 
487Sigor@sysoev.ru typedef enum {
49809Svbart@nginx.com     NXT_TIMER_NOPE = 0,
50809Svbart@nginx.com     NXT_TIMER_ADD,
517Sigor@sysoev.ru     NXT_TIMER_DELETE,
527Sigor@sysoev.ru } nxt_timer_operation_t;
537Sigor@sysoev.ru 
547Sigor@sysoev.ru 
556Sigor@sysoev.ru typedef struct {
567Sigor@sysoev.ru     nxt_timer_operation_t     change:8;
576Sigor@sysoev.ru     nxt_msec_t                time;
586Sigor@sysoev.ru     nxt_timer_t               *timer;
596Sigor@sysoev.ru } nxt_timer_change_t;
606Sigor@sysoev.ru 
616Sigor@sysoev.ru 
626Sigor@sysoev.ru typedef struct {
636Sigor@sysoev.ru     nxt_rbtree_t              tree;
646Sigor@sysoev.ru 
656Sigor@sysoev.ru     /* An overflown milliseconds counter. */
666Sigor@sysoev.ru     nxt_msec_t                now;
677Sigor@sysoev.ru     nxt_msec_t                minimum;
686Sigor@sysoev.ru 
696Sigor@sysoev.ru     nxt_uint_t                mchanges;
706Sigor@sysoev.ru     nxt_uint_t                nchanges;
716Sigor@sysoev.ru 
72*1008Szelenkov@nginx.com     nxt_timer_change_t        *changes;
736Sigor@sysoev.ru } nxt_timers_t;
746Sigor@sysoev.ru 
756Sigor@sysoev.ru 
766Sigor@sysoev.ru #define nxt_timer_data(obj, type, timer)                                      \
776Sigor@sysoev.ru     nxt_container_of(obj, type, timer)
786Sigor@sysoev.ru 
796Sigor@sysoev.ru 
806Sigor@sysoev.ru /*
816Sigor@sysoev.ru  * When timer resides in rbtree all links of its node are not NULL.
826Sigor@sysoev.ru  * A parent link is the nearst to other timer flags.
836Sigor@sysoev.ru  */
846Sigor@sysoev.ru 
856Sigor@sysoev.ru #define nxt_timer_is_in_tree(timer)                                           \
866Sigor@sysoev.ru     ((timer)->node.parent != NULL)
876Sigor@sysoev.ru 
886Sigor@sysoev.ru #define nxt_timer_in_tree_set(timer)
896Sigor@sysoev.ru     /* Noop, because rbtree insertion sets a node's parent link. */
906Sigor@sysoev.ru 
916Sigor@sysoev.ru #define nxt_timer_in_tree_clear(timer)                                        \
926Sigor@sysoev.ru     (timer)->node.parent = NULL
936Sigor@sysoev.ru 
946Sigor@sysoev.ru 
957Sigor@sysoev.ru nxt_int_t nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges);
967Sigor@sysoev.ru nxt_msec_t nxt_timer_find(nxt_event_engine_t *engine);
977Sigor@sysoev.ru void nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now);
986Sigor@sysoev.ru 
996Sigor@sysoev.ru NXT_EXPORT void nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer,
1006Sigor@sysoev.ru     nxt_msec_t timeout);
1017Sigor@sysoev.ru NXT_EXPORT nxt_bool_t nxt_timer_delete(nxt_event_engine_t *engine,
1027Sigor@sysoev.ru     nxt_timer_t *timer);
1036Sigor@sysoev.ru 
104809Svbart@nginx.com nxt_inline void
nxt_timer_disable(nxt_event_engine_t * engine,nxt_timer_t * timer)105809Svbart@nginx.com nxt_timer_disable(nxt_event_engine_t *engine, nxt_timer_t *timer)
106809Svbart@nginx.com {
107809Svbart@nginx.com     nxt_debug(timer->task, "timer disable: %M", timer->time);
108809Svbart@nginx.com 
109809Svbart@nginx.com     timer->enabled = 0;
110809Svbart@nginx.com }
111809Svbart@nginx.com 
1126Sigor@sysoev.ru 
1136Sigor@sysoev.ru #endif /* _NXT_TIMER_H_INCLUDED_ */
114