nginx-0.0.2-2004-02-25-23:16:15 import

This commit is contained in:
Igor Sysoev 2004-02-25 20:16:15 +00:00
parent 14dab45638
commit f2334416e7
10 changed files with 123 additions and 34 deletions

View File

@ -2,7 +2,7 @@
if [ $USE_THREADS = "rfork" ]; then
have=NGX_THREADS . auto/have
have=USE_RFORK . auto/have
have=NGX_USE_RFORK . auto/have
CORE_DEPS="$CORE_DEPS $UNIX_THREADS_DEPS"
CORE_SRCS="$CORE_SRCS $FREEBSD_RFORK_SRCS"

View File

@ -147,6 +147,12 @@ int main(int argc, char *const *argv, char **envp)
ctx.argc = argc;
ctx.argv = argv;
#if (NGX_THREADS)
if (ngx_time_mutex_init(log) == NGX_ERROR) {
return 1;
}
#endif
if (ngx_getopt(&ctx, &init_cycle) == NGX_ERROR) {
return 1;
}
@ -649,7 +655,7 @@ static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
#if (NGX_THREADS)
if (ngx_init_threads(5, 128 * 1024 * 1024, cycle->log) == NGX_ERROR) {
if (ngx_init_threads(5, 128 * 1024 * 1024, cycle) == NGX_ERROR) {
/* fatal */
exit(1);
}

View File

@ -51,16 +51,23 @@ void ngx_time_init()
ngx_elapsed_msec = 0;
ngx_time_update(tv.tv_sec);
#if (NGX_THREADS0)
if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT);
return 0;
}
#endif
}
#if (NGX_THREADS)
ngx_int_t ngx_time_mutex_init(ngx_log_t *log)
{
if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT))) {
return NGX_ERROR;
}
return NGX_OK;
}
#endif
void ngx_time_update(time_t s)
{
ngx_tm_t tm;
@ -69,9 +76,11 @@ void ngx_time_update(time_t s)
return;
}
#if (NGX_THREADS0)
if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) {
return;
#if (NGX_THREADS)
if (ngx_time_mutex) {
if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) {
return;
}
}
#endif
@ -109,8 +118,10 @@ void ngx_time_update(time_t s)
tm.ngx_tm_min,
tm.ngx_tm_sec);
#if (NGX_THREADS0)
ngx_mutex_unlock(ngx_time_mutex);
#if (NGX_THREADS)
if (ngx_time_mutex) {
ngx_mutex_unlock(ngx_time_mutex);
}
#endif
}

View File

@ -7,6 +7,9 @@
void ngx_time_init();
#if (NGX_THREADS)
ngx_int_t ngx_time_mutex_init(ngx_log_t *log);
#endif
void ngx_time_update(time_t s);
size_t ngx_http_time(char *buf, time_t t);
void ngx_gmtime(time_t t, ngx_tm_t *tp);

View File

@ -337,7 +337,7 @@ static int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags)
}
static int ngx_kqueue_process_events(ngx_log_t *log)
static ngx_int_t ngx_kqueue_process_events(ngx_log_t *log)
{
int events;
ngx_int_t instance, i;
@ -349,6 +349,19 @@ static int ngx_kqueue_process_events(ngx_log_t *log)
struct timespec ts, *tp;
timer = ngx_event_find_timer();
#if (NGX_THREADS)
if (timer == NGX_TIMER_ERROR) {
return NGX_ERROR;
}
/*
* TODO: if timer is 0 and any worker thread is still busy
* then set 1 second timeout
*/
#endif
ngx_old_elapsed_msec = ngx_elapsed_msec;
if (timer) {

View File

@ -133,7 +133,9 @@ static int ngx_event_init(ngx_cycle_t *cycle)
#endif
ngx_event_timer_init();
if (ngx_event_timer_init(cycle->log) == NGX_ERROR) {
return NGX_ERROR;
}
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);

View File

@ -4,13 +4,8 @@
#include <ngx_event.h>
/*
* TODO: in multithreaded enviroment all timer operations must be
* protected by the single mutex
*/
#if (NGX_THREADS)
static ngx_mutex_t *ngx_event_timer_mutex;
ngx_mutex_t *ngx_event_timer_mutex;
#endif
@ -18,13 +13,20 @@ ngx_rbtree_t *ngx_event_timer_rbtree;
ngx_rbtree_t ngx_event_timer_sentinel;
void ngx_event_timer_init(void)
ngx_int_t ngx_event_timer_init(ngx_log_t *log)
{
if (ngx_event_timer_rbtree) {
return;
ngx_event_timer_mutex->log = log;
return NGX_OK;
}
ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
if (!(ngx_event_timer_mutex = ngx_mutex_init(log, 0))) {
return NGX_ERROR;
}
return NGX_OK;
}
@ -36,8 +38,18 @@ ngx_msec_t ngx_event_find_timer(void)
return 0;
}
#if (NGX_THREADS)
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
return NGX_TIMER_ERROR;
}
#endif
node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel);
#if (NGX_THREADS)
ngx_mutex_unlock(ngx_event_timer_mutex);
#endif
return (ngx_msec_t)
(node->key * NGX_TIMER_RESOLUTION -
ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION);
@ -58,9 +70,19 @@ void ngx_event_expire_timers(ngx_msec_t timer)
break;
}
#if (NGX_THREADS)
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
return;
}
#endif
node = ngx_rbtree_min(ngx_event_timer_rbtree,
&ngx_event_timer_sentinel);
#if (NGX_THREADS)
ngx_mutex_unlock(ngx_event_timer_mutex);
#endif
if ((ngx_msec_t) node->key <= (ngx_msec_t)
(ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION)
{
@ -79,6 +101,10 @@ void ngx_event_expire_timers(ngx_msec_t timer)
ev->timedout = 1;
}
#if (NGX_THREADS)
/* STUB: post event */
#endif
ev->event_handler(ev);
continue;
}

View File

@ -7,6 +7,8 @@
#include <ngx_event.h>
#define NGX_TIMER_ERROR (ngx_msec_t) -1
/*
* 32 bit timer key value resolution
*
@ -19,11 +21,16 @@
#define NGX_TIMER_RESOLUTION 1
void ngx_event_timer_init(void);
ngx_int_t ngx_event_timer_init(ngx_log_t *log);
ngx_msec_t ngx_event_find_timer(void);
void ngx_event_expire_timers(ngx_msec_t timer);
#if (NGX_THREADS)
extern ngx_mutex_t *ngx_event_timer_mutex;
#endif
extern ngx_rbtree_t *ngx_event_timer_rbtree;
extern ngx_rbtree_t ngx_event_timer_sentinel;
@ -34,9 +41,19 @@ ngx_inline static void ngx_event_del_timer(ngx_event_t *ev)
"event timer del: %d: %d",
ngx_event_ident(ev->data), ev->rbtree_key);
#if (NGX_THREADS)
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
return;
}
#endif
ngx_rbtree_delete(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
(ngx_rbtree_t *) &ev->rbtree_key);
#if (NGX_THREADS)
ngx_mutex_unlock(ngx_event_timer_mutex);
#endif
#if (NGX_DEBUG)
ev->rbtree_left = NULL;
ev->rbtree_right = NULL;
@ -64,9 +81,19 @@ ngx_inline static void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
"event timer add: %d: %d",
ngx_event_ident(ev->data), ev->rbtree_key);
#if (NGX_THREADS)
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
return;
}
#endif
ngx_rbtree_insert(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel,
(ngx_rbtree_t *) &ev->rbtree_key);
#if (NGX_THREADS)
ngx_mutex_unlock(ngx_event_timer_mutex);
#endif
ev->timer_set = 1;
}

View File

@ -147,7 +147,7 @@ int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg,
}
ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log)
ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle)
{
size_t len;
char *red_zone, *zone;
@ -156,7 +156,7 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log)
len = sizeof(usrstack);
if (sysctlbyname("kern.usrstack", &usrstack, &len, NULL, 0) == -1) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"sysctlbyname(kern.usrstack) failed");
return NGX_ERROR;
}
@ -164,13 +164,13 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log)
/* the main thread stack red zone */
red_zone = usrstack - (size + rz_size);
ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0,
ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
"usrstack: " PTR_FMT " red zone: " PTR_FMT,
usrstack, red_zone);
zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0);
if (zone == MAP_FAILED) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"mmap(" PTR_FMT ":" SIZE_T_FMT
", PROT_NONE, MAP_ANON) red zone failed",
red_zone, rz_size);
@ -178,18 +178,19 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log)
}
if (zone != red_zone) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "red zone address was changed");
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"red zone address was changed");
}
/* create the threads errno array */
if (!(errnos = ngx_calloc(n * sizeof(int), log))) {
if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
return NGX_ERROR;
}
/* create the threads tid array */
if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), log))) {
if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
return NGX_ERROR;
}

View File

@ -7,7 +7,7 @@
#if (NGX_THREADS)
#if (USE_RFORK)
#if (NGX_USE_RFORK)
#include <sys/ipc.h>
#include <sys/sem.h>
@ -46,7 +46,7 @@ typedef pthread_t ngx_tid_t;
#endif
ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log);
ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle);
int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg,
ngx_log_t *log);
ngx_tid_t ngx_thread_self();