2003-05-07 01:03:16 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
/*
|
|
|
|
* The threads implementation uses the rfork(RFPROC|RFTHREAD|RFMEM)
|
|
|
|
* to create threads. All threads use the stacks of the same size mmap()ed
|
|
|
|
* below the main stack. Thus the stack pointer is used to determine
|
|
|
|
* the current thread id.
|
|
|
|
*
|
|
|
|
* The mutex implementation uses the ngx_atomic_cmp_set() operation
|
|
|
|
* to acquire mutex and the SysV semaphore to wait on a mutex or to wake up
|
|
|
|
* the waiting threads.
|
|
|
|
*
|
|
|
|
* The condition variable implementation uses the SysV semaphore set of two
|
|
|
|
* semaphores. The first is used by the CV mutex, and the second is used
|
|
|
|
* by CV itself.
|
2004-02-25 01:31:46 +08:00
|
|
|
*
|
|
|
|
* This threads implementation currently works on i486 and amd64
|
|
|
|
* platforms only.
|
2004-02-24 04:57:12 +08:00
|
|
|
*/
|
2003-05-07 01:03:16 +08:00
|
|
|
|
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
ngx_int_t ngx_threaded;
|
|
|
|
char *ngx_freebsd_kern_usrstack;
|
|
|
|
size_t ngx_thread_stack_size;
|
2004-02-27 01:10:01 +08:00
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
static size_t rz_size = /* STUB: PAGE_SIZE */ 4096;
|
2003-05-12 23:52:24 +08:00
|
|
|
static size_t usable_stack_size;
|
2003-05-07 01:03:16 +08:00
|
|
|
static char *last_stack;
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
static ngx_uint_t nthreads;
|
|
|
|
static ngx_uint_t max_threads;
|
|
|
|
static ngx_tid_t *tids; /* the threads tids array */
|
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-25 01:31:46 +08:00
|
|
|
/* the thread-safe libc errno */
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
static int errno0; /* the main thread's errno */
|
2004-02-24 04:57:12 +08:00
|
|
|
static int *errnos; /* the threads errno's array */
|
2003-05-07 01:03:16 +08:00
|
|
|
|
|
|
|
int *__error()
|
|
|
|
{
|
2003-05-12 23:52:24 +08:00
|
|
|
int tid;
|
|
|
|
|
|
|
|
tid = ngx_gettid();
|
|
|
|
|
|
|
|
return tid ? &errnos[tid - 1] : &errno0;
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-25 01:31:46 +08:00
|
|
|
/*
|
2004-03-01 05:03:02 +08:00
|
|
|
* __isthreaded enables the spinlocks in some libc functions, i.e. in malloc()
|
2004-02-25 01:31:46 +08:00
|
|
|
* and some other places. Nevertheless we protect our malloc()/free() calls
|
|
|
|
* by own mutex that is more efficient than the spinlock.
|
|
|
|
*
|
2004-03-01 05:03:02 +08:00
|
|
|
* _spinlock() is a weak referenced stub in src/lib/libc/gen/_spinlock_stub.c
|
|
|
|
* that does nothing.
|
2004-02-25 01:31:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern int __isthreaded;
|
|
|
|
|
|
|
|
void _spinlock(ngx_atomic_t *lock)
|
|
|
|
{
|
|
|
|
ngx_int_t tries;
|
|
|
|
|
|
|
|
tries = 0;
|
2004-03-01 05:03:02 +08:00
|
|
|
|
2004-02-25 01:31:46 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
if (*lock) {
|
|
|
|
if (ngx_freebsd_hw_ncpu > 1 && tries++ < 1000) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_yield();
|
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_atomic_cmp_set(lock, 0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
|
|
|
* Before FreeBSD 5.1 _spinunlock() is a simple #define in
|
|
|
|
* src/lib/libc/include/spinlock.h that zeroes lock.
|
|
|
|
*
|
|
|
|
* Since FreeBSD 5.1 _spinunlock() is a weak referenced stub in
|
|
|
|
* src/lib/libc/gen/_spinlock_stub.c that does nothing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _spinunlock
|
|
|
|
|
|
|
|
void _spinunlock(ngx_atomic_t *lock)
|
|
|
|
{
|
|
|
|
*lock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg,
|
|
|
|
ngx_log_t *log)
|
2003-05-07 01:03:16 +08:00
|
|
|
{
|
2004-02-24 04:57:12 +08:00
|
|
|
int id, err;
|
|
|
|
char *stack, *stack_top;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
if (nthreads >= max_threads) {
|
2003-05-12 23:52:24 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, log, 0,
|
2004-02-24 04:57:12 +08:00
|
|
|
"no more than %d threads can be created", max_threads);
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
last_stack -= ngx_thread_stack_size;
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
stack = mmap(last_stack, usable_stack_size, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_STACK, -1, 0);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
if (stack == MAP_FAILED) {
|
2004-02-24 04:57:12 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mmap(" PTR_FMT ":" SIZE_T_FMT
|
|
|
|
", MAP_STACK) thread stack failed",
|
2003-05-12 23:52:24 +08:00
|
|
|
last_stack, usable_stack_size);
|
2003-05-07 01:03:16 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
if (stack != last_stack) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0, "stack address was changed");
|
|
|
|
}
|
|
|
|
|
|
|
|
stack_top = stack + usable_stack_size;
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"thread stack: " PTR_FMT "-" PTR_FMT, stack, stack_top);
|
2003-05-12 23:52:24 +08:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
id = rfork_thread(RFPROC|RFTHREAD|RFMEM, stack_top, func, arg);
|
|
|
|
#elif 1
|
2003-05-07 01:03:16 +08:00
|
|
|
id = rfork_thread(RFPROC|RFMEM, stack_top, func, arg);
|
2003-05-12 23:52:24 +08:00
|
|
|
#elif 1
|
|
|
|
id = rfork_thread(RFFDG|RFCFDG, stack_top, func, arg);
|
2003-05-07 01:03:16 +08:00
|
|
|
#else
|
2003-05-12 23:52:24 +08:00
|
|
|
id = rfork(RFFDG|RFCFDG);
|
2003-05-07 01:03:16 +08:00
|
|
|
#endif
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
err = ngx_errno;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
|
|
|
if (id == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, err, "rfork() failed");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*tid = id;
|
2004-03-01 05:03:02 +08:00
|
|
|
nthreads = (ngx_freebsd_kern_usrstack - stack_top)
|
|
|
|
/ ngx_thread_stack_size;
|
2004-02-24 04:57:12 +08:00
|
|
|
tids[nthreads] = id;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "rfork()ed thread: %d", id);
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle)
|
2003-05-07 01:03:16 +08:00
|
|
|
{
|
2004-02-25 01:31:46 +08:00
|
|
|
size_t len;
|
|
|
|
char *red_zone, *zone;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
max_threads = n;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
len = sizeof(ngx_freebsd_kern_usrstack);
|
|
|
|
if (sysctlbyname("kern.usrstack", &ngx_freebsd_kern_usrstack, &len,
|
|
|
|
NULL, 0) == -1)
|
|
|
|
{
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
2003-05-07 01:03:16 +08:00
|
|
|
"sysctlbyname(kern.usrstack) failed");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
/* the main thread stack red zone */
|
2004-03-01 05:03:02 +08:00
|
|
|
red_zone = ngx_freebsd_kern_usrstack - (size + rz_size);
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"usrstack: " PTR_FMT " red zone: " PTR_FMT,
|
2004-03-01 05:03:02 +08:00
|
|
|
ngx_freebsd_kern_usrstack, red_zone);
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0);
|
2003-05-07 01:03:16 +08:00
|
|
|
if (zone == MAP_FAILED) {
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mmap(" PTR_FMT ":" SIZE_T_FMT
|
|
|
|
", PROT_NONE, MAP_ANON) red zone failed",
|
2004-02-24 04:57:12 +08:00
|
|
|
red_zone, rz_size);
|
2003-05-07 01:03:16 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
if (zone != red_zone) {
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
|
|
"red zone address was changed");
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
/* create the threads errno array */
|
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
|
2004-02-24 04:57:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the threads tid array */
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
|
2004-02-24 04:57:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
tids[0] = ngx_pid;
|
|
|
|
nthreads = 1;
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
last_stack = zone + rz_size;
|
2003-05-12 23:52:24 +08:00
|
|
|
usable_stack_size = size;
|
2004-03-01 05:03:02 +08:00
|
|
|
ngx_thread_stack_size = size + rz_size;
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
/* allow the spinlock in libc malloc() */
|
|
|
|
__isthreaded = 1;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-27 01:10:01 +08:00
|
|
|
ngx_threaded = 1;
|
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_tid_t ngx_thread_self()
|
|
|
|
{
|
2003-05-12 23:52:24 +08:00
|
|
|
int tid;
|
|
|
|
ngx_tid_t pid;
|
|
|
|
|
|
|
|
tid = ngx_gettid();
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
if (tids == NULL) {
|
|
|
|
return ngx_pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2003-05-12 23:52:24 +08:00
|
|
|
if (tids[tid] == 0) {
|
2004-02-24 04:57:12 +08:00
|
|
|
pid = ngx_pid;
|
2003-05-12 23:52:24 +08:00
|
|
|
tids[tid] = pid;
|
|
|
|
return pid;
|
|
|
|
}
|
2004-02-24 04:57:12 +08:00
|
|
|
#endif
|
2003-05-12 23:52:24 +08:00
|
|
|
|
|
|
|
return tids[tid];
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags)
|
2003-05-21 21:28:21 +08:00
|
|
|
{
|
2004-02-24 04:57:12 +08:00
|
|
|
int nsem, i;
|
|
|
|
ngx_mutex_t *m;
|
|
|
|
union semun op;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
if (!(m = ngx_alloc(sizeof(ngx_mutex_t), log))) {
|
|
|
|
return NULL;
|
2003-05-12 23:52:24 +08:00
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
m->lock = 0;
|
|
|
|
m->log = log;
|
2003-05-07 01:03:16 +08:00
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
if (flags & NGX_MUTEX_LIGHT) {
|
|
|
|
m->semid = -1;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsem = flags & NGX_MUTEX_CV ? 2 : 1;
|
|
|
|
|
|
|
|
m->semid = semget(IPC_PRIVATE, nsem, SEM_R|SEM_A);
|
|
|
|
if (m->semid == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "semget() failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
op.val = 0;
|
|
|
|
for (i = 0; i < nsem; i++) {
|
|
|
|
if (semctl(m->semid, i, SETVAL, op) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
"semctl(SETVAL) failed");
|
|
|
|
|
|
|
|
if (semctl(m->semid, 0, IPC_RMID) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
|
|
|
|
"semctl(IPC_RMID) failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_mutex_done(ngx_mutex_t *m)
|
|
|
|
{
|
|
|
|
if (semctl(m->semid, 0, IPC_RMID) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
|
|
|
|
"semctl(IPC_RMID) failed");
|
|
|
|
}
|
|
|
|
|
2004-02-25 01:31:46 +08:00
|
|
|
ngx_free((void *) m);
|
2004-02-24 04:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
|
2004-02-24 04:57:12 +08:00
|
|
|
{
|
|
|
|
uint32_t lock, new, old;
|
|
|
|
ngx_uint_t tries;
|
|
|
|
struct sembuf op;
|
|
|
|
|
2004-02-27 01:10:01 +08:00
|
|
|
if (!ngx_threaded) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
#if (NGX_DEBUG)
|
|
|
|
if (try) {
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"try lock mutex " PTR_FMT " lock:%X", m, m->lock);
|
2004-02-24 04:57:12 +08:00
|
|
|
} else {
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"lock mutex " PTR_FMT " lock:%X", m, m->lock);
|
2004-02-24 04:57:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
if (old & NGX_MUTEX_LOCK_BUSY) {
|
|
|
|
|
|
|
|
if (try) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_freebsd_hw_ncpu > 1 && tries++ < 1000) {
|
|
|
|
|
|
|
|
/* the spinlock is used only on the SMP system */
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->semid == -1) {
|
|
|
|
sched_yield();
|
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
old = m->lock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mutex " PTR_FMT " lock:%X", m, m->lock);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The mutex is locked so we increase a number
|
|
|
|
* of the threads that are waiting on the mutex
|
|
|
|
*/
|
|
|
|
|
|
|
|
lock = old + 1;
|
|
|
|
|
|
|
|
if ((lock & ~NGX_MUTEX_LOCK_BUSY) > nthreads) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
|
2004-02-25 01:31:46 +08:00
|
|
|
"%d threads wait for mutex " PTR_FMT
|
|
|
|
", while only %d threads are available",
|
2004-02-24 04:57:12 +08:00
|
|
|
lock & ~NGX_MUTEX_LOCK_BUSY, m, nthreads);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"wait mutex " PTR_FMT " lock:%X", m, m->lock);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The number of the waiting threads has been increased
|
|
|
|
* and we would wait on the SysV semaphore.
|
|
|
|
* A semaphore should wake up us more efficiently than
|
|
|
|
* a simple usleep().
|
|
|
|
*/
|
|
|
|
|
|
|
|
op.sem_num = 0;
|
|
|
|
op.sem_op = -1;
|
|
|
|
op.sem_flg = SEM_UNDO;
|
|
|
|
|
|
|
|
if (semop(m->semid, &op, 1) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
|
|
|
|
"semop() failed while waiting "
|
2004-02-25 01:31:46 +08:00
|
|
|
"on mutex " PTR_FMT, m);
|
2004-02-24 04:57:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
old = m->lock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
lock = old | NGX_MUTEX_LOCK_BUSY;
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
|
|
|
|
|
|
|
|
/* we locked the mutex */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tries++ > 1000) {
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mutex " PTR_FMT " is contested", m);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
/* the mutex is probably contested so we are giving up now */
|
|
|
|
|
|
|
|
sched_yield();
|
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
old = m->lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mutex " PTR_FMT " is locked, lock:%X", m, m->lock);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
|
|
|
|
{
|
|
|
|
uint32_t lock, new, old;
|
|
|
|
struct sembuf op;
|
|
|
|
|
2004-02-27 01:10:01 +08:00
|
|
|
if (!ngx_threaded) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
old = m->lock;
|
|
|
|
|
|
|
|
if (!(old & NGX_MUTEX_LOCK_BUSY)) {
|
2004-03-01 05:03:02 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"tring to unlock the free mutex " PTR_FMT, m);
|
2004-02-24 04:57:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the mutex */
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
lock = old & ~NGX_MUTEX_LOCK_BUSY;
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->semid == -1) {
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mutex " PTR_FMT " is unlocked", m);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check weather we need to wake up a waiting thread */
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
if (old & NGX_MUTEX_LOCK_BUSY) {
|
|
|
|
|
|
|
|
/* the mutex is just locked by another thread */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* there are the waiting threads */
|
|
|
|
|
|
|
|
lock = old - 1;
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
|
|
|
|
|
|
|
|
/* wake up the thread that waits on semaphore */
|
|
|
|
|
|
|
|
op.sem_num = 0;
|
|
|
|
op.sem_op = 1;
|
|
|
|
op.sem_flg = SEM_UNDO;
|
|
|
|
|
|
|
|
if (semop(m->semid, &op, 1) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
|
2004-02-25 01:31:46 +08:00
|
|
|
"semop() failed while waking up on mutex "
|
|
|
|
PTR_FMT, m);
|
2004-02-24 04:57:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = m->lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
|
2004-02-25 01:31:46 +08:00
|
|
|
"mutex " PTR_FMT " is unlocked", m);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|