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_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
43 T(pthread_mutex_init(a[0], &ma));
44 T(pthread_mutexattr_destroy(&ma));
45 E(sem_init(a[1], 0, 0));
46 T(pthread_create(&t, 0, relock, a));
48 E(clock_gettime(CLOCK_REALTIME, &ts));
49 ts.tv_nsec += 100*1000*1000;
50 if (ts.tv_nsec >= 1000*1000*1000) {
51 ts.tv_nsec -= 1000*1000*1000;
54 r = sem_timedwait(a[1],&ts);
56 if (errno != ETIMEDOUT)
57 t_error("sem_timedwait failed with unexpected error: %s\n", strerror(errno));
58 /* leave the deadlocked relock thread running */
61 T(pthread_join(t, &p));
62 T(pthread_mutex_destroy(a[0]));
67 static void *unlock(void *arg)
71 *(int*)a[1] = pthread_mutex_unlock(a[0]);
75 static int test_unlock(int mtype)
79 pthread_mutexattr_t ma;
85 T(pthread_mutexattr_init(&ma));
86 T(pthread_mutexattr_settype(&ma, mtype));
87 T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
88 T(pthread_mutex_init(a[0], &ma));
89 T(pthread_mutexattr_destroy(&ma));
90 T(pthread_create(&t, 0, unlock, a));
91 T(pthread_join(t, &p));
92 T(pthread_mutex_destroy(a[0]));
96 static int test_unlock_other(int mtype)
100 pthread_mutexattr_t ma;
106 T(pthread_mutexattr_init(&ma));
107 T(pthread_mutexattr_settype(&ma, mtype));
108 T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
109 T(pthread_mutex_init(a[0], &ma));
110 T(pthread_mutexattr_destroy(&ma));
111 T(pthread_mutex_lock(a[0]));
112 T(pthread_create(&t, 0, unlock, a));
113 T(pthread_join(t, &p));
114 T(pthread_mutex_unlock(a[0]));
115 T(pthread_mutex_destroy(a[0]));
119 static void test_mutexattr()
122 pthread_mutexattr_t a;
126 T(pthread_mutexattr_init(&a));
127 T(pthread_mutexattr_gettype(&a, &i));
128 if (i != PTHREAD_MUTEX_DEFAULT)
129 t_error("default mutex type is %d, wanted PTHREAD_MUTEX_DEFAULT (%d)\n", i, PTHREAD_MUTEX_DEFAULT);
130 T(pthread_mutexattr_settype(&a, PTHREAD_MUTEX_ERRORCHECK));
131 T(pthread_mutexattr_gettype(&a, &i));
132 if (i != PTHREAD_MUTEX_ERRORCHECK)
133 t_error("setting error check mutex type failed failed: got %d, wanted %d\n", i, PTHREAD_MUTEX_ERRORCHECK);
134 T(pthread_mutexattr_destroy(&a));
143 i = test_relock(PTHREAD_MUTEX_NORMAL);
145 t_error("PTHREAD_MUTEX_NORMAL relock did not deadlock, got %s\n", strerror(i));
146 i = test_relock(PTHREAD_MUTEX_ERRORCHECK);
148 t_error("PTHREAD_MUTEX_ERRORCHECK relock did not return EDEADLK, got %s\n", i==-1?"deadlock":strerror(i));
149 i = test_relock(PTHREAD_MUTEX_RECURSIVE);
151 t_error("PTHREAD_MUTEX_RECURSIVE relock did not succed, got %s\n", i==-1?"deadlock":strerror(i));
153 i = test_unlock(PTHREAD_MUTEX_ERRORCHECK);
155 t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
156 i = test_unlock(PTHREAD_MUTEX_RECURSIVE);
158 t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));
160 i = test_unlock_other(PTHREAD_MUTEX_ERRORCHECK);
162 t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
163 i = test_unlock_other(PTHREAD_MUTEX_RECURSIVE);
165 t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));