2004-06-22 03:22:53 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-06-30 23:30:41 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2004-06-22 03:22:53 +08:00
|
|
|
|
2004-06-30 23:30:41 +08:00
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
/*
|
|
|
|
* TODO: the P4 optimized assembler version with the "pause" operation
|
|
|
|
*/
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
void
|
|
|
|
ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin)
|
2004-06-22 03:22:53 +08:00
|
|
|
{
|
2004-09-23 00:18:21 +08:00
|
|
|
|
|
|
|
#if (NGX_HAVE_ATOMIC_OPS)
|
|
|
|
|
2004-06-30 23:30:41 +08:00
|
|
|
ngx_uint_t tries;
|
2004-06-22 03:22:53 +08:00
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
if (*lock) {
|
2004-06-30 23:30:41 +08:00
|
|
|
if (ngx_ncpu > 1 && tries++ < spin) {
|
2004-06-22 03:22:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-30 23:30:41 +08:00
|
|
|
ngx_sched_yield();
|
|
|
|
|
2004-06-22 03:22:53 +08:00
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_atomic_cmp_set(lock, 0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-23 00:18:21 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if (NGX_THREADS)
|
|
|
|
|
|
|
|
#error ngx_spinlock() or ngx_atomic_cmp_set() are not defined !
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-06-22 03:22:53 +08:00
|
|
|
}
|