-
Notifications
You must be signed in to change notification settings - Fork 8
/
atomic_punch_card.c
58 lines (53 loc) · 1.37 KB
/
atomic_punch_card.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
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
extern uint64_t counts[];
a_int32_t pcard[SLOT] = {};
void setup(int num_threads) {}
void* test(void *arg)
{
uint64_t bound = *(uint64_t*)arg;
uint64_t mask = (1L << 4)-1; // 1/16 use write lock, else read lock
uint32_t tid = atomic_fetch_add_explicit(&thread_count, 1,
memory_order_relaxed);
uint32_t slot = tid % SLOT;
for (uint64_t i = 0; i < bound; i++)
{
while (1)
{
if (!atomic_check_in(&pcard[slot]))
continue;
if ((i & mask) == 0)
{
if (!atomic_book_critical(&pcard[slot]))
{
atomic_check_out(&pcard[slot]);
continue;
}
while(!atomic_enter_critical(&pcard[slot]))
;
counts[slot]++;
atomic_exit_critical(&pcard[slot]);
atomic_check_out(&pcard[slot]);
break;
}
else
{
asm("nop");
atomic_check_out(&pcard[slot]);
break;
}
}
}
return NULL;
}