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:
1.0 KB
|
| Line | |
|---|
| 1 | //Simple test_and_set lock with exponential backoff
|
|---|
| 2 | //
|
|---|
| 3 | //From Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors, 1991 (Fig. 1).
|
|---|
| 4 | //Also available as pseudo-code here: http://www.cs.rochester.edu/research/synchronization/pseudocode/ss.html#tas
|
|---|
| 5 |
|
|---|
| 6 | #include <pthread.h>
|
|---|
| 7 |
|
|---|
| 8 | #define unlocked 0
|
|---|
| 9 | #define locked 1
|
|---|
| 10 | volatile int lock = unlocked;
|
|---|
| 11 |
|
|---|
| 12 | #define assert(e) { if(!(e)) { ERROR: goto ERROR; (void)0; } }
|
|---|
| 13 |
|
|---|
| 14 | void __VERIFIER_atomic_TAS(
|
|---|
| 15 | volatile int *v,
|
|---|
| 16 | volatile int *o)
|
|---|
| 17 | {
|
|---|
| 18 | *o = *v;
|
|---|
| 19 | *v = 1;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | inline void acquire_lock(){
|
|---|
| 23 | int delay;
|
|---|
| 24 | int cond;
|
|---|
| 25 |
|
|---|
| 26 | delay = 1;
|
|---|
| 27 | __VERIFIER_atomic_TAS(&lock,&cond);
|
|---|
| 28 | while(cond == locked){
|
|---|
| 29 | //pause(delay);
|
|---|
| 30 | if(delay*2 > delay)
|
|---|
| 31 | delay *= 2;
|
|---|
| 32 | __VERIFIER_atomic_TAS(&lock,&cond);
|
|---|
| 33 | }
|
|---|
| 34 | assert(cond != lock);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | inline void release_lock(){
|
|---|
| 38 | assert(lock != unlocked);
|
|---|
| 39 | lock = unlocked;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int c = 0;
|
|---|
| 43 | void* thr1(void *arg){
|
|---|
| 44 | while(1){
|
|---|
| 45 | acquire_lock();
|
|---|
| 46 | c++; assert(c == 1); c--;
|
|---|
| 47 | release_lock();
|
|---|
| 48 | }
|
|---|
| 49 | return 0;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main(){
|
|---|
| 53 | pthread_t t;
|
|---|
| 54 |
|
|---|
| 55 | while(1) { pthread_create(&t, 0, thr1, 0); }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.