Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions tests/pthread_mutex1.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
#define _GNU_SOURCE
#include <pthread.h>
#include <sys/mman.h>
#include <assert.h>

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)++;
}
Expand Down