2006-02-08 23:33:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2006-02-08 23:33:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
|
|
|
#if (NGX_HAVE_ATOMIC_OPS)
|
|
|
|
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
static void ngx_shmtx_wakeup(ngx_shmtx_t *mtx);
|
|
|
|
|
|
|
|
|
2006-02-08 23:33:12 +08:00
|
|
|
ngx_int_t
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_shmtx_create(ngx_shmtx_t *mtx, ngx_shmtx_sh_t *addr, u_char *name)
|
2006-02-08 23:33:12 +08:00
|
|
|
{
|
2011-11-23 21:55:38 +08:00
|
|
|
mtx->lock = &addr->lock;
|
2006-02-08 23:33:12 +08:00
|
|
|
|
2011-05-10 19:39:13 +08:00
|
|
|
if (mtx->spin == (ngx_uint_t) -1) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx->spin = 2048;
|
|
|
|
|
|
|
|
#if (NGX_HAVE_POSIX_SEM)
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
mtx->wait = &addr->wait;
|
|
|
|
|
2011-05-10 19:39:13 +08:00
|
|
|
if (sem_init(&mtx->sem, 1, 0) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
|
|
"sem_init() failed");
|
|
|
|
} else {
|
|
|
|
mtx->semaphore = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-02-08 23:33:12 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-10 19:39:13 +08:00
|
|
|
|
|
|
|
void
|
2012-07-03 21:06:40 +08:00
|
|
|
ngx_shmtx_destroy(ngx_shmtx_t *mtx)
|
2011-05-10 19:39:13 +08:00
|
|
|
{
|
|
|
|
#if (NGX_HAVE_POSIX_SEM)
|
|
|
|
|
|
|
|
if (mtx->semaphore) {
|
|
|
|
if (sem_destroy(&mtx->sem) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
|
|
"sem_destroy() failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_uint_t
|
|
|
|
ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
2011-11-23 21:55:38 +08:00
|
|
|
return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid));
|
2011-05-10 19:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_shmtx_lock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx lock");
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
|
2011-05-10 19:39:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_ncpu > 1) {
|
|
|
|
|
|
|
|
for (n = 1; n < mtx->spin; n <<= 1) {
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
ngx_cpu_pause();
|
|
|
|
}
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
if (*mtx->lock == 0
|
|
|
|
&& ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid))
|
2011-05-10 19:39:13 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (NGX_HAVE_POSIX_SEM)
|
|
|
|
|
|
|
|
if (mtx->semaphore) {
|
2011-11-28 19:01:42 +08:00
|
|
|
(void) ngx_atomic_fetch_add(mtx->wait, 1);
|
2011-05-10 19:39:13 +08:00
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-10 19:39:13 +08:00
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
|
|
"shmtx wait %uA", *mtx->wait);
|
2011-05-10 19:39:13 +08:00
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
while (sem_wait(&mtx->sem) == -1) {
|
|
|
|
ngx_err_t err;
|
2011-05-10 19:39:13 +08:00
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
if (err != NGX_EINTR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
|
|
|
|
"sem_wait() failed while waiting on shmtx");
|
|
|
|
break;
|
2011-05-10 19:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
|
|
"shmtx awoke");
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_sched_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_shmtx_unlock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
|
|
|
if (mtx->spin != (ngx_uint_t) -1) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx unlock");
|
|
|
|
}
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
if (ngx_atomic_cmp_set(mtx->lock, ngx_pid, 0)) {
|
|
|
|
ngx_shmtx_wakeup(mtx);
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 19:39:13 +08:00
|
|
|
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_uint_t
|
|
|
|
ngx_shmtx_force_unlock(ngx_shmtx_t *mtx, ngx_pid_t pid)
|
|
|
|
{
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
|
|
"shmtx forced unlock");
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(mtx->lock, pid, 0)) {
|
|
|
|
ngx_shmtx_wakeup(mtx);
|
|
|
|
return 1;
|
2011-05-10 19:39:13 +08:00
|
|
|
}
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_shmtx_wakeup(ngx_shmtx_t *mtx)
|
|
|
|
{
|
2011-05-10 19:39:13 +08:00
|
|
|
#if (NGX_HAVE_POSIX_SEM)
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_atomic_uint_t wait;
|
2011-05-10 19:39:13 +08:00
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
if (!mtx->semaphore) {
|
2011-05-10 19:39:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
wait = *mtx->wait;
|
|
|
|
|
|
|
|
if (wait == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_atomic_cmp_set(mtx->wait, wait, wait - 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 19:39:13 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
2011-11-23 21:55:38 +08:00
|
|
|
"shmtx wake %uA", wait);
|
2011-05-10 19:39:13 +08:00
|
|
|
|
|
|
|
if (sem_post(&mtx->sem) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
|
|
"sem_post() failed while wake shmtx");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-08 23:33:12 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_shmtx_create(ngx_shmtx_t *mtx, ngx_shmtx_sh_t *addr, u_char *name)
|
2006-02-08 23:33:12 +08:00
|
|
|
{
|
|
|
|
if (mtx->name) {
|
|
|
|
|
|
|
|
if (ngx_strcmp(name, mtx->name) == 0) {
|
|
|
|
mtx->name = name;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-03 21:06:40 +08:00
|
|
|
ngx_shmtx_destroy(mtx);
|
2006-02-08 23:33:12 +08:00
|
|
|
}
|
|
|
|
|
2007-02-11 15:49:12 +08:00
|
|
|
mtx->fd = ngx_open_file(name, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
|
|
|
|
NGX_FILE_DEFAULT_ACCESS);
|
2006-02-08 23:33:12 +08:00
|
|
|
|
|
|
|
if (mtx->fd == NGX_INVALID_FILE) {
|
2006-11-20 16:51:45 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
|
2006-02-08 23:33:12 +08:00
|
|
|
ngx_open_file_n " \"%s\" failed", name);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
2006-11-20 16:51:45 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
2006-02-08 23:33:12 +08:00
|
|
|
ngx_delete_file_n " \"%s\" failed", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx->name = name;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-07-03 21:06:40 +08:00
|
|
|
ngx_shmtx_destroy(ngx_shmtx_t *mtx)
|
2006-02-08 23:33:12 +08:00
|
|
|
{
|
|
|
|
if (ngx_close_file(mtx->fd) == NGX_FILE_ERROR) {
|
2006-11-20 16:51:45 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
2006-02-08 23:33:12 +08:00
|
|
|
ngx_close_file_n " \"%s\" failed", mtx->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-10 19:39:13 +08:00
|
|
|
ngx_uint_t
|
|
|
|
ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
err = ngx_trylock_fd(mtx->fd);
|
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __osf__ /* Tru64 UNIX */
|
|
|
|
|
|
|
|
if (err == NGX_EACCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_log_abort(err, ngx_trylock_fd_n " %s failed", mtx->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_shmtx_lock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
err = ngx_lock_fd(mtx->fd);
|
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_abort(err, ngx_lock_fd_n " %s failed", mtx->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_shmtx_unlock(ngx_shmtx_t *mtx)
|
|
|
|
{
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
err = ngx_unlock_fd(mtx->fd);
|
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_abort(err, ngx_unlock_fd_n " %s failed", mtx->name);
|
|
|
|
}
|
|
|
|
|
2011-11-23 21:55:38 +08:00
|
|
|
|
2011-11-23 22:07:06 +08:00
|
|
|
ngx_uint_t
|
2011-11-23 21:55:38 +08:00
|
|
|
ngx_shmtx_force_unlock(ngx_shmtx_t *mtx, ngx_pid_t pid)
|
|
|
|
{
|
2011-11-23 22:07:06 +08:00
|
|
|
return 0;
|
2011-11-23 21:55:38 +08:00
|
|
|
}
|
|
|
|
|
2006-02-08 23:33:12 +08:00
|
|
|
#endif
|