lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* ex7 |
| 2 | * |
| 3 | * Test case that illustrates a timed wait on a condition variable. |
| 4 | */ |
| 5 | |
| 6 | #include <errno.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <pthread.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | /* Our event variable using a condition variable contruct. */ |
| 14 | typedef struct { |
| 15 | pthread_mutex_t mutex; |
| 16 | pthread_cond_t cond; |
| 17 | int flag; |
| 18 | } event_t; |
| 19 | |
| 20 | |
| 21 | /* Global event to signal main thread the timeout of the child thread. */ |
| 22 | event_t main_event; |
| 23 | |
| 24 | |
| 25 | static void * |
| 26 | test_thread (void *ms_param) |
| 27 | { |
| 28 | unsigned long status = 0; |
| 29 | event_t foo; |
| 30 | struct timespec timeout; |
| 31 | struct timeval now; |
| 32 | long ms = (long) ms_param; |
| 33 | |
| 34 | /* initialize cond var */ |
| 35 | pthread_cond_init(&foo.cond, NULL); |
| 36 | pthread_mutex_init(&foo.mutex, NULL); |
| 37 | foo.flag = 0; |
| 38 | |
| 39 | /* set the time out value */ |
| 40 | printf("waiting %ld ms ...\n", ms); |
| 41 | gettimeofday(&now, NULL); |
| 42 | timeout.tv_sec = now.tv_sec + ms/1000 + (now.tv_usec + (ms%1000)*1000)/1000000; |
| 43 | timeout.tv_nsec = ((now.tv_usec + (ms%1000)*1000) % 1000000) * 1000; |
| 44 | |
| 45 | /* Just use this to test the time out. The cond var is never signaled. */ |
| 46 | pthread_mutex_lock(&foo.mutex); |
| 47 | while (foo.flag == 0 && status != ETIMEDOUT) { |
| 48 | status = pthread_cond_timedwait(&foo.cond, &foo.mutex, &timeout); |
| 49 | } |
| 50 | pthread_mutex_unlock(&foo.mutex); |
| 51 | |
| 52 | /* post the main event */ |
| 53 | pthread_mutex_lock(&main_event.mutex); |
| 54 | main_event.flag = 1; |
| 55 | pthread_cond_signal(&main_event.cond); |
| 56 | pthread_mutex_unlock(&main_event.mutex); |
| 57 | |
| 58 | /* that's it, bye */ |
| 59 | return (void*) status; |
| 60 | } |
| 61 | |
| 62 | int |
| 63 | main (void) |
| 64 | { |
| 65 | unsigned long count; |
| 66 | struct timespec ts; |
| 67 | ts.tv_sec = 0; |
| 68 | ts.tv_nsec = 10 * 1000; |
| 69 | |
| 70 | setvbuf (stdout, NULL, _IONBF, 0); |
| 71 | |
| 72 | /* initialize main event cond var */ |
| 73 | pthread_cond_init(&main_event.cond, NULL); |
| 74 | pthread_mutex_init(&main_event.mutex, NULL); |
| 75 | main_event.flag = 0; |
| 76 | |
| 77 | for (count = 0; count < 20; ++count) |
| 78 | { |
| 79 | pthread_t thread; |
| 80 | int status; |
| 81 | |
| 82 | /* pass down the milli-second timeout in the void* param */ |
| 83 | status = pthread_create (&thread, NULL, test_thread, (void*) (count*100)); |
| 84 | if (status != 0) { |
| 85 | printf ("status = %d, count = %lu: %s\n", status, count, |
| 86 | strerror (errno)); |
| 87 | return 1; |
| 88 | } |
| 89 | else { |
| 90 | |
| 91 | /* wait for the event posted by the child thread */ |
| 92 | pthread_mutex_lock(&main_event.mutex); |
| 93 | while (main_event.flag == 0) { |
| 94 | pthread_cond_wait(&main_event.cond, &main_event.mutex); |
| 95 | } |
| 96 | main_event.flag = 0; |
| 97 | pthread_mutex_unlock(&main_event.mutex); |
| 98 | |
| 99 | printf ("count = %lu\n", count); |
| 100 | } |
| 101 | |
| 102 | nanosleep (&ts, NULL); |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |