diff --git a/tests/pthread_mutex1.c b/tests/pthread_mutex1.c index cfad200..85a8d35 100644 --- a/tests/pthread_mutex1.c +++ b/tests/pthread_mutex1.c @@ -1,15 +1,60 @@ #define _GNU_SOURCE #include +#include +#include char *testcase_description = "Contended pthread mutex"; +#ifdef THREADS pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t pmutex = &mutex; +#else +static pthread_mutex_t *pmutex = NULL; + +/* + * Alloc a shared memory where it is placing a cross-process + * pthread mutex for verifying its scability of multi-process + */ +void testcase_prepare(unsigned long nr_tasks) +{ + int ret; + void *addr; + + addr = mmap(NULL, sizeof(pthread_mutex_t), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + assert(addr != MAP_FAILED); + + pmutex = (pthread_mutex_t *)addr; + + pthread_mutexattr_t attr; + ret = pthread_mutexattr_init(&attr); + assert(ret == 0); + + ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + assert(ret == 0); + + ret = pthread_mutex_init(pmutex, &attr); + assert(ret == 0); + + pthread_mutexattr_destroy(&attr); +} + +void testcase_cleanup(void) +{ + if (pmutex) { + pthread_mutex_destroy(pmutex); + munmap(pmutex, sizeof(pthread_mutex_t)); + } +} + +#endif void testcase(unsigned long long *iterations, unsigned long nr) { while (1) { - pthread_mutex_lock(&mutex); - pthread_mutex_unlock(&mutex); + pthread_mutex_lock(pmutex); + pthread_mutex_unlock(pmutex); (*iterations)++; }