pthread mutex tests

This commit is contained in:
Ali Mashtizadeh 2015-02-02 13:04:43 -08:00
parent 1ceea3c677
commit 73064ffdf6
2 changed files with 47 additions and 3 deletions

View File

@ -440,7 +440,7 @@ Syscall_ThreadSleep(uint64_t time)
uint64_t
Syscall_ThreadWait(uint64_t tid)
{
int status;
uint64_t status;
Thread *cur = Sched_Current();
/*

View File

@ -5,8 +5,8 @@
#include <string.h>
#include <pthread.h>
#include <stdbool.h>
#include <core/mutex.h>
pthread_mutex_t mtx;
void *
thread_simple(void *arg)
@ -16,9 +16,27 @@ thread_simple(void *arg)
return arg;
}
void *
thread_lock(void *arg)
{
int i;
int status;
for (i = 0; i < 100; i++) {
status = pthread_mutex_lock(&mtx);
assert(status == 0);
pthread_yield();
status = pthread_mutex_unlock(&mtx);
assert(status == 0);
}
return NULL;
}
int
main(int argc, const char *argv[])
{
int i;
int status;
pthread_t thr;
void *result;
@ -42,6 +60,32 @@ main(int argc, const char *argv[])
assert(result == (void *)1);
// Mutex test
printf("simple mutex lock test\n");
status = pthread_mutex_init(&mtx, NULL);
assert(status == 0);
status = pthread_mutex_lock(&mtx);
assert(status == 0);
status = pthread_mutex_unlock(&mtx);
assert(status == 0);
status = pthread_mutex_destroy(&mtx);
assert(status == 0);
// Mutex Contention Test
printf("contended mutex lock test\n");
pthread_mutex_init(&mtx, NULL);
status = pthread_create(&thr, NULL, thread_simple, (void *)1);
assert(status == 0);
for (i = 0; i < 100; i++) {
status = pthread_mutex_lock(&mtx);
assert(status == 0);
pthread_yield();
pthread_mutex_unlock(&mtx);
assert(status == 0);
}
status = pthread_join(thr, &result);
assert(status == 0);
status = pthread_mutex_destroy(&mtx);
assert(status == 0);
printf("Success!\n");