Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | #include <ngx_event.h> |
| 10 | |
| 11 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 12 | ngx_thread_volatile ngx_event_t *ngx_posted_accept_events; |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 13 | ngx_thread_volatile ngx_event_t *ngx_posted_events; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 14 | |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 15 | #if (NGX_THREADS) |
| 16 | ngx_mutex_t *ngx_posted_events_mutex; |
| 17 | #endif |
| 18 | |
| 19 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 20 | void |
| 21 | ngx_event_process_posted(ngx_cycle_t *cycle, |
| 22 | ngx_thread_volatile ngx_event_t **posted) |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 23 | { |
| 24 | ngx_event_t *ev; |
| 25 | |
| 26 | for ( ;; ) { |
| 27 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 28 | ev = (ngx_event_t *) *posted; |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 29 | |
| 30 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 31 | "posted event %p", ev); |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 32 | |
| 33 | if (ev == NULL) { |
| 34 | return; |
| 35 | } |
| 36 | |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 37 | ngx_delete_posted_event(ev); |
| 38 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 39 | ev->handler(ev); |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 44 | #if (NGX_THREADS) && !(NGX_WIN32) |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 45 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 46 | void |
| 47 | ngx_wakeup_worker_thread(ngx_cycle_t *cycle) |
Igor Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 48 | { |
Igor Sysoev | f1be53b | 2004-07-05 15:08:23 +0000 | [diff] [blame] | 49 | ngx_int_t i; |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 50 | #if 0 |
Igor Sysoev | f1be53b | 2004-07-05 15:08:23 +0000 | [diff] [blame] | 51 | ngx_uint_t busy; |
| 52 | ngx_event_t *ev; |
| 53 | |
Igor Sysoev | f1be53b | 2004-07-05 15:08:23 +0000 | [diff] [blame] | 54 | 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 Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 73 | |
| 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 Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 83 | ngx_int_t |
| 84 | ngx_event_thread_process_posted(ngx_cycle_t *cycle) |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 85 | { |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 86 | ngx_event_t *ev; |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 87 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 88 | for ( ;; ) { |
| 89 | |
| 90 | ev = (ngx_event_t *) ngx_posted_events; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 91 | |
| 92 | for ( ;; ) { |
| 93 | |
| 94 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 95 | "posted event %p", ev); |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 96 | |
| 97 | if (ev == NULL) { |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 98 | return NGX_OK; |
| 99 | } |
| 100 | |
Igor Sysoev | b14b910 | 2004-06-28 16:05:02 +0000 | [diff] [blame] | 101 | if (ngx_trylock(ev->lock) == 0) { |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 102 | |
| 103 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 104 | "posted event %p is busy", ev); |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 105 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 106 | ev = ev->next; |
| 107 | continue; |
| 108 | } |
| 109 | |
Igor Sysoev | c78c41c | 2004-07-07 06:15:04 +0000 | [diff] [blame] | 110 | if (ev->lock != ev->own_lock) { |
| 111 | if (*(ev->own_lock)) { |
| 112 | ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 113 | "the own lock of the posted event %p is busy", ev); |
Igor Sysoev | c78c41c | 2004-07-07 06:15:04 +0000 | [diff] [blame] | 114 | ngx_unlock(ev->lock); |
| 115 | ev = ev->next; |
| 116 | continue; |
| 117 | } |
| 118 | *(ev->own_lock) = 1; |
| 119 | } |
| 120 | |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 121 | ngx_delete_posted_event(ev); |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 122 | |
Igor Sysoev | 46cd7c2 | 2004-07-02 05:47:00 +0000 | [diff] [blame] | 123 | ev->locked = 1; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 124 | |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 125 | ev->ready |= ev->posted_ready; |
| 126 | ev->timedout |= ev->posted_timedout; |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 127 | ev->pending_eof |= ev->posted_eof; |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 128 | #if (NGX_HAVE_KQUEUE) |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 129 | ev->kq_errno |= ev->posted_errno; |
Igor Sysoev | 46cd7c2 | 2004-07-02 05:47:00 +0000 | [diff] [blame] | 130 | #endif |
Igor Sysoev | f1be53b | 2004-07-05 15:08:23 +0000 | [diff] [blame] | 131 | if (ev->posted_available) { |
| 132 | ev->available = ev->posted_available; |
| 133 | } |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 134 | |
| 135 | ev->posted_ready = 0; |
| 136 | ev->posted_timedout = 0; |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 137 | ev->posted_eof = 0; |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 138 | #if (NGX_HAVE_KQUEUE) |
Igor Sysoev | 98c1cf1 | 2004-07-02 15:54:34 +0000 | [diff] [blame] | 139 | ev->posted_errno = 0; |
| 140 | #endif |
Igor Sysoev | f1be53b | 2004-07-05 15:08:23 +0000 | [diff] [blame] | 141 | ev->posted_available = 0; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 142 | |
Igor Sysoev | 46cd7c2 | 2004-07-02 05:47:00 +0000 | [diff] [blame] | 143 | ngx_mutex_unlock(ngx_posted_events_mutex); |
| 144 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 145 | ev->handler(ev); |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 146 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 147 | ngx_mutex_lock(ngx_posted_events_mutex); |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 148 | |
Igor Sysoev | 46cd7c2 | 2004-07-02 05:47:00 +0000 | [diff] [blame] | 149 | if (ev->locked) { |
| 150 | ngx_unlock(ev->lock); |
Igor Sysoev | c78c41c | 2004-07-07 06:15:04 +0000 | [diff] [blame] | 151 | |
| 152 | if (ev->lock != ev->own_lock) { |
| 153 | ngx_unlock(ev->own_lock); |
| 154 | } |
Igor Sysoev | 46cd7c2 | 2004-07-02 05:47:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Igor Sysoev | 81a432a | 2004-07-06 16:12:16 +0000 | [diff] [blame] | 157 | ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 158 | "posted event %p is done", ev); |
Igor Sysoev | 81a432a | 2004-07-06 16:12:16 +0000 | [diff] [blame] | 159 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 162 | } |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Igor Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 165 | #else |
| 166 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 167 | void |
| 168 | ngx_wakeup_worker_thread(ngx_cycle_t *cycle) |
Igor Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 169 | { |
| 170 | } |
| 171 | |
Igor Sysoev | 9a864bd | 2004-04-04 20:32:09 +0000 | [diff] [blame] | 172 | #endif |