-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread_test.cc
More file actions
78 lines (60 loc) · 1.83 KB
/
pthread_test.cc
File metadata and controls
78 lines (60 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "check.h"
#include "kutrace_lib.h"
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
void *rdlockThread(void *arg)
{
int rc;
int count = 0;
printf("Entered thread, getting read lock with mp wait\n");
Retry:
printf("pthread_rwlock_tryrdlock: %p\n",pthread_rwlock_tryrdlock);
rc = pthread_rwlock_tryrdlock(&rwlock);
if (rc == EBUSY) {
if (count >= 10) {
printf("Retried too many times, failure!\n");
exit(EXIT_FAILURE);
}
++count;
printf("Could not get lock, do other work, then RETRY...\n");
sleep(1);
goto Retry;
}
compResults("pthread_rwlock_tryrdlock() 1\n", rc);
sleep(2);
printf("unlock the read lock\n");
rc = pthread_rwlock_unlock(&rwlock);
compResults("pthread_rwlock_unlock()\n", rc);
printf("Secondary thread complete\n");
return NULL;
}
int main(int argc, char **argv)
{
kutrace::mark_a("main");
int rc=0;
pthread_t thread;
printf("Enter test case - %s\n", argv[0]);
printf("Main, get the write lock\n");
rc = pthread_rwlock_wrlock(&rwlock);
compResults("pthread_rwlock_wrlock()\n", rc);
printf("Main, create the try read lock thread\n");
rc = pthread_create(&thread, NULL, rdlockThread, NULL);
compResults("pthread_create\n", rc);
printf("Main, wait a bit holding the write lock\n");
sleep(5);
printf("Main, Now unlock the write lock\n");
rc = pthread_rwlock_unlock(&rwlock);
compResults("pthread_rwlock_unlock()\n", rc);
printf("Main, wait for the thread to end\n");
rc = pthread_join(thread, NULL);
compResults("pthread_join\n", rc);
rc = pthread_rwlock_destroy(&rwlock);
compResults("pthread_rwlock_destroy()\n", rc);
printf("Main completed\n");
kutrace::mark_a("/main");
return 0;
}