Commit Graph

9 Commits

Author SHA1 Message Date
Valentin Bartenev
ce6ce15c20 Fixed violation of the strict aliasing rules in 5d0edd35c4ce.
In order to reduce number of operations over rb-tree and process them in
batches simultaneously, all the timers changes are temporary stored in array.
While processing of these changes, the same memory is also used for storing
pointers to postpone timers adding.

As the same block of memory has been referenced by two different types of
pointers (nxt_timer_change_t * and nxt_timer_t **), some compilers may reorder
operations with these pointers and produce broken code.  See ticket #221 on
GitHub for a particular case.

Now the same "nxt_timer_change_t" structure is used in both cases.

Also, reverted the -fno-strict-aliasing flag, which has been introduced in
ef76227ec159 as a workaround for this issue.
2019-02-26 17:42:20 +03:00
Valentin Bartenev
da0ef366dc Handling of timers with bias.
Timers that don't require maximum precision (most of them, actually) can be
triggered earlier or later within the bias interval.

To reduce wakeups by timers, the expire function now triggers not only all
timers that fall within the elapsed time, but also those whose bias falls
within this interval.
2018-10-22 16:04:16 +03:00
Valentin Bartenev
b20e995e80 Timers: separation of delete and insert operations on rbtree.
Delete/insert operation complexity for a red-black tree is O(log n),
where n is the total number of tree elements.

If all delete operations are performed before all insert operations,
the average number of tree elements during an operation will be
lower than in the mixed-operations case.
2018-10-22 16:02:14 +03:00
Valentin Bartenev
a4aaf90679 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.
2018-10-22 16:01:10 +03:00
Igor Sysoev
bb8c285678 Using type for nxt_msec_diff(). 2018-03-28 19:07:40 +03:00
Sergey Kandaurov
771e9d3cc3 Fixed formatting in nxt_sprintf() and logging. 2018-01-24 15:16:33 +03:00
Igor Sysoev
eb5c49ead9 Timer should be marked as it is not in tree after deletion.
This closes #64 issue on GitHub.
2017-11-27 18:50:31 +03:00
Igor Sysoev
18281ee37e Event engine timers refactoring. 2017-01-31 22:26:50 +03:00
Igor Sysoev
bb87fa11ca nxt_event_timer has been renamed to nxt_timer. 2017-01-30 16:47:50 +03:00