main
test-branch
|
Last change
on this file since 1aaefd4 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
688 bytes
|
| Line | |
|---|
| 1 | //http://www.ibm.com/developerworks/java/library/j-jtp04186/index.html
|
|---|
| 2 | //Listing 2. A nonblocking counter using CAS
|
|---|
| 3 |
|
|---|
| 4 | #include <pthread.h>
|
|---|
| 5 |
|
|---|
| 6 | #define assert(e) { if(!(e)) { ERROR: goto ERROR; (void)0; } }
|
|---|
| 7 |
|
|---|
| 8 | void __VERIFIER_atomic_CAS(
|
|---|
| 9 | volatile unsigned *v,
|
|---|
| 10 | unsigned e,
|
|---|
| 11 | unsigned u,
|
|---|
| 12 | unsigned *r)
|
|---|
| 13 | {
|
|---|
| 14 | if(*v == e)
|
|---|
| 15 | {
|
|---|
| 16 | *v = u, *r = 1;
|
|---|
| 17 | }
|
|---|
| 18 | else
|
|---|
| 19 | {
|
|---|
| 20 | *r = 0;
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | volatile unsigned value;
|
|---|
| 25 |
|
|---|
| 26 | void* thr1(void* arg) {
|
|---|
| 27 | unsigned v,vn,casret;
|
|---|
| 28 |
|
|---|
| 29 | do {
|
|---|
| 30 | v = value;
|
|---|
| 31 |
|
|---|
| 32 | if(v == 0u-1) {
|
|---|
| 33 | return 0;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | vn = v + 1;
|
|---|
| 37 |
|
|---|
| 38 | __VERIFIER_atomic_CAS(&value,v,vn,&casret);
|
|---|
| 39 | }
|
|---|
| 40 | while (casret==0);
|
|---|
| 41 | assert(value > v);
|
|---|
| 42 |
|
|---|
| 43 | return 0;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | int main(){
|
|---|
| 47 | pthread_t t;
|
|---|
| 48 |
|
|---|
| 49 | while(1) { pthread_create(&t, 0, thr1, 0); }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.