comparison src/core/ngx_spinlock.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children 41ccba1aba45
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10
11 void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin)
12 {
13
14 #if (NGX_HAVE_ATOMIC_OPS)
15
16 ngx_uint_t tries;
17
18 tries = 0;
19
20 for ( ;; ) {
21
22 if (*lock) {
23 if (ngx_ncpu > 1 && tries++ < spin) {
24 continue;
25 }
26
27 ngx_sched_yield();
28
29 tries = 0;
30
31 } else {
32 if (ngx_atomic_cmp_set(lock, 0, 1)) {
33 return;
34 }
35 }
36 }
37
38 #else
39
40 #if (NGX_THREADS)
41
42 #error ngx_spinlock() or ngx_atomic_cmp_set() are not defined !
43
44 #endif
45
46 #endif
47
48 }