1 /* testing pthread mutex behaviour with various attributes */
9 #define T(f) if ((r=(f))) t_error(#f " failed: %s\n", strerror(r))
10 #define E(f) if (f) t_error(#f " failed: %s\n", strerror(errno))
12 static void *relock(void *arg)
17 T(pthread_mutex_lock(a[0]));
19 *(int*)a[2] = pthread_mutex_lock(a[0]);
22 T(pthread_mutex_unlock(a[0]));
24 T(pthread_mutex_unlock(a[0]));
28 static int test_relock(int mtype)
33 pthread_mutexattr_t ma;
38 void *a[] = {&m,&s,&i};
40 T(pthread_mutexattr_init(&ma));
41 T(pthread_mutexattr_settype(&ma, mtype));
42 T(pthread_mutex_init(a[0], &ma));
43 T(pthread_mutexattr_destroy(&ma));
44 E(sem_init(a[1], 0, 0));
45 T(pthread_create(&t, 0, relock, a));
47 E(clock_gettime(CLOCK_REALTIME, &ts));
48 ts.tv_nsec += 100*1000*1000;
49 if (ts.tv_nsec >= 1000*1000*1000) {
50 ts.tv_nsec -= 1000*1000*1000;
53 r = sem_timedwait(a[1],&ts);
55 if (errno != ETIMEDOUT)
56 t_error("sem_timedwait failed with unexpected error: %s\n", strerror(errno));
57 /* leave the deadlocked relock thread running */
60 T(pthread_join(t, &p));
61 T(pthread_mutex_destroy(a[0]));
66 static void *unlock(void *arg)
70 *(int*)a[1] = pthread_mutex_unlock(a[0]);
74 static int test_unlock(int mtype)
78 pthread_mutexattr_t ma;
84 T(pthread_mutexattr_init(&ma));
85 T(pthread_mutexattr_settype(&ma, mtype));
86 T(pthread_mutex_init(a[0], &ma));
87 T(pthread_mutexattr_destroy(&ma));
88 T(pthread_create(&t, 0, unlock, a));
89 T(pthread_join(t, &p));
90 T(pthread_mutex_destroy(a[0]));
94 static int test_unlock_other(int mtype)
98 pthread_mutexattr_t ma;
104 T(pthread_mutexattr_init(&ma));
105 T(pthread_mutexattr_settype(&ma, mtype));
106 T(pthread_mutex_init(a[0], &ma));
107 T(pthread_mutexattr_destroy(&ma));
108 T(pthread_mutex_lock(a[0]));
109 T(pthread_create(&t, 0, unlock, a));
110 T(pthread_join(t, &p));
111 T(pthread_mutex_unlock(a[0]));
112 T(pthread_mutex_destroy(a[0]));
116 static void test_mutexattr()
119 pthread_mutexattr_t a;
123 T(pthread_mutexattr_init(&a));
124 T(pthread_mutexattr_gettype(&a, &i));
125 if (i != PTHREAD_MUTEX_DEFAULT)
126 t_error("default mutex type is %d, wanted PTHREAD_MUTEX_DEFAULT (%d)\n", i, PTHREAD_MUTEX_DEFAULT);
127 T(pthread_mutexattr_settype(&a, PTHREAD_MUTEX_ERRORCHECK));
128 T(pthread_mutexattr_gettype(&a, &i));
129 if (i != PTHREAD_MUTEX_ERRORCHECK)
130 t_error("setting error check mutex type failed failed: got %d, wanted %d\n", i, PTHREAD_MUTEX_ERRORCHECK);
131 T(pthread_mutexattr_destroy(&a));
140 i = test_relock(PTHREAD_MUTEX_NORMAL);
142 t_error("PTHREAD_MUTEX_NORMAL relock did not deadlock, got %s\n", strerror(i));
143 i = test_relock(PTHREAD_MUTEX_ERRORCHECK);
145 t_error("PTHREAD_MUTEX_ERRORCHECK relock did not return EDEADLK, got %s\n", i==-1?"deadlock":strerror(i));
146 i = test_relock(PTHREAD_MUTEX_RECURSIVE);
148 t_error("PTHREAD_MUTEX_RECURSIVE relock did not succed, got %s\n", i==-1?"deadlock":strerror(i));
150 i = test_unlock(PTHREAD_MUTEX_ERRORCHECK);
152 t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
153 i = test_unlock(PTHREAD_MUTEX_RECURSIVE);
155 t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));
157 i = test_unlock_other(PTHREAD_MUTEX_ERRORCHECK);
159 t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
160 i = test_unlock_other(PTHREAD_MUTEX_RECURSIVE);
162 t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));