forked from liexusong/pcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncx_lock.c
45 lines (31 loc) · 804 Bytes
/
ncx_lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdlib.h>
#include "ncx_lock.h"
extern int pcache_ncpu;
void ncx_shmtx_lock(ncx_atomic_t *lock)
{
int i, n;
int pid = (int) getpid();
for ( ;; ) {
if (*lock == 0 &&
__sync_bool_compare_and_swap(lock, 0, pid)) {
return;
}
if (pcache_ncpu > 1) {
for (n = 1; n < 129; n << 1) {
for (i = 0; i < n; i++) {
__asm__("pause");
}
if (*lock == 0 &&
__sync_bool_compare_and_swap(lock, 0, pid)) {
return;
}
}
}
sched_yield();
}
}
void ncx_shmtx_unlock(ncx_atomic_t *lock)
{
int pid = (int) getpid();
__sync_bool_compare_and_swap(lock, pid, 0);
}