-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnc_atomic_rwlock_r.c
More file actions
48 lines (44 loc) · 1.14 KB
/
nc_atomic_rwlock_r.c
File metadata and controls
48 lines (44 loc) · 1.14 KB
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
45
46
47
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "op_atomic.h"
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
ATOMIC_HACK_DECLARE
a_uint32_t thread_count = 0;
a_int32_t lock[4096] = {};
void setup(int num_threads) {}
void* test(void *arg)
{
const int power2 = *(int*) arg;
uint64_t bound = 1L << power2;
uint32_t tid = atomic_fetch_add_explicit(&thread_count, 1,
memory_order_relaxed);
for (uint64_t i = 0; i < bound; i++)
{
while (1)
{
int32_t val;
do
{
val = atomic_load_explicit(&lock[tid],
memory_order_relaxed);
if (val < 0)
continue;
}
while (!atomic_compare_exchange_weak_explicit
(&lock[tid], &val, val + 1,
memory_order_acquire,
memory_order_relaxed));
break;
}
asm("nop");
atomic_fetch_sub_explicit(&lock[tid], 1, memory_order_release);
}
return NULL;
}