]> pd.if.org Git - btree/blobdiff - threads2h.c
rework spinlock data structure to survive gcc -O3 optimization
[btree] / threads2h.c
index 4a28044811ef815c490558f5cf66af89d13c29aa..160308a9fcd1c8053e864b4a1ef7c7d1fa65aee4 100644 (file)
@@ -93,11 +93,11 @@ typedef enum{
 // 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
@@ -372,10 +372,10 @@ ushort prev;
   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
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        //  see if exclusive request is granted or pending
@@ -384,9 +384,9 @@ ushort prev;
                latch->share++;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 
        if( prev )
@@ -407,10 +407,10 @@ uint prev;
 
   do {
 #ifdef  unix
-       if( __sync_lock_test_and_set(&latch->mutex, 1) )
+       if( __sync_lock_test_and_set(latch->mutex, 1) )
                continue;
 #else
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                continue;
 #endif
        if( prev = !(latch->share | latch->exclusive) )
@@ -418,9 +418,9 @@ uint prev;
        else
                latch->pending = 1;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        if( prev )
                return;
@@ -441,10 +441,10 @@ int bt_spinwritetry(BtSpinLatch *latch)
 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
-       if( _InterlockedExchange8(&latch->mutex, 1) )
+       if( _InterlockedExchange8(latch->mutex, 1) )
                return 0;
 #endif
        //      take write access if all bits are clear
@@ -453,9 +453,9 @@ uint prev;
                latch->exclusive = 1;
 
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
        return prev;
 }
@@ -466,17 +466,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch)
 {
        //      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
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->exclusive = 0;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
@@ -485,17 +485,17 @@ void bt_spinreleasewrite(BtSpinLatch *latch)
 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
-       while( _InterlockedExchange8(&latch->mutex, 1) )
+       while( _InterlockedExchange8(latch->mutex, 1) )
                SwitchToThread();
 #endif
        latch->share--;
 #ifdef unix
-       __sync_lock_release (&latch->mutex);
+       __sync_lock_release (latch->mutex);
 #else
-       _InterlockedExchange8(&latch->mutex, 0);
+       _InterlockedExchange8(latch->mutex, 0);
 #endif
 }
 
@@ -783,7 +783,7 @@ uint slot;
        close (mgr->idx);
        free (mgr->pool);
        free (mgr->hash);
-       free (mgr->latch);
+       free ((void *)mgr->latch);
        free (mgr);
 #else
        FlushFileBuffers(mgr->idx);