]> git.saurik.com Git - apple/libpthread.git/blob - tests/cond_timed.c
9e4249d41baeb4ea108c947b8f15d24ce09b5a19
[apple/libpthread.git] / tests / cond_timed.c
1 #include <assert.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <sys/time.h>
10 #include <libkern/OSAtomic.h>
11 #include <dispatch/dispatch.h>
12
13 #include <darwintest.h>
14
15 #define NUM_THREADS 8
16
17 struct context {
18 pthread_cond_t cond;
19 pthread_mutex_t mutex;
20 long udelay;
21 long count;
22 };
23
24 static void *wait_thread(void *ptr) {
25 int res;
26 struct context *context = ptr;
27
28 bool loop = true;
29 while (loop) {
30 struct timespec ts;
31 struct timeval tv;
32 gettimeofday(&tv, NULL);
33 tv.tv_sec += (tv.tv_usec + context->udelay) / (__typeof(tv.tv_sec)) USEC_PER_SEC;
34 tv.tv_usec = (tv.tv_usec + context->udelay) % (__typeof(tv.tv_usec)) USEC_PER_SEC;
35 TIMEVAL_TO_TIMESPEC(&tv, &ts);
36
37 res = pthread_mutex_lock(&context->mutex);
38 if (res) {
39 fprintf(stderr, "[%ld] pthread_mutex_lock: %s\n", context->count, strerror(res));
40 abort();
41 }
42
43 if (context->count > 0) {
44 res = pthread_cond_timedwait(&context->cond, &context->mutex, &ts);
45 if (res != ETIMEDOUT) {
46 fprintf(stderr, "[%ld] pthread_cond_timedwait: %s\n", context->count, strerror(res));
47 abort();
48 }
49 --context->count;
50 } else {
51 loop = false;
52 }
53
54 res = pthread_mutex_unlock(&context->mutex);
55 if (res) {
56 fprintf(stderr, "[%ld] pthread_mutex_unlock: %s\n", context->count, strerror(res));
57 abort();
58 }
59 }
60
61 return NULL;
62 }
63
64 T_DECL(cond_timedwait_timeout, "pthread_cond_timedwait() timeout")
65 {
66 // This testcase launches 8 threads that all perform timed wait on the same
67 // conditional variable that is not being signaled in a loop. Ater the total
68 // of 8000 timeouts all threads finish and the testcase prints out the
69 // expected time (5[ms]*8000[timeouts]/8[threads]=5s) vs elapsed time.
70 struct context context = {
71 .cond = PTHREAD_COND_INITIALIZER,
72 .mutex = PTHREAD_MUTEX_INITIALIZER,
73 .udelay = 5000,
74 .count = 8000,
75 };
76
77 long uexpected = (context.udelay * context.count) / NUM_THREADS;
78 T_LOG("waittime expected: %ld us", uexpected);
79 struct timeval start, end;
80 gettimeofday(&start, NULL);
81
82 pthread_t p[NUM_THREADS];
83 for (int i = 0; i < NUM_THREADS; ++i) {
84 T_ASSERT_POSIX_ZERO(pthread_create(&p[i], NULL, wait_thread, &context),
85 "pthread_create");
86 }
87
88 usleep((useconds_t) uexpected);
89 bool loop = true;
90 while (loop) {
91 T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&context.mutex),
92 "pthread_mutex_lock");
93 if (context.count <= 0) {
94 loop = false;
95 }
96 T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&context.mutex),
97 "pthread_mutex_unlock");
98 }
99
100 for (int i = 0; i < NUM_THREADS; ++i) {
101 T_ASSERT_POSIX_ZERO(pthread_join(p[i], NULL), "pthread_join");
102 }
103
104 gettimeofday(&end, NULL);
105 uint64_t uelapsed =
106 ((uint64_t) end.tv_sec * USEC_PER_SEC + (uint64_t) end.tv_usec) -
107 ((uint64_t) start.tv_sec * USEC_PER_SEC + (uint64_t) start.tv_usec);
108 T_LOG("waittime actual: %llu us", uelapsed);
109 }