1 // commit: 9d5251f72b627974bcf438501e07ad42c24d94be 2011-03-08
2 // disallow cpu time clocks in condattr
3 #include <pthread.h>
4 #include <time.h>
5 #include <string.h>
6 #include <errno.h>
7 #include "test.h"
8
9 #define T(r,f) if ((r=(f))) t_error(#f " failed: %s\n", strerror(r))
10
main(void)11 int main(void)
12 {
13 pthread_cond_t c;
14 pthread_condattr_t a;
15 pthread_mutex_t m;
16 clockid_t clk;
17 struct timespec ts;
18 void *p;
19 int r;
20
21 T(r,pthread_condattr_init(&a));
22 r = pthread_condattr_setclock(&a, CLOCK_PROCESS_CPUTIME_ID);
23 if (r != EINVAL)
24 t_error("pthread_condattr_setclock CLOCK_PROCESS_CPUTIME_ID should fail with EINVAL, got %s\n", strerror(r));
25 r = pthread_condattr_setclock(&a, CLOCK_THREAD_CPUTIME_ID);
26 if (r != EINVAL)
27 t_error("pthread_condattr_setclock CLOCK_THREAD_CPUTIME_ID should fail with EINVAL, got %s\n", strerror(r));
28 T(r,pthread_condattr_getclock(&a, &clk));
29 if (clk != CLOCK_REALTIME)
30 t_error("condattr default clock is %d, wanted CLOCK_REALTIME (%d)\n", (int)clk, (int)CLOCK_REALTIME);
31
32 T(r,pthread_cond_init(&c, &a));
33 T(r,pthread_mutex_init(&m, 0));
34 T(r,pthread_mutex_lock(&m));
35 r = clock_gettime(CLOCK_REALTIME, &ts);
36 if (r)
37 t_error("clock_gettime failed: %s\n", strerror(errno));
38 /* wait 10ms */
39 ts.tv_nsec += 10*1000*1000;
40 if (ts.tv_nsec >= 1000*1000*1000) {
41 ts.tv_nsec -= 1000*1000*1000;
42 ts.tv_sec += 1;
43 }
44 r = pthread_cond_timedwait(&c, &m, &ts);
45 if (r != ETIMEDOUT)
46 t_error("pthread_cond_timedwait did not timeout, returned %s\n", strerror(r));
47 T(r,pthread_mutex_unlock(&m));
48
49 T(r,pthread_mutex_destroy(&m));
50 T(r,pthread_cond_destroy(&c));
51 T(r,pthread_condattr_destroy(&a));
52 return t_status;
53 }
54