2004-06-21 15:22:53 -04:00
|
|
|
|
2004-09-28 04:34:51 -04:00
|
|
|
/*
|
2004-09-29 12:00:49 -04:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 04:34:51 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-06-30 11:30:41 -04:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2004-06-21 15:22:53 -04:00
|
|
|
|
2004-06-30 11:30:41 -04:00
|
|
|
|
2005-10-19 09:33:58 -03:00
|
|
|
void
|
2006-02-08 12:33:12 -03:00
|
|
|
ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin)
|
2004-06-21 15:22:53 -04:00
|
|
|
{
|
2004-09-22 12:18:21 -04:00
|
|
|
|
|
|
|
#if (NGX_HAVE_ATOMIC_OPS)
|
|
|
|
|
2006-02-08 12:33:12 -03:00
|
|
|
ngx_uint_t i, n;
|
2004-06-21 15:22:53 -04:00
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
2006-02-08 12:33:12 -03:00
|
|
|
if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
|
|
|
|
return;
|
|
|
|
}
|
2004-06-21 15:22:53 -04:00
|
|
|
|
2006-02-08 12:33:12 -03:00
|
|
|
if (ngx_ncpu > 1) {
|
2004-06-30 11:30:41 -04:00
|
|
|
|
2006-02-08 12:33:12 -03:00
|
|
|
for (n = 1; n < spin; n <<= 1) {
|
2004-06-21 15:22:53 -04:00
|
|
|
|
2006-02-08 12:33:12 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
ngx_cpu_pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
|
|
|
|
return;
|
|
|
|
}
|
2004-06-21 15:22:53 -04:00
|
|
|
}
|
|
|
|
}
|
2006-02-08 12:33:12 -03:00
|
|
|
|
|
|
|
ngx_sched_yield();
|
2004-06-21 15:22:53 -04:00
|
|
|
}
|
2004-09-22 12:18:21 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if (NGX_THREADS)
|
|
|
|
|
|
|
|
#error ngx_spinlock() or ngx_atomic_cmp_set() are not defined !
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-06-21 15:22:53 -04:00
|
|
|
}
|