aboutsummaryrefslogtreecommitdiff
path: root/SingleSource/UnitTests/AtomicOps.c
blob: 9c33e5adbe5d196078431941cf0d6e9333af77ff (plain)
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
#include <stdio.h>

int foo(volatile *mem, int val, int c) {
  int oldval = __sync_fetch_and_add(mem, val);
  return oldval + c;
}

volatile int x = 0;
int main() {
  long long test = 0;
  int i;
  int y = foo(&x, 1, 2);
  printf("%d, %d\n", y, x);
  y = __sync_val_compare_and_swap(&x, 1, 2);
  printf("%d, %d\n", y, x);
  y = __sync_lock_test_and_set(&x, 1);
  printf("%d, %d\n", y, x);

  for (i = 0; i < 5; i++) {
    __sync_add_and_fetch(&test, 1);
    printf("test = %d\n", (int)test);
  }

  for (i = 0; i < 5; i++) {
    __sync_fetch_and_sub(&test,1);
    printf("test = %d\n", (int)test);
  }

  return 0;
}