]> pd.if.org Git - btree/commitdiff
rework spinlock data structure to survive gcc -O3 optimization
authorunknown <karl@E04.petzent.com>
Wed, 19 Feb 2014 22:50:58 +0000 (14:50 -0800)
committerunknown <karl@E04.petzent.com>
Wed, 19 Feb 2014 22:50:58 +0000 (14:50 -0800)
threads2h.c
threads2i.c
threads2j.c

index 4a28044811ef815c490558f5cf66af89d13c29aa..160308a9fcd1c8053e864b4a1ef7c7d1fa65aee4 100644 (file)
@@ -93,11 +93,11 @@ typedef enum{
 // share is count of read accessors
 // grant write lock when share == 0
 
 // share is count of read accessors
 // grant write lock when share == 0
 
-typedef struct {
-       volatile unsigned char mutex;
-       volatile unsigned char exclusive:1;
-       volatile unsigned char pending:1;
-       volatile ushort share;
+volatile typedef struct {
+       unsigned char mutex[1];
+       unsigned char exclusive:1;
+       unsigned char pending:1;
+       ushort share;
 } BtSpinLatch;
 
 //  hash table entries
 } BtSpinLatch;
 
 //  hash table entries
@@ -372,10 +372,10 @@ ushort prev;
   do {
        //      obtain latch mutex
 #ifdef unix
   do {
        //      obtain latch mutex
 #ifdef unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                continue;
 #else
                continue;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        //  see if exclusive request is granted or pending
                continue;
 #endif
        //  see if exclusive request is granted or pending
@@ -384,9 +384,9 @@ ushort prev;
                latch->share++;
 
 #ifdef unix
                latch->share++;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 
        if( prev )
 #endif
 
        if( prev )
@@ -407,10 +407,10 @@ uint prev;
 
   do {
 #ifdef  unix
 
   do {
 #ifdef  unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                continue;
 #else
                continue;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        if( prev = !(latch->share | latch->exclusive) )
                continue;
 #endif
        if( prev = !(latch->share | latch->exclusive) )
@@ -418,9 +418,9 @@ uint prev;
        else
                latch->pending = 1;
 #ifdef unix
        else
                latch->pending = 1;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        if( prev )
                return;
 #endif
        if( prev )
                return;
@@ -441,10 +441,10 @@ int bt_spinwritetry(BtSpinLatch *latch)
 uint prev;
 
 #ifdef unix
 uint prev;
 
 #ifdef unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                return 0;
 #else
                return 0;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                return 0;
 #endif
        //      take write access if all bits are clear
                return 0;
 #endif
        //      take write access if all bits are clear
@@ -453,9 +453,9 @@ uint prev;
                latch->exclusive = 1;
 
 #ifdef unix
                latch->exclusive = 1;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        return prev;
 }
 #endif
        return prev;
 }
@@ -466,17 +466,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch)
 {
        //      obtain latch mutex
 #ifdef unix
 {
        //      obtain latch mutex
 #ifdef unix
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 #else
                sched_yield();
 #else
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->exclusive = 0;
 #ifdef unix
                SwitchToThread();
 #endif
        latch->exclusive = 0;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
 #endif
 }
 
@@ -485,17 +485,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch)
 void bt_spinreleaseread(BtSpinLatch *latch)
 {
 #ifdef unix
 void bt_spinreleaseread(BtSpinLatch *latch)
 {
 #ifdef unix
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 #else
                sched_yield();
 #else
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->share--;
 #ifdef unix
                SwitchToThread();
 #endif
        latch->share--;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
 #endif
 }
 
@@ -783,7 +783,7 @@ uint slot;
        close (mgr->idx);
        free (mgr->pool);
        free (mgr->hash);
        close (mgr->idx);
        free (mgr->pool);
        free (mgr->hash);
-       free (mgr->latch);
+       free ((void *)mgr->latch);
        free (mgr);
 #else
        FlushFileBuffers(mgr->idx);
        free (mgr);
 #else
        FlushFileBuffers(mgr->idx);
index c425d1813601f9c3c0f2eef029d67b93e9104ac4..d8ac6be9663ac2ca6825e8ddc5ff8e58570146eb 100644 (file)
@@ -93,11 +93,11 @@ typedef enum{
 // share is count of read accessors
 // grant write lock when share == 0
 
 // share is count of read accessors
 // grant write lock when share == 0
 
-typedef struct {
-       volatile unsigned char mutex;
-       volatile unsigned char exclusive:1;
-       volatile unsigned char pending:1;
-       volatile ushort share;
+volatile typedef struct {
+       unsigned char mutex[1];
+       unsigned char exclusive:1;
+       unsigned char pending:1;
+       ushort share;
 } BtSpinLatch;
 
 //  hash table entries
 } BtSpinLatch;
 
 //  hash table entries
@@ -162,10 +162,9 @@ typedef struct BtPage_ {
        uint min;                                       // next key offset
        unsigned char bits:7;           // page size in bits
        unsigned char free:1;           // page is on free chain
        uint min;                                       // next key offset
        unsigned char bits:7;           // page size in bits
        unsigned char free:1;           // page is on free chain
-       unsigned char lvl:5;            // level of page
+       unsigned char lvl:6;            // level of page
        unsigned char kill:1;           // page is being deleted
        unsigned char dirty:1;          // page has deleted keys
        unsigned char kill:1;           // page is being deleted
        unsigned char dirty:1;          // page has deleted keys
-       unsigned char posted:1;         // page fence is posted
        unsigned char right[BtId];      // page number to right
 } *BtPage;
 
        unsigned char right[BtId];      // page number to right
 } *BtPage;
 
@@ -366,10 +365,10 @@ ushort prev;
   do {
        //      obtain latch mutex
 #ifdef unix
   do {
        //      obtain latch mutex
 #ifdef unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                continue;
 #else
                continue;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        //  see if exclusive request is granted or pending
                continue;
 #endif
        //  see if exclusive request is granted or pending
@@ -378,9 +377,9 @@ ushort prev;
                latch->share++;
 
 #ifdef unix
                latch->share++;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       *latch->mutex = 0;
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 
        if( prev )
 #endif
 
        if( prev )
@@ -401,10 +400,10 @@ uint prev;
 
   do {
 #ifdef  unix
 
   do {
 #ifdef  unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                continue;
 #else
                continue;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        if( prev = !(latch->share | latch->exclusive) )
                continue;
 #endif
        if( prev = !(latch->share | latch->exclusive) )
@@ -412,9 +411,9 @@ uint prev;
        else
                latch->pending = 1;
 #ifdef unix
        else
                latch->pending = 1;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       *latch->mutex = 0;
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        if( prev )
                return;
 #endif
        if( prev )
                return;
@@ -435,10 +434,10 @@ int bt_spinwritetry(BtSpinLatch *latch)
 uint prev;
 
 #ifdef unix
 uint prev;
 
 #ifdef unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                return 0;
 #else
                return 0;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                return 0;
 #endif
        //      take write access if all bits are clear
                return 0;
 #endif
        //      take write access if all bits are clear
@@ -447,9 +446,9 @@ uint prev;
                latch->exclusive = 1;
 
 #ifdef unix
                latch->exclusive = 1;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       *latch->mutex = 0;
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        return prev;
 }
 #endif
        return prev;
 }
@@ -459,17 +458,17 @@ uint prev;
 void bt_spinreleasewrite(BtSpinLatch *latch)
 {
 #ifdef unix
 void bt_spinreleasewrite(BtSpinLatch *latch)
 {
 #ifdef unix
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 #else
                sched_yield();
 #else
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->exclusive = 0;
 #ifdef unix
                SwitchToThread();
 #endif
        latch->exclusive = 0;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       *latch->mutex = 0;
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
 #endif
 }
 
@@ -478,17 +477,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch)
 void bt_spinreleaseread(BtSpinLatch *latch)
 {
 #ifdef unix
 void bt_spinreleaseread(BtSpinLatch *latch)
 {
 #ifdef unix
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 #else
                sched_yield();
 #else
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->share--;
 #ifdef unix
                SwitchToThread();
 #endif
        latch->share--;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       *latch->mutex = 0;
 #else
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
 #endif
 }
 
@@ -697,7 +696,7 @@ uint slot;
        close (mgr->idx);
        free (mgr->pool);
        free (mgr->hash);
        close (mgr->idx);
        free (mgr->pool);
        free (mgr->hash);
-       free (mgr->latch);
+       free ((void *)mgr->latch);
        free (mgr);
 #else
        FlushFileBuffers(mgr->idx);
        free (mgr);
 #else
        FlushFileBuffers(mgr->idx);
index a1081b33ed78e104815d37e7285c4d68e0c79163..27212ef96602fb8a18bbd29d04117f34d209a106 100644 (file)
@@ -101,15 +101,15 @@ enum {
 // share is count of read accessors
 // grant write lock when share == 0
 
 // share is count of read accessors
 // grant write lock when share == 0
 
-typedef struct {
-       volatile unsigned char mutex;           // 1 = busy
-       volatile unsigned char write:1;         // 1 = exclusive
-       volatile unsigned char readwait:1;      // readers are waiting
-       volatile unsigned char writewait:1;     // writers are waiting
-       volatile unsigned char filler:5;
-       volatile ushort share;                          // count of readers holding locks
-       volatile ushort rcnt;                           // count of waiting readers
-       volatile ushort wcnt;                           // count of waiting writers
+volatile typedef struct {
+       unsigned char mutex[1];         // 1 = busy
+       unsigned char write:1;          // 1 = exclusive
+       unsigned char readwait:1;       // readers are waiting
+       unsigned char writewait:1;      // writers are waiting
+       unsigned char filler:5;
+       ushort share;                           // count of readers holding locks
+       ushort rcnt;                            // count of waiting readers
+       ushort wcnt;                            // count of waiting writers
 } BtLatch;
 
 //     Define the length of the page and key pointers
 } BtLatch;
 
 //     Define the length of the page and key pointers
@@ -153,10 +153,9 @@ typedef struct BtPage_ {
        uint min;                                       // next key offset
        unsigned char bits:7;           // page size in bits
        unsigned char free:1;           // page is on free list
        uint min;                                       // next key offset
        unsigned char bits:7;           // page size in bits
        unsigned char free:1;           // page is on free list
-       unsigned char lvl:5;            // level of page
+       unsigned char lvl:6;            // level of page
        unsigned char kill:1;           // page is being deleted
        unsigned char dirty:1;          // page has deleted keys
        unsigned char kill:1;           // page is being deleted
        unsigned char dirty:1;          // page has deleted keys
-       unsigned char posted:1;         // page fence has posted
        unsigned char right[BtId];      // page number to right
 } *BtPage;
 
        unsigned char right[BtId];      // page number to right
 } *BtPage;
 
@@ -385,7 +384,7 @@ uint prev;
 
   while( 1 ) {
        //      obtain latch mutex
 
   while( 1 ) {
        //      obtain latch mutex
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 
        if( decr )
                sched_yield();
 
        if( decr )
@@ -398,7 +397,7 @@ uint prev;
                latch->readwait = 1;
                latch->rcnt++;
                prev = *(uint *)latch & ~1;
                latch->readwait = 1;
                latch->rcnt++;
                prev = *(uint *)latch & ~1;
-               __sync_lock_release (&latch->mutex);
+               __sync_lock_release (latch->mutex);
                sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueRd );
                decr = 1;
                continue;
                sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueRd );
                decr = 1;
                continue;
@@ -409,7 +408,7 @@ uint prev;
 
        latch->readwait = 0;
        latch->share++;
 
        latch->readwait = 0;
        latch->share++;
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
        return;
   }
 }
        return;
   }
 }
@@ -426,7 +425,7 @@ uint prev;
 
   while( 1 ) {
        //      obtain latch mutex
 
   while( 1 ) {
        //      obtain latch mutex
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 
        if( decr )
                sched_yield();
 
        if( decr )
@@ -438,7 +437,7 @@ uint prev;
                latch->writewait = 1;
                latch->wcnt++;
                prev = *(uint *)latch & ~1;
                latch->writewait = 1;
                latch->wcnt++;
                prev = *(uint *)latch & ~1;
-               __sync_lock_release (&latch->mutex);
+               __sync_lock_release (latch->mutex);
                sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueWr );
                decr = 1;
                continue;
                sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueWr );
                decr = 1;
                continue;
@@ -451,7 +450,7 @@ uint prev;
                latch->writewait = 0;
 
        latch->write = 1;
                latch->writewait = 0;
 
        latch->write = 1;
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
        return;
   }
 }
        return;
   }
 }
@@ -468,7 +467,7 @@ int ans;
        //      try for mutex,
        //      abandon request if not taken
 
        //      try for mutex,
        //      abandon request if not taken
 
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                return 0;
 
        //      see if write mode is available
                return 0;
 
        //      see if write mode is available
@@ -480,7 +479,7 @@ int ans;
 
        // release latch mutex
 
 
        // release latch mutex
 
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
        return ans;
 }
 
        return ans;
 }
 
@@ -493,7 +492,7 @@ void bt_spinreleasewrite(BtLatch *latch, int private)
 
        //      obtain latch mutex
 
 
        //      obtain latch mutex
 
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 
        latch->write = 0;
                sched_yield();
 
        latch->write = 0;
@@ -510,7 +509,7 @@ void bt_spinreleasewrite(BtLatch *latch, int private)
        // release latch mutex
 
 wakexit:
        // release latch mutex
 
 wakexit:
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 }
 
 //     decrement reader count
 }
 
 //     decrement reader count
@@ -522,19 +521,19 @@ void bt_spinreleaseread(BtLatch *latch, int private)
 
        //      obtain latch mutex
 
 
        //      obtain latch mutex
 
-       while( __sync_lock_test_and_set(&latch->mutex, 1) )
+       while( __sync_lock_test_and_set(latch->mutex, 1) )
                sched_yield();
 
        latch->share--;
 
                sched_yield();
 
        latch->share--;
 
-       // wake waiting writers
+       // wake one waiting writer
 
        if( !latch->share && latch->wcnt )
                sys_futex( (uint *)latch, FUTEX_WAKE_BITSET | private, 1, NULL, NULL, QueWr );
 
        // release latch mutex
 
 
        if( !latch->share && latch->wcnt )
                sys_futex( (uint *)latch, FUTEX_WAKE_BITSET | private, 1, NULL, NULL, QueWr );
 
        // release latch mutex
 
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 }
 
 //     link latch table entry into latch hash table
 }
 
 //     link latch table entry into latch hash table