blob: a7b09fdd6ee963b78507552ce9c2c159f6d673a1 [file] [log] [blame]
Igor Sysoev9a864bd2004-04-04 20:32:09 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev9a864bd2004-04-04 20:32:09 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9#include <ngx_event.h>
10
11
Igor Sysoevc2068d02005-10-19 12:33:58 +000012ngx_thread_volatile ngx_event_t *ngx_posted_accept_events;
Igor Sysoev9a864bd2004-04-04 20:32:09 +000013ngx_thread_volatile ngx_event_t *ngx_posted_events;
Igor Sysoevc0247302004-06-27 18:01:57 +000014
Igor Sysoev9a864bd2004-04-04 20:32:09 +000015#if (NGX_THREADS)
16ngx_mutex_t *ngx_posted_events_mutex;
17#endif
18
19
Igor Sysoevc2068d02005-10-19 12:33:58 +000020void
21ngx_event_process_posted(ngx_cycle_t *cycle,
22 ngx_thread_volatile ngx_event_t **posted)
Igor Sysoev9a864bd2004-04-04 20:32:09 +000023{
24 ngx_event_t *ev;
25
26 for ( ;; ) {
27
Igor Sysoevc2068d02005-10-19 12:33:58 +000028 ev = (ngx_event_t *) *posted;
Igor Sysoev9a864bd2004-04-04 20:32:09 +000029
30 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000031 "posted event %p", ev);
Igor Sysoev9a864bd2004-04-04 20:32:09 +000032
33 if (ev == NULL) {
34 return;
35 }
36
Igor Sysoev98c1cf12004-07-02 15:54:34 +000037 ngx_delete_posted_event(ev);
38
Igor Sysoev899b44e2005-05-12 14:58:06 +000039 ev->handler(ev);
Igor Sysoev9a864bd2004-04-04 20:32:09 +000040 }
41}
42
43
Igor Sysoev1b735832004-11-11 14:07:14 +000044#if (NGX_THREADS) && !(NGX_WIN32)
Igor Sysoev9a864bd2004-04-04 20:32:09 +000045
Igor Sysoevc2068d02005-10-19 12:33:58 +000046void
47ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
Igor Sysoev32fcd5c2004-07-05 06:55:54 +000048{
Igor Sysoevf1be53b2004-07-05 15:08:23 +000049 ngx_int_t i;
Igor Sysoevc1571722005-03-19 12:38:37 +000050#if 0
Igor Sysoevf1be53b2004-07-05 15:08:23 +000051 ngx_uint_t busy;
52 ngx_event_t *ev;
53
Igor Sysoevf1be53b2004-07-05 15:08:23 +000054 busy = 1;
55
56 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
57 return;
58 }
59
60 for (ev = (ngx_event_t *) ngx_posted_events; ev; ev = ev->next) {
61 if (*(ev->lock) == 0) {
62 busy = 0;
63 break;
64 }
65 }
66
67 ngx_mutex_unlock(ngx_posted_events_mutex);
68
69 if (busy) {
70 return;
71 }
72#endif
Igor Sysoev32fcd5c2004-07-05 06:55:54 +000073
74 for (i = 0; i < ngx_threads_n; i++) {
75 if (ngx_threads[i].state == NGX_THREAD_FREE) {
76 ngx_cond_signal(ngx_threads[i].cv);
77 return;
78 }
79 }
80}
81
82
Igor Sysoevc2068d02005-10-19 12:33:58 +000083ngx_int_t
84ngx_event_thread_process_posted(ngx_cycle_t *cycle)
Igor Sysoev9a864bd2004-04-04 20:32:09 +000085{
Igor Sysoev98c1cf12004-07-02 15:54:34 +000086 ngx_event_t *ev;
Igor Sysoev9a864bd2004-04-04 20:32:09 +000087
Igor Sysoevc0247302004-06-27 18:01:57 +000088 for ( ;; ) {
89
90 ev = (ngx_event_t *) ngx_posted_events;
Igor Sysoevc0247302004-06-27 18:01:57 +000091
92 for ( ;; ) {
93
94 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +000095 "posted event %p", ev);
Igor Sysoevc0247302004-06-27 18:01:57 +000096
97 if (ev == NULL) {
Igor Sysoevc0247302004-06-27 18:01:57 +000098 return NGX_OK;
99 }
100
Igor Sysoevb14b9102004-06-28 16:05:02 +0000101 if (ngx_trylock(ev->lock) == 0) {
Igor Sysoevc0247302004-06-27 18:01:57 +0000102
103 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000104 "posted event %p is busy", ev);
Igor Sysoevc0247302004-06-27 18:01:57 +0000105
Igor Sysoevc0247302004-06-27 18:01:57 +0000106 ev = ev->next;
107 continue;
108 }
109
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000110 if (ev->lock != ev->own_lock) {
111 if (*(ev->own_lock)) {
112 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000113 "the own lock of the posted event %p is busy", ev);
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000114 ngx_unlock(ev->lock);
115 ev = ev->next;
116 continue;
117 }
118 *(ev->own_lock) = 1;
119 }
120
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000121 ngx_delete_posted_event(ev);
Igor Sysoevc0247302004-06-27 18:01:57 +0000122
Igor Sysoev46cd7c22004-07-02 05:47:00 +0000123 ev->locked = 1;
Igor Sysoevc0247302004-06-27 18:01:57 +0000124
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000125 ev->ready |= ev->posted_ready;
126 ev->timedout |= ev->posted_timedout;
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000127 ev->pending_eof |= ev->posted_eof;
Igor Sysoevf6906042004-11-25 16:17:31 +0000128#if (NGX_HAVE_KQUEUE)
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000129 ev->kq_errno |= ev->posted_errno;
Igor Sysoev46cd7c22004-07-02 05:47:00 +0000130#endif
Igor Sysoevf1be53b2004-07-05 15:08:23 +0000131 if (ev->posted_available) {
132 ev->available = ev->posted_available;
133 }
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000134
135 ev->posted_ready = 0;
136 ev->posted_timedout = 0;
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000137 ev->posted_eof = 0;
Igor Sysoevf6906042004-11-25 16:17:31 +0000138#if (NGX_HAVE_KQUEUE)
Igor Sysoev98c1cf12004-07-02 15:54:34 +0000139 ev->posted_errno = 0;
140#endif
Igor Sysoevf1be53b2004-07-05 15:08:23 +0000141 ev->posted_available = 0;
Igor Sysoevc0247302004-06-27 18:01:57 +0000142
Igor Sysoev46cd7c22004-07-02 05:47:00 +0000143 ngx_mutex_unlock(ngx_posted_events_mutex);
144
Igor Sysoev899b44e2005-05-12 14:58:06 +0000145 ev->handler(ev);
Igor Sysoevc0247302004-06-27 18:01:57 +0000146
Igor Sysoevc2068d02005-10-19 12:33:58 +0000147 ngx_mutex_lock(ngx_posted_events_mutex);
Igor Sysoevc0247302004-06-27 18:01:57 +0000148
Igor Sysoev46cd7c22004-07-02 05:47:00 +0000149 if (ev->locked) {
150 ngx_unlock(ev->lock);
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000151
152 if (ev->lock != ev->own_lock) {
153 ngx_unlock(ev->own_lock);
154 }
Igor Sysoev46cd7c22004-07-02 05:47:00 +0000155 }
156
Igor Sysoev81a432a2004-07-06 16:12:16 +0000157 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000158 "posted event %p is done", ev);
Igor Sysoev81a432a2004-07-06 16:12:16 +0000159
Igor Sysoevc0247302004-06-27 18:01:57 +0000160 break;
161 }
Igor Sysoev9a864bd2004-04-04 20:32:09 +0000162 }
Igor Sysoev9a864bd2004-04-04 20:32:09 +0000163}
164
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000165#else
166
Igor Sysoevc2068d02005-10-19 12:33:58 +0000167void
168ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000169{
170}
171
Igor Sysoev9a864bd2004-04-04 20:32:09 +0000172#endif