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 * Upon successful completion, a value of zero shall be returned. 10 */ 11 12#define _XOPEN_SOURCE 600 13 14#include <pthread.h> 15#include <stdio.h> 16#include <unistd.h> 17#include <stdlib.h> 18#include <errno.h> 19#include "posixtest.h" 20 21#define THREAD_NUM 5 22 23struct testdata 24{ 25 pthread_mutex_t mutex; 26 pthread_cond_t cond; 27} td; 28 29int start_num = 0; 30int waken_num = 0; 31 32void *thr_func(void *arg) 33{ 34 int rc; 35 pthread_t self = pthread_self(); 36 37 if (pthread_mutex_lock(&td.mutex) != 0) { 38 fprintf(stderr,"[Thread 0x%p] failed to acquire the mutex\n", (void*)self); 39 exit(PTS_UNRESOLVED); 40 } 41 fprintf(stderr,"[Thread 0x%p] started and locked the mutex\n", (void*)self); 42 start_num ++; 43 44 fprintf(stderr,"[Thread 0x%p] is waiting for the cond\n", (void*)self); 45 rc = pthread_cond_wait(&td.cond, &td.mutex); 46 if(rc != 0) { 47 fprintf(stderr,"pthread_cond_wait return %d\n", rc); 48 exit(PTS_UNRESOLVED); 49 } 50 fprintf(stderr,"[Thread 0x%p] was wakened\n", (void*)self); 51 waken_num ++; 52 53 if (pthread_mutex_unlock(&td.mutex) != 0) { 54 fprintf(stderr,"[Thread 0x%p] failed to release the mutex\n", (void*)self); 55 exit(PTS_UNRESOLVED); 56 } 57 return NULL; 58} 59 60int main() 61{ 62 int i, rc; 63 pthread_t thread[THREAD_NUM]; 64 65 if (pthread_mutex_init(&td.mutex, NULL) != 0) { 66 fprintf(stderr,"Fail to initialize mutex\n"); 67 return PTS_UNRESOLVED; 68 } 69 if (pthread_cond_init(&td.cond, NULL) != 0) { 70 fprintf(stderr,"Fail to initialize cond\n"); 71 return PTS_UNRESOLVED; 72 } 73 74 for (i=0; i<THREAD_NUM; i++) { /* create THREAD_NUM threads */ 75 if (pthread_create(&thread[i], NULL, thr_func, NULL) != 0) { 76 fprintf(stderr,"Fail to create thread[%d]\n", i); 77 return PTS_UNRESOLVED; 78 } 79 } 80 while (start_num < THREAD_NUM) /* waiting for all threads started */ 81 usleep(100); 82 83 /* Acquire the mutex to make sure that all waiters are currently 84 blocked on pthread_cond_wait */ 85 if (pthread_mutex_lock(&td.mutex) != 0) { 86 fprintf(stderr,"Main: Fail to acquire mutex\n"); 87 return PTS_UNRESOLVED; 88 } 89 if (pthread_mutex_unlock(&td.mutex) != 0) { 90 fprintf(stderr,"Main: Fail to release mutex\n"); 91 return PTS_UNRESOLVED; 92 } 93 sleep(1); 94 95 /* broadcast the condition to wake up all waiters */ 96 fprintf(stderr,"[Main thread] broadcast the condition\n"); 97 rc = pthread_cond_broadcast(&td.cond); 98 if (rc != 0) { 99 if (rc == EINVAL) { 100 fprintf(stderr,"pthread_cond_broadcast returns EINVAL\n"); 101 return PTS_UNRESOLVED; 102 } 103 else if (rc != 0) { 104 fprintf(stderr,"pthread_cond_broadcast returns %d\n",rc); 105 printf("Test FAILED\n"); 106 return PTS_FAIL; 107 } 108 } 109 fprintf(stderr,"[Main thread] pthread_cond_broadcast() returned 0\n"); 110 sleep(1); 111 if (waken_num < THREAD_NUM){ 112 fprintf(stderr,"[Main thread] Not all waiters were wakened\n"); 113 for (i=0; i<THREAD_NUM; i++) { 114 pthread_cancel(thread[i]); 115 } 116 return PTS_UNRESOLVED; 117 } 118 fprintf(stderr,"[Main thread] all waiters were wakened\n"); 119 120 /* join all secondary threads */ 121 for (i=0; i<THREAD_NUM; i++) { 122 if (pthread_join(thread[i], NULL) != 0) { 123 fprintf(stderr,"Fail to join thread[%d]\n", i); 124 return PTS_UNRESOLVED; 125 } 126 } 127 printf("Test PASSED\n"); 128 return PTS_PASS; 129} 130