diff options
-rw-r--r-- | auto/sources | 4 | ||||
-rw-r--r-- | src/core/ngx_spinlock.c (renamed from src/event/ngx_event_spinlock.c) | 13 | ||||
-rw-r--r-- | src/event/modules/ngx_kqueue_module.c | 3 | ||||
-rw-r--r-- | src/event/ngx_event_accept.c | 21 | ||||
-rw-r--r-- | src/event/ngx_event_posted.c | 3 | ||||
-rw-r--r-- | src/event/ngx_event_posted.h | 34 | ||||
-rw-r--r-- | src/os/unix/ngx_atomic.h (renamed from src/core/ngx_atomic.h) | 20 | ||||
-rw-r--r-- | src/os/unix/ngx_freebsd_init.c | 15 | ||||
-rw-r--r-- | src/os/unix/ngx_os.h | 1 | ||||
-rw-r--r-- | src/os/unix/ngx_posix_init.c | 5 | ||||
-rw-r--r-- | src/os/unix/ngx_process.h | 2 | ||||
-rw-r--r-- | src/os/win32/ngx_atomic.h | 33 | ||||
-rw-r--r-- | src/os/win32/ngx_os.h | 1 | ||||
-rw-r--r-- | src/os/win32/ngx_process.h | 3 | ||||
-rw-r--r-- | src/os/win32/ngx_win32_init.c | 2 |
15 files changed, 135 insertions, 25 deletions
diff --git a/auto/sources b/auto/sources index 4827f154b..3fcf58eaf 100644 --- a/auto/sources +++ b/auto/sources @@ -6,7 +6,6 @@ CORE_INCS="src/core" CORE_DEPS="src/core/nginx.h \ src/core/ngx_config.h \ src/core/ngx_core.h \ - src/core/ngx_atomic.h \ src/core/ngx_log.h \ src/core/ngx_palloc.h \ src/core/ngx_array.h \ @@ -40,6 +39,7 @@ CORE_SRCS="src/core/nginx.c \ src/core/ngx_times.c \ src/core/ngx_connection.c \ src/core/ngx_cycle.c \ + src/core/ngx_spinlock.c \ src/core/ngx_conf_file.c \ src/core/ngx_garbage_collector.c" @@ -108,6 +108,7 @@ UNIX_DEPS="$CORE_DEPS $EVENT_DEPS \ src/os/unix/ngx_channel.h \ src/os/unix/ngx_shared.h \ src/os/unix/ngx_process.h \ + src/os/unix/ngx_atomic.h \ src/os/unix/ngx_thread.h \ src/os/unix/ngx_socket.h \ src/os/unix/ngx_os.h \ @@ -158,6 +159,7 @@ WIN32_DEPS="$CORE_DEPS $EVENT_DEPS \ src/os/win32/ngx_files.h \ src/os/win32/ngx_shared.h \ src/os/win32/ngx_process.h \ + src/os/win32/ngx_atomic.h \ src/os/win32/ngx_socket.h \ src/os/win32/ngx_os.h \ src/os/win32/ngx_process_cycle.h" diff --git a/src/event/ngx_event_spinlock.c b/src/core/ngx_spinlock.c index 58edb2af5..c3c65d303 100644 --- a/src/event/ngx_event_spinlock.c +++ b/src/core/ngx_spinlock.c @@ -1,19 +1,23 @@ +#include <ngx_config.h> +#include <ngx_core.h> -void _spinlock(ngx_atomic_t *lock) + +void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin) { - ngx_int_t tries; + ngx_uint_t tries; tries = 0; for ( ;; ) { if (*lock) { - if (ngx_ncpu > 1 && tries++ < 1000) { + if (ngx_ncpu > 1 && tries++ < spin) { continue; } - sched_yield(); + ngx_sched_yield(); + tries = 0; } else { @@ -23,4 +27,3 @@ void _spinlock(ngx_atomic_t *lock) } } } - diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index cc84d2eaa..379e5304d 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -506,7 +506,6 @@ static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle) instance = (uintptr_t) ev & 1; ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1); - ev->returned_instance = instance; if (!ev->active || ev->instance != instance) { @@ -520,6 +519,8 @@ static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle) continue; } + ev->returned_instance = instance; + if (ev->log && (ev->log->log_level & NGX_LOG_DEBUG_CONNECTION)) { ngx_kqueue_dump_event(ev->log, &event_list[i]); } diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c index 984570f80..18f8586cd 100644 --- a/src/event/ngx_event_accept.c +++ b/src/event/ngx_event_accept.c @@ -210,10 +210,31 @@ void ngx_event_accept(ngx_event_t *ev) rinstance = rev->returned_instance; winstance = wev->returned_instance; +#if (NGX_THREADS) + + /* + * We has to acquire the lock to avoid the race condition when + * the connection was just closed by another thread but its lock + * is not unlocked at this point and we got the same descriptor. + * + * The condition should be too rare. + */ + + if (ngx_trylock(&c->lock) == 0) { + + /* TODO: ngx_cycle->stat.accept.spinlock++; */ + + ngx_spinlock(&c->lock, 1000); + } + +#endif + ngx_memzero(rev, sizeof(ngx_event_t)); ngx_memzero(wev, sizeof(ngx_event_t)); ngx_memzero(c, sizeof(ngx_connection_t)); + /* ngx_memzero(c) does ngx_unlock(&c->lock); */ + c->pool = pool; c->listening = ls->listening; diff --git a/src/event/ngx_event_posted.c b/src/event/ngx_event_posted.c index 795aa6171..0c91f4ea5 100644 --- a/src/event/ngx_event_posted.c +++ b/src/event/ngx_event_posted.c @@ -111,6 +111,9 @@ ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle) if (ev->posted) { ev->posted = 0; + if (!ev->timedout) { + ev->ready = 1; + } } ev->event_handler(ev); diff --git a/src/event/ngx_event_posted.h b/src/event/ngx_event_posted.h index c2945c4ff..5059fa9ad 100644 --- a/src/event/ngx_event_posted.h +++ b/src/event/ngx_event_posted.h @@ -7,6 +7,19 @@ #include <ngx_event.h> +typedef struct ngx_posted_events_s ngx_posted_event_t; + +struct ngx_posted_events_s { + ngx_event_t *event; + ngx_posted_event_t *next; + + unsigned instance:1; + unsigned ready:1; + unsigned timedout:1; + unsigned complete:1; +}; + + #define ngx_post_event(ev) \ if (!ev->posted) { \ ev->next = (ngx_event_t *) ngx_posted_events; \ @@ -14,6 +27,27 @@ ev->posted = 1; \ } +/* +\ + ngx_log_debug3(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, \ + "POST: %08X %08X %08X", ngx_posted_events, \ + (ngx_posted_events ? ngx_posted_events->next: 0), \ + ((ngx_posted_events && ngx_posted_events->next) ? \ + ngx_posted_events->next->next: 0)); \ +\ +*/ + +/* +\ +{ int i; ngx_event_t *e;\ + e = (ngx_event_t *) ngx_posted_events; \ +for (i = 0; e && i < 10; e = e->next, i++) { \ + ngx_log_debug2(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, \ + "POST: %d %08X", i, e);\ +}} \ +\ +*/ + void ngx_event_process_posted(ngx_cycle_t *cycle); diff --git a/src/core/ngx_atomic.h b/src/os/unix/ngx_atomic.h index a149ad28b..18d5edac5 100644 --- a/src/core/ngx_atomic.h +++ b/src/os/unix/ngx_atomic.h @@ -68,24 +68,6 @@ static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock, } -#elif (WIN32) - -#define ngx_atomic_inc(p) InterlockedIncrement((long *) p) -#define ngx_atomic_dec(p) InterlockedDecrement((long *) p) - -#if defined(__WATCOMC__) || defined(__BORLANDC__) - -#define ngx_atomic_cmp_set(lock, old, set) \ - (InterlockedCompareExchange((long *) lock, set, old) == old) - -#else - -#define ngx_atomic_cmp_set(lock, old, set) \ - (InterlockedCompareExchange((void **) lock, (void *) set, (void *) old) \ - == (void *) old) - -#endif - #else typedef volatile uint32_t ngx_atomic_t; @@ -99,6 +81,8 @@ typedef volatile uint32_t ngx_atomic_t; #endif +void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin); + #define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1)) #define ngx_unlock(lock) *(lock) = 0 diff --git a/src/os/unix/ngx_freebsd_init.c b/src/os/unix/ngx_freebsd_init.c index 35be16412..5addd26a8 100644 --- a/src/os/unix/ngx_freebsd_init.c +++ b/src/os/unix/ngx_freebsd_init.c @@ -8,6 +8,7 @@ char ngx_freebsd_kern_ostype[20]; char ngx_freebsd_kern_osrelease[20]; int ngx_freebsd_kern_osreldate; int ngx_freebsd_hw_ncpu; +int ngx_freebsd_machdep_hlt_logical_cpus; int ngx_freebsd_net_inet_tcp_sendspace; int ngx_freebsd_sendfile_nbytes_bug; int ngx_freebsd_use_tcp_nopush; @@ -42,6 +43,10 @@ sysctl_t sysctls[] = { &ngx_freebsd_hw_ncpu, sizeof(int)}, + {"machdep.hlt_logical_cpus", + &ngx_freebsd_machdep_hlt_logical_cpus, + sizeof(int)}, + {"net.inet.tcp.sendspace", &ngx_freebsd_net_inet_tcp_sendspace, sizeof(int)}, @@ -166,6 +171,10 @@ int ngx_os_init(ngx_log_t *log) == -1) { err = errno; if (err != NGX_ENOENT) { + if (sysctls[i].value == &ngx_freebsd_machdep_hlt_logical_cpus) { + continue; + } + ngx_log_error(NGX_LOG_ALERT, log, err, "sysctlbyname(%s) failed", sysctls[i].name); return NGX_ERROR; @@ -177,5 +186,11 @@ int ngx_os_init(ngx_log_t *log) } } + if (ngx_freebsd_machdep_hlt_logical_cpus) { + ngx_ncpu = ngx_freebsd_hw_ncpu / 2; + } else { + ngx_ncpu = ngx_freebsd_hw_ncpu; + } + return ngx_posix_init(log); } diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h index d9a1a4560..c4da5cdbd 100644 --- a/src/os/unix/ngx_os.h +++ b/src/os/unix/ngx_os.h @@ -47,6 +47,7 @@ ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, extern ngx_os_io_t ngx_os_io; +extern int ngx_ncpu; extern int ngx_max_sockets; extern int ngx_inherited_nonblocking; diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c index f94ffffc6..7498ae609 100644 --- a/src/os/unix/ngx_posix_init.c +++ b/src/os/unix/ngx_posix_init.c @@ -3,6 +3,7 @@ #include <ngx_core.h> +int ngx_ncpu; int ngx_max_sockets; int ngx_inherited_nonblocking; @@ -84,6 +85,10 @@ int ngx_posix_init(ngx_log_t *log) ngx_pagesize = getpagesize(); + if (ngx_ncpu == 0) { + ngx_ncpu = 1; + } + for (sig = signals; sig->signo != 0; sig++) { ngx_memzero(&sa, sizeof(struct sigaction)); sa.sa_handler = sig->handler; diff --git a/src/os/unix/ngx_process.h b/src/os/unix/ngx_process.h index ccea40db7..66ef38d57 100644 --- a/src/os/unix/ngx_process.h +++ b/src/os/unix/ngx_process.h @@ -49,6 +49,8 @@ ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx); void ngx_process_get_status(void); void ngx_close_channel(ngx_fd_t *fd, ngx_log_t *log); +#define ngx_sched_yield() sched_yield() + extern ngx_pid_t ngx_pid; extern ngx_socket_t ngx_channel; diff --git a/src/os/win32/ngx_atomic.h b/src/os/win32/ngx_atomic.h new file mode 100644 index 000000000..fcc3ff1ee --- /dev/null +++ b/src/os/win32/ngx_atomic.h @@ -0,0 +1,33 @@ +#ifndef _NGX_ATOMIC_H_INCLUDED_ +#define _NGX_ATOMIC_H_INCLUDED_ + + +#include <ngx_config.h> +#include <ngx_core.h> + + +#define ngx_atomic_inc(p) InterlockedIncrement((long *) p) +#define ngx_atomic_dec(p) InterlockedDecrement((long *) p) + + +#if defined( __WATCOMC__ ) || defined( __BORLANDC__ ) + +#define ngx_atomic_cmp_set(lock, old, set) \ + (InterlockedCompareExchange((long *) lock, set, old) == old) + +#else + +#define ngx_atomic_cmp_set(lock, old, set) \ + (InterlockedCompareExchange((void **) lock, (void *) set, (void *) old) \ + == (void *) old) + +#endif + + +void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin); + +#define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1)) +#define ngx_unlock(lock) *(lock) = 0 + + +#endif /* _NGX_ATOMIC_H_INCLUDED_ */ diff --git a/src/os/win32/ngx_os.h b/src/os/win32/ngx_os.h index aa66e70d3..84b81a1c4 100644 --- a/src/os/win32/ngx_os.h +++ b/src/os/win32/ngx_os.h @@ -44,6 +44,7 @@ ngx_chain_t *ngx_overlapped_wsasend_chain(ngx_connection_t *c, ngx_chain_t *in, extern ngx_os_io_t ngx_os_io; +extern int ngx_ncpu; extern int ngx_max_sockets; extern int ngx_inherited_nonblocking; extern int ngx_win32_version; diff --git a/src/os/win32/ngx_process.h b/src/os/win32/ngx_process.h index c8587688b..2866c449b 100644 --- a/src/os/win32/ngx_process.h +++ b/src/os/win32/ngx_process.h @@ -23,6 +23,9 @@ typedef struct { ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx); +#define ngx_sched_yeld() Sleep(0) + + extern ngx_pid_t ngx_pid; diff --git a/src/os/win32/ngx_win32_init.c b/src/os/win32/ngx_win32_init.c index 7a25b2845..7d9d433e5 100644 --- a/src/os/win32/ngx_win32_init.c +++ b/src/os/win32/ngx_win32_init.c @@ -4,6 +4,7 @@ int ngx_win32_version; +int ngx_ncpu; int ngx_max_sockets; int ngx_inherited_nonblocking = 1; @@ -124,6 +125,7 @@ int ngx_os_init(ngx_log_t *log) GetSystemInfo(&si); ngx_pagesize = si.dwPageSize; + ngx_ncpu = si.dwNumberOfProcessors; /* init Winsock */ |