Change spinlock to use gcc atomic function when available; enhanced spinlock tests.

This commit is contained in:
counterpoint
2014-09-30 16:08:51 +01:00
parent 32d66e6f84
commit 7dc9cf78f6
2 changed files with 159 additions and 33 deletions

View File

@ -30,6 +30,7 @@
#include <spinlock.h> #include <spinlock.h>
#include <atomic.h> #include <atomic.h>
#include <time.h>
/** /**
* Initialise a spinlock. * Initialise a spinlock.
@ -62,9 +63,15 @@ int spins = 0;
atomic_add(&(lock->waiting), 1); atomic_add(&(lock->waiting), 1);
#endif #endif
#ifdef __GNUC__
while (__sync_lock_test_and_set(&(lock->lock), 1))
while (lock->lock) {
#else
while (atomic_add(&(lock->lock), 1) != 0) while (atomic_add(&(lock->lock), 1) != 0)
{ {
atomic_add(&(lock->lock), -1); atomic_add(&(lock->lock), -1);
#endif
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
atomic_add(&(lock->spins), 1); atomic_add(&(lock->spins), 1);
spins++; spins++;
@ -92,11 +99,15 @@ int spins = 0;
int int
spinlock_acquire_nowait(SPINLOCK *lock) spinlock_acquire_nowait(SPINLOCK *lock)
{ {
#ifdef __GNUC__
if (__sync_lock_test_and_set(&(lock->lock), 1)) return FALSE;
#else
if (atomic_add(&(lock->lock), 1) != 0) if (atomic_add(&(lock->lock), 1) != 0)
{ {
atomic_add(&(lock->lock), -1); atomic_add(&(lock->lock), -1);
return FALSE; return FALSE;
} }
#endif
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
lock->acquired++; lock->acquired++;
lock->owner = THREAD_SHELF(); lock->owner = THREAD_SHELF();
@ -112,11 +123,16 @@ spinlock_acquire_nowait(SPINLOCK *lock)
void void
spinlock_release(SPINLOCK *lock) spinlock_release(SPINLOCK *lock)
{ {
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
if (lock->waiting > lock->max_waiting) if (lock->waiting > lock->max_waiting)
lock->max_waiting = lock->waiting; lock->max_waiting = lock->waiting;
#endif #endif
#ifdef __GNUC__
__sync_synchronize(); // Memory barrier.
lock->lock = 0;
#else
atomic_add(&(lock->lock), -1); atomic_add(&(lock->lock), -1);
#endif
} }
/** /**

View File

@ -105,12 +105,16 @@ test2()
{ {
SPINLOCK lck; SPINLOCK lck;
void *handle; void *handle;
struct timespec sleeptime;
sleeptime.tv_sec = 10;
sleeptime.tv_nsec = 0;
acquire_time = 0; acquire_time = 0;
spinlock_init(&lck); spinlock_init(&lck);
spinlock_acquire(&lck); spinlock_acquire(&lck);
handle = thread_start(test2_helper, (void *)&lck); handle = thread_start(test2_helper, (void *)&lck);
sleep(10); nanosleep(&sleeptime, NULL);
spinlock_release(&lck); spinlock_release(&lck);
thread_wait(handle); thread_wait(handle);
@ -122,12 +126,118 @@ void *handle;
return 0; return 0;
} }
main(int argc, char **argv) /**
* test3 spinlock_acquire tests process bound threads
*
* Check that spinlock correctly blocks all other threads whilst the spinlock
* is held.
*
* Start multiple threads that obtain spinlock and run process bound
*/
#define THREADS 5
#define ITERATIONS 25000
#define PROCESS_LOOP 10000
#define SECONDS 15
#define NANOTIME 100000
static int times_run, failures;
static volatile int active;
static int threadrun[THREADS];
static int nowait[THREADS];
static SPINLOCK lck;
static void
test3_helper(void *data)
{
// SPINLOCK *lck = (SPINLOCK *)data;
int i;
int n = *(int *)data;
struct timespec sleeptime;
time_t rawtime;
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = 1;
while (1) {
if (spinlock_acquire_nowait(&lck)) {
nowait[n]++;
}
else {
spinlock_acquire(&lck);
}
if (times_run++ > ITERATIONS) {
break;
}
threadrun[n]++;
/*
if (99 == (times_run % 100)) {
time ( &rawtime );
fprintf(stderr, "%s Done %d iterations of test, in thread %d.\n", asctime (localtime ( &rawtime )), times_run, n);
}
*/
if (0 != active) {
fprintf(stderr, "spinlock: test 3 failed with active non-zero after lock obtained.\n");
failures++;
}
else {
active = 1;
for (i=0; i<PROCESS_LOOP; i++);
}
active = 0;
spinlock_release(&lck);
for (i=0; i<(8*PROCESS_LOOP); i++);
// nanosleep(&sleeptime, NULL);
}
spinlock_release(&lck);
}
static int
test3()
{
// SPINLOCK lck;
void *handle[THREADS];
int i;
int tnum[THREADS];
time_t rawtime;
struct timespec sleeptime;
sleeptime.tv_sec = 20;
sleeptime.tv_nsec = NANOTIME;
times_run = 0;
active = 0;
failures = 0;
spinlock_init(&lck);
time ( &rawtime );
fprintf(stderr, "%s Starting %d threads.\n", asctime (localtime ( &rawtime )), THREADS);
for (i = 0; i<THREADS; i++) {
threadrun[i] = 0;
tnum[i] = i;
handle[i] = thread_start(test3_helper, &tnum[i]);
}
for (i = 0; i<THREADS; i++) {
fprintf(stderr, "spinlock_test 3 thread %d ran %d times, no wait %d times before waits.\n", i, threadrun[i], nowait[i]);
}
for (i = 0; i<THREADS; i++) {
time ( &rawtime );
fprintf(stderr, "%s spinlock_test 3 finished sleeps, about to wait for thread %d.\n", asctime (localtime ( &rawtime )), i);
thread_wait(handle[i]);
}
for (i = 0; i<THREADS; i++) {
fprintf(stderr, "spinlock_test 3 thread %d ran %d times, no wait %d times.\n", i, threadrun[i], nowait[i]);
}
time ( &rawtime );
fprintf(stderr, "%s spinlock_test 3 completed, %d failures.\n", asctime (localtime ( &rawtime )), failures);
return 0 == failures ? 0: 1;
}
int main(int argc, char **argv)
{ {
int result = 0; int result = 0;
result += test1(); result += test1();
result += test2(); result += test2();
result += test3();
exit(result); exit(result);
} }