// 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
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
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 )
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) )
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;
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
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;
}
{
// 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
}
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
}
close (mgr->idx);
free (mgr->pool);
free (mgr->hash);
- free (mgr->latch);
+ free ((void *)mgr->latch);
free (mgr);
#else
FlushFileBuffers(mgr->idx);
CloseHandle(mgr->idx);
GlobalFree (mgr->pool);
GlobalFree (mgr->hash);
- GlobalFree (mgr->latch);
+ GlobalFree ((void *)mgr->latch);
GlobalFree (mgr);
#endif
}
#ifdef STANDALONE
+#ifndef unix
+double getCpuTime(int type)
+{
+FILETIME crtime[1];
+FILETIME xittime[1];
+FILETIME systime[1];
+FILETIME usrtime[1];
+SYSTEMTIME timeconv[1];
+double ans;
+
+ GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
+ memset (timeconv, 0, sizeof(SYSTEMTIME));
+
+ switch( type ) {
+ case 1:
+ FileTimeToSystemTime (usrtime, timeconv);
+ break;
+ case 2:
+ FileTimeToSystemTime (systime, timeconv);
+ break;
+ }
+
+ ans = (double)timeconv->wHour * 3600;
+ ans += (double)timeconv->wMinute * 60;
+ ans += (double)timeconv->wSecond;
+ ans += (double)timeconv->wMilliseconds / 1000;
+ return ans;
+}
+#else
+#include <sys/time.h>
+#include <sys/resource.h>
+
+double getCpuTime(int type)
+{
+struct rusage used[1];
+
+ getrusage(RUSAGE_SELF, used);
+ switch( type ) {
+ case 1:
+ return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
+
+ case 2:
+ return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
+ }
+
+ return 0;
+}
+#endif
+
void bt_latchaudit (BtDb *bt)
{
ushort idx, hashidx;
double real_time;
ThreadArg *args;
uint poolsize = 0;
+float elapsed;
int num = 0;
char key[1];
BtMgr *mgr;
time (stop);
real_time = 1000 * (*stop - *start);
#endif
- fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000);
+ elapsed = real_time / 1000;
+ fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+ elapsed = getCpuTime(1);
+ fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+ elapsed = getCpuTime(2);
+ fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+
bt_mgrclose (mgr);
}