forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef __STDATOMIC_H | ||
#define __STDATOMIC_H | ||
|
||
#define atomic_compare_exchange_weak(p, old, new) \ | ||
__builtin_compare_and_swap((p), (old), (new)) | ||
|
||
#define atomic_compare_exchange_strong(p, old, new) \ | ||
__builtin_compare_and_swap((p), (old), (new)) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "test.h" | ||
#include <stdatomic.h> | ||
#include <pthread.h> | ||
|
||
static int incr(int *p) { | ||
int oldval = *p; | ||
int newval; | ||
do { | ||
newval = oldval + 1; | ||
} while (!atomic_compare_exchange_weak(p, &oldval, newval)); | ||
return newval; | ||
} | ||
|
||
static int add(void *arg) { | ||
int *x = arg; | ||
for (int i = 0; i < 1000*1000; i++) | ||
incr(x); | ||
return 0; | ||
} | ||
|
||
static int add_millions(void) { | ||
int x = 0; | ||
|
||
pthread_t thr1; | ||
pthread_t thr2; | ||
|
||
pthread_create(&thr1, NULL, add, &x); | ||
pthread_create(&thr2, NULL, add, &x); | ||
|
||
for (int i = 0; i < 1000*1000; i++) | ||
incr(&x); | ||
|
||
pthread_join(thr1, NULL); | ||
pthread_join(thr2, NULL); | ||
return x; | ||
} | ||
|
||
int main() { | ||
ASSERT(3*1000*1000, add_millions()); | ||
|
||
printf("OK\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters