nginx-0.0.7-2004-06-30-19:30:41 import

This commit is contained in:
Igor Sysoev 2004-06-30 15:30:41 +00:00
parent 0a94cfd2ae
commit 078d1b2c22
15 changed files with 135 additions and 25 deletions

View File

@ -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"

View File

@ -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)
}
}
}

View File

@ -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]);
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

33
src/os/win32/ngx_atomic.h Normal file
View File

@ -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_ */

View File

@ -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;

View File

@ -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;

View File

@ -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 */