1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: bing.wei.liu REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test that pthread_cond_broadcast() 9 * shall unblock all threads currently blocked on the specified condition 10 * variable cond. 11 */ 12 13 #define _XOPEN_SOURCE 600 14 15 #include <pthread.h> 16 #include <stdio.h> 17 #include <unistd.h> 18 #include <stdlib.h> 19 #include "posixtest.h" 20 21 #define THREAD_NUM 3 22 23 struct testdata { 24 pthread_mutex_t mutex; 25 pthread_cond_t cond; 26 } td; 27 28 static int start_num; 29 static int waken_num; 30 31 static void *thr_func(void *arg) 32 { 33 int rc; 34 pthread_t self = pthread_self(); 35 36 if (pthread_mutex_lock(&td.mutex) != 0) { 37 fprintf(stderr, "[Thread 0x%p] failed to acquire the mutex\n", 38 (void *)self); 39 exit(PTS_UNRESOLVED); 40 } 41 start_num++; 42 fprintf(stderr, "[Thread 0x%p] started and locked the mutex\n", 43 (void *)self); 44 45 fprintf(stderr, "[Thread 0x%p] is waiting for the cond\n", 46 (void *)self); 47 rc = pthread_cond_wait(&td.cond, &td.mutex); 48 if (rc != 0) { 49 fprintf(stderr, "pthread_cond_wait return %d\n", rc); 50 exit(PTS_UNRESOLVED); 51 } 52 waken_num++; 53 fprintf(stderr, "[Thread 0x%p] was wakened and acquired the mutex " 54 "again\n", (void *)self); 55 56 if (pthread_mutex_unlock(&td.mutex) != 0) { 57 fprintf(stderr, "[Thread 0x%p] failed to release the mutex\n", 58 (void *)self); 59 exit(PTS_UNRESOLVED); 60 } 61 fprintf(stderr, "[Thread 0x%p] released the mutex\n", (void *)self); 62 return NULL; 63 } 64 65 int main(void) 66 { 67 int i, rc; 68 pthread_t thread[THREAD_NUM]; 69 70 if (pthread_mutex_init(&td.mutex, NULL) != 0) { 71 fprintf(stderr, "Fail to initialize mutex\n"); 72 return PTS_UNRESOLVED; 73 } 74 if (pthread_cond_init(&td.cond, NULL) != 0) { 75 fprintf(stderr, "Fail to initialize cond\n"); 76 return PTS_UNRESOLVED; 77 } 78 79 for (i = 0; i < THREAD_NUM; i++) { 80 if (pthread_create(&thread[i], NULL, thr_func, NULL) != 0) { 81 fprintf(stderr, "Fail to create thread[%d]\n", i); 82 return PTS_UNRESOLVED; 83 } 84 } 85 86 while (start_num < THREAD_NUM) 87 usleep(100); 88 89 /* 90 * Acquire the mutex to make sure that all waiters are currently 91 * blocked on pthread_cond_wait 92 */ 93 if (pthread_mutex_lock(&td.mutex) != 0) { 94 fprintf(stderr, "Main: Fail to acquire mutex\n"); 95 return PTS_UNRESOLVED; 96 } 97 if (pthread_mutex_unlock(&td.mutex) != 0) { 98 fprintf(stderr, "Main: Fail to release mutex\n"); 99 return PTS_UNRESOLVED; 100 } 101 102 /* broadcast and check if all waiters are wakened */ 103 fprintf(stderr, "[Main thread] broadcast the condition\n"); 104 rc = pthread_cond_broadcast(&td.cond); 105 if (rc != 0) { 106 fprintf(stderr, "[Main thread] failed to broadcast the " 107 "condition\n"); 108 return PTS_UNRESOLVED; 109 } 110 sleep(1); 111 if (waken_num < THREAD_NUM) { 112 fprintf(stderr, "[Main thread] Not all waiters were wakened\n"); 113 printf("Test FAILED\n"); 114 for (i = 0; i < THREAD_NUM; i++) 115 pthread_cancel(thread[i]); 116 117 exit(PTS_FAIL); 118 } 119 fprintf(stderr, "[Main thread] all waiters were wakened\n"); 120 121 /* join all secondary threads */ 122 for (i = 0; i < THREAD_NUM; i++) { 123 if (pthread_join(thread[i], NULL) != 0) { 124 fprintf(stderr, "Fail to join thread[%d]\n", i); 125 return PTS_UNRESOLVED; 126 } 127 } 128 printf("Test PASSED\n"); 129 return PTS_PASS; 130 } 131