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"
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;
}
#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);
}
ngx_elapsed_msec = 0;
ngx_time_update(tv.tv_sec);
+}
+
+
+#if (NGX_THREADS)
-#if (NGX_THREADS0)
- if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT);
- return 0;
+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;
}
-#endif
+ return NGX_OK;
}
+#endif
+
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
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
}
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);
}
-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;
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) {
#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);
#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
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;
}
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);
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)
{
ev->timedout = 1;
}
+#if (NGX_THREADS)
+ /* STUB: post event */
+#endif
+
ev->event_handler(ev);
continue;
}
#include <ngx_event.h>
+#define NGX_TIMER_ERROR (ngx_msec_t) -1
+
/*
* 32 bit timer key value resolution
*
#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;
"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;
"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;
}
}
-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;
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;
}
/* 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);
}
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;
}
#if (NGX_THREADS)
-#if (USE_RFORK)
+#if (NGX_USE_RFORK)
#include <sys/ipc.h>
#include <sys/sem.h>
#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();