Mercurial > hg > nginx
changeset 373:018569a8f09c
nginx-0.0.7-2004-06-30-19:30:41 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 30 Jun 2004 15:30:41 +0000 |
parents | c9fdfccebc49 |
children | 213f17e9f776 |
files | auto/sources src/core/ngx_atomic.h src/core/ngx_spinlock.c src/event/modules/ngx_kqueue_module.c src/event/ngx_event_accept.c src/event/ngx_event_posted.c src/event/ngx_event_posted.h src/event/ngx_event_spinlock.c src/os/unix/ngx_atomic.h src/os/unix/ngx_freebsd_init.c src/os/unix/ngx_os.h src/os/unix/ngx_posix_init.c src/os/unix/ngx_process.h src/os/win32/ngx_atomic.h src/os/win32/ngx_os.h src/os/win32/ngx_process.h src/os/win32/ngx_win32_init.c |
diffstat | 17 files changed, 244 insertions(+), 134 deletions(-) [+] |
line wrap: on
line diff
--- a/auto/sources Mon Jun 28 21:03:14 2004 +0000 +++ b/auto/sources Wed Jun 30 15:30:41 2004 +0000 @@ -6,7 +6,6 @@ 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 @@ 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 @@ 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 @@ 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"
--- a/src/core/ngx_atomic.h Mon Jun 28 21:03:14 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -#ifndef _NGX_ATOMIC_H_INCLUDED_ -#define _NGX_ATOMIC_H_INCLUDED_ - - -#include <ngx_config.h> -#include <ngx_core.h> - - -#if ( __i386__ || __amd64__ ) - -typedef volatile uint32_t ngx_atomic_t; - -#if (NGX_SMP) -#define NGX_SMP_LOCK "lock;" -#else -#define NGX_SMP_LOCK -#endif - - -static ngx_inline uint32_t ngx_atomic_inc(ngx_atomic_t *value) -{ - uint32_t old; - - __asm__ volatile ( - - NGX_SMP_LOCK - " xaddl %0, %2; " - " incl %0; " - - : "=q" (old) : "0" (1), "m" (*value)); - - return old; -} - - -static ngx_inline uint32_t ngx_atomic_dec(ngx_atomic_t *value) -{ - uint32_t old; - - __asm__ volatile ( - - NGX_SMP_LOCK - " xaddl %0, %1; " - " decl %0; " - - : "=q" (old) : "0" (-1), "m" (*value)); - - return old; -} - - -static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock, - ngx_atomic_t old, - ngx_atomic_t set) -{ - uint32_t res; - - __asm__ volatile ( - - NGX_SMP_LOCK - " cmpxchgl %3, %1; " - " setz %%al; " - " movzbl %%al, %0; " - - : "=a" (res) : "m" (*lock), "a" (old), "q" (set)); - - return res; -} - - -#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; - -/* STUB */ -#define ngx_atomic_inc(x) (*(x))++; -#define ngx_atomic_dec(x) (*(x))--; -#define ngx_atomic_cmp_set(lock, old, set) 1 -/**/ - -#endif - - -#define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1)) -#define ngx_unlock(lock) *(lock) = 0 - - -#endif /* _NGX_ATOMIC_H_INCLUDED_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/ngx_spinlock.c Wed Jun 30 15:30:41 2004 +0000 @@ -0,0 +1,29 @@ + +#include <ngx_config.h> +#include <ngx_core.h> + + +void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin) +{ + ngx_uint_t tries; + + tries = 0; + + for ( ;; ) { + + if (*lock) { + if (ngx_ncpu > 1 && tries++ < spin) { + continue; + } + + ngx_sched_yield(); + + tries = 0; + + } else { + if (ngx_atomic_cmp_set(lock, 0, 1)) { + return; + } + } + } +}
--- a/src/event/modules/ngx_kqueue_module.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/event/modules/ngx_kqueue_module.c Wed Jun 30 15:30:41 2004 +0000 @@ -506,7 +506,6 @@ 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 @@ 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]); }
--- a/src/event/ngx_event_accept.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/event/ngx_event_accept.c Wed Jun 30 15:30:41 2004 +0000 @@ -210,10 +210,31 @@ 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;
--- a/src/event/ngx_event_posted.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/event/ngx_event_posted.c Wed Jun 30 15:30:41 2004 +0000 @@ -111,6 +111,9 @@ if (ev->posted) { ev->posted = 0; + if (!ev->timedout) { + ev->ready = 1; + } } ev->event_handler(ev);
--- a/src/event/ngx_event_posted.h Mon Jun 28 21:03:14 2004 +0000 +++ b/src/event/ngx_event_posted.h Wed Jun 30 15:30:41 2004 +0000 @@ -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);
--- a/src/event/ngx_event_spinlock.c Mon Jun 28 21:03:14 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ - - -void _spinlock(ngx_atomic_t *lock) -{ - ngx_int_t tries; - - tries = 0; - - for ( ;; ) { - - if (*lock) { - if (ngx_ncpu > 1 && tries++ < 1000) { - continue; - } - - sched_yield(); - tries = 0; - - } else { - if (ngx_atomic_cmp_set(lock, 0, 1)) { - return; - } - } - } -} -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/os/unix/ngx_atomic.h Wed Jun 30 15:30:41 2004 +0000 @@ -0,0 +1,90 @@ +#ifndef _NGX_ATOMIC_H_INCLUDED_ +#define _NGX_ATOMIC_H_INCLUDED_ + + +#include <ngx_config.h> +#include <ngx_core.h> + + +#if ( __i386__ || __amd64__ ) + +typedef volatile uint32_t ngx_atomic_t; + +#if (NGX_SMP) +#define NGX_SMP_LOCK "lock;" +#else +#define NGX_SMP_LOCK +#endif + + +static ngx_inline uint32_t ngx_atomic_inc(ngx_atomic_t *value) +{ + uint32_t old; + + __asm__ volatile ( + + NGX_SMP_LOCK + " xaddl %0, %2; " + " incl %0; " + + : "=q" (old) : "0" (1), "m" (*value)); + + return old; +} + + +static ngx_inline uint32_t ngx_atomic_dec(ngx_atomic_t *value) +{ + uint32_t old; + + __asm__ volatile ( + + NGX_SMP_LOCK + " xaddl %0, %1; " + " decl %0; " + + : "=q" (old) : "0" (-1), "m" (*value)); + + return old; +} + + +static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock, + ngx_atomic_t old, + ngx_atomic_t set) +{ + uint32_t res; + + __asm__ volatile ( + + NGX_SMP_LOCK + " cmpxchgl %3, %1; " + " setz %%al; " + " movzbl %%al, %0; " + + : "=a" (res) : "m" (*lock), "a" (old), "q" (set)); + + return res; +} + + +#else + +typedef volatile uint32_t ngx_atomic_t; + +/* STUB */ +#define ngx_atomic_inc(x) (*(x))++; +#define ngx_atomic_dec(x) (*(x))--; +#define ngx_atomic_cmp_set(lock, old, set) 1 +/**/ + +#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_ */
--- a/src/os/unix/ngx_freebsd_init.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/unix/ngx_freebsd_init.c Wed Jun 30 15:30:41 2004 +0000 @@ -8,6 +8,7 @@ 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 @@ &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 @@ == -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 @@ } } + 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); }
--- a/src/os/unix/ngx_os.h Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/unix/ngx_os.h Wed Jun 30 15:30:41 2004 +0000 @@ -47,6 +47,7 @@ extern ngx_os_io_t ngx_os_io; +extern int ngx_ncpu; extern int ngx_max_sockets; extern int ngx_inherited_nonblocking;
--- a/src/os/unix/ngx_posix_init.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/unix/ngx_posix_init.c Wed Jun 30 15:30:41 2004 +0000 @@ -3,6 +3,7 @@ #include <ngx_core.h> +int ngx_ncpu; int ngx_max_sockets; int ngx_inherited_nonblocking; @@ -84,6 +85,10 @@ 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;
--- a/src/os/unix/ngx_process.h Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/unix/ngx_process.h Wed Jun 30 15:30:41 2004 +0000 @@ -49,6 +49,8 @@ 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;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/os/win32/ngx_atomic.h Wed Jun 30 15:30:41 2004 +0000 @@ -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_ */
--- a/src/os/win32/ngx_os.h Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/win32/ngx_os.h Wed Jun 30 15:30:41 2004 +0000 @@ -44,6 +44,7 @@ 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;
--- a/src/os/win32/ngx_process.h Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/win32/ngx_process.h Wed Jun 30 15:30:41 2004 +0000 @@ -23,6 +23,9 @@ 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;
--- a/src/os/win32/ngx_win32_init.c Mon Jun 28 21:03:14 2004 +0000 +++ b/src/os/win32/ngx_win32_init.c Wed Jun 30 15:30:41 2004 +0000 @@ -4,6 +4,7 @@ int ngx_win32_version; +int ngx_ncpu; int ngx_max_sockets; int ngx_inherited_nonblocking = 1; @@ -124,6 +125,7 @@ GetSystemInfo(&si); ngx_pagesize = si.dwPageSize; + ngx_ncpu = si.dwNumberOfProcessors; /* init Winsock */