Skip to content

Instantly share code, notes, and snippets.

@candeira
Last active August 29, 2015 14:14

Revisions

  1. candeira revised this gist Feb 3, 2015. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -9,14 +9,13 @@

    // and the wrap-a-block version

    #define SPINLOCKME(lock, block) { do { \
    #define SPINLOCKME(lock, statement_or_block) \
    do { \
    lock_p * _mylock = &(lock); \
    while (*_mylock) || ! __sync_bool_compare_and_swap(_mylock , 0 , 1)); \
    block; \
    statement_or_block; \
    *_mylock = 0; \
    } while (0)
    }
    } while(0)

    // for reference, my second attempt:

  2. candeira revised this gist Feb 3, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,21 @@
    // macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    // third version, thanks to kragen for explains
    // third version, thanks to kragen for explains of expression/statement and do/while protective coding

    #define SPINLOCK(lock) do { while ((lock) || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while 0
    #define SPINLOCK(lock) do { while ((lock) || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while (0)

    #define SPINUNLOCK(lock) (lock) = 0;

    // still workin progress
    // and the wrap-a-block version

    #define SPINLOCKME(lock, block) { do { \
    do { \
    lock_p * _mylock = &(lock); \
    while (*_mylock) || ! __sync_bool_compare_and_swap(_mylock , 0 , 1)); \
    block; \
    *_mylock = 0; \
    } while 0
    } while (0)
    }

    // for reference, my second attempt:
  3. candeira revised this gist Feb 3, 2015. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,13 @@

    // still workin progress

    #define SPINLOCKME(lock, block) { \
    do { (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); \
    block; \
    lock = 0; \
    #define SPINLOCKME(lock, block) { do { \
    do { \
    lock_p * _mylock = &(lock); \
    while (*_mylock) || ! __sync_bool_compare_and_swap(_mylock , 0 , 1)); \
    block; \
    *_mylock = 0; \
    } while 0
    }

    // for reference, my second attempt:
  4. candeira revised this gist Feb 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@

    // third version, thanks to kragen for explains

    #define SPINLOCK(lock) do { while (lock || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while 0
    #define SPINLOCK(lock) do { while ((lock) || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while 0

    #define SPINUNLOCK(lock) (lock) = 0;

  5. candeira revised this gist Feb 3, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    // macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    // third version, thanks to kragen for explains

    #define SPINLOCK(lock) do { while (lock || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while 0

    #define SPINUNLOCK(lock) (lock) = 0;
  6. candeira revised this gist Feb 3, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@

    #define SPINUNLOCK(lock) (lock) = 0;

    // still workin progress

    #define SPINLOCKME(lock, block) { \
    do { (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); \
  7. candeira revised this gist Feb 3, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,19 @@
    // macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    #define SPINLOCK(lock) do { while (lock || ! __sync_bool_compare_and_swap(&(lock) , 0 , 1));} while 0

    #define SPINUNLOCK(lock) (lock) = 0;


    #define SPINLOCKME(lock, block) { \
    do { (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); \
    block; \
    lock = 0; \
    }

    // for reference, my second attempt:

    #define SPINLOCK(lock) while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1));

    #define SPINUNLOCK(lock) lock = 0;
  8. candeira revised this gist Feb 3, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    // macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    #define SPINLOCK(lock) (while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1));)
    #define SPINLOCK(lock) while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1));

    #define SPINUNLOCK(lock) (lock = 0;)
    #define SPINUNLOCK(lock) lock = 0;

    //

  9. candeira revised this gist Feb 2, 2015. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,17 @@
    // attempting to macro-ize gallir's spinlock code from:
    // macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    #define SPINLOCK(lock) (while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1));)

    #define SPINUNLOCK(lock) (lock = 0;)

    //

    // for reference, my first attempt: will probably not work because of whitespace in code, but first attempt at a macro ever.

    #define SPINLOCKME(lock, code) { \
    while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); \
    code; \
    lock = 0; \
    }

    // will probably not work because of whitespace in code, but first attempt at a macro ever.
  10. candeira revised this gist Feb 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    #define SPINLOCKME(lock, code) { \
    while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); // spin \
    while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); \
    code; \
    lock = 0; \
    }
  11. candeira created this gist Feb 2, 2015.
    10 changes: 10 additions & 0 deletions spinlock-macro.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    // attempting to macro-ize gallir's spinlock code from:
    // http://stackoverflow.com/questions/6704252/atomic-memcpy-suggestion/28286503#28286503

    #define SPINLOCKME(lock, code) { \
    while (lock || ! __sync_bool_compare_and_swap(&lock , 0 , 1)); // spin \
    code; \
    lock = 0; \
    }

    // will probably not work because of whitespace in code, but first attempt at a macro ever.