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 * When each thread unblocked as a result of pthread_cond_signal() 10 * returns from its call to pthread_cond_timedwait(), the thread shall 11 * own the mutex with which it called pthread_cond_timedwait(). 12 */ 13 14#define _XOPEN_SOURCE 600 15 16#include <pthread.h> 17#include <stdio.h> 18#include <stdlib.h> 19#include <unistd.h> 20#include <sys/time.h> 21#include <errno.h> 22#include "posixtest.h" 23 24#define THREAD_NUM 3 25#define TIMEOUT THREAD_NUM * 2 26 27struct testdata 28{ 29 pthread_mutex_t mutex; 30 pthread_cond_t cond; 31} td; 32 33int start_num = 0; 34int waken_num = 0; 35 36void *thr_func(void *arg) 37{ 38 int rc; 39 struct timespec timeout; 40 struct timeval curtime; 41 pthread_t self = pthread_self(); 42 43 if (pthread_mutex_lock(&td.mutex) != 0) { 44 fprintf(stderr,"[Thread 0x%p] failed to acquire the mutex\n", (void*)self); 45 exit(PTS_UNRESOLVED); 46 } 47 fprintf(stderr,"[Thread 0x%p] started and locked the mutex\n", (void*)self); 48 start_num ++; 49 50 if (gettimeofday(&curtime, NULL) !=0 ) { 51 fprintf(stderr,"Fail to get current time\n"); 52 exit(PTS_UNRESOLVED); 53 } 54 timeout.tv_sec = curtime.tv_sec + TIMEOUT; 55 timeout.tv_nsec = curtime.tv_usec * 1000; 56 57 fprintf(stderr,"[Thread 0x%p] is waiting for the cond for at most %d secs\n", 58 (void*)self, TIMEOUT); 59 rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout); 60 if(rc != 0) { 61 fprintf(stderr,"[Thread 0x%p] pthread_cond_wait returned %d\n", 62 (void*)self, rc); 63 exit(PTS_UNRESOLVED); 64 } 65 66 if (pthread_mutex_trylock(&td.mutex) == 0) { 67 fprintf(stderr,"[Thread 0x%p] should not be able to lock the mutex again\n", 68 (void*)self); 69 printf("Test FAILED\n"); 70 exit(PTS_FAIL); 71 } 72 fprintf(stderr,"[Thread 0x%p] was wakened and acquired the mutex again\n", (void*)self); 73 waken_num ++; 74 75 if (pthread_mutex_unlock(&td.mutex) != 0) { 76 fprintf(stderr,"[Thread 0x%p] failed to release the mutex\n", (void*)self); 77 printf("Test FAILED\n"); 78 exit(PTS_FAIL); 79 } 80 fprintf(stderr,"[Thread 0x%p] released the mutex\n", (void*)self); 81 return NULL; 82} 83 84int main() 85{ 86 int i, rc; 87 pthread_t thread[THREAD_NUM]; 88 89 if (pthread_mutex_init(&td.mutex, NULL) != 0) { 90 fprintf(stderr,"Fail to initialize mutex\n"); 91 return PTS_UNRESOLVED; 92 } 93 if (pthread_cond_init(&td.cond, NULL) != 0) { 94 fprintf(stderr,"Fail to initialize cond\n"); 95 return PTS_UNRESOLVED; 96 } 97 98 for (i=0; i<THREAD_NUM; i++) { /* create THREAD_NUM threads */ 99 if (pthread_create(&thread[i], NULL, thr_func, NULL) != 0) { 100 fprintf(stderr,"Fail to create thread[%d]\n", i); 101 return PTS_UNRESOLVED; 102 } 103 } 104 while (start_num < THREAD_NUM) /* waiting for all threads started */ 105 usleep(100); 106 107 /* Acquire the mutex to make sure that all waiters are currently 108 blocked on pthread_cond_timedwait */ 109 if (pthread_mutex_lock(&td.mutex) != 0) { 110 fprintf(stderr,"Main: Fail to acquire mutex\n"); 111 return PTS_UNRESOLVED; 112 } 113 if (pthread_mutex_unlock(&td.mutex) != 0) { 114 fprintf(stderr,"Main: Fail to release mutex\n"); 115 return PTS_UNRESOLVED; 116 } 117 118 /* broadcast the condition to wake up all waiters */ 119 fprintf(stderr,"[Main thread] broadcast the condition\n"); 120 rc = pthread_cond_broadcast(&td.cond); 121 if (rc != 0) { 122 fprintf(stderr,"[Main thread] failed to broadcast the condition\n"); 123 return PTS_UNRESOLVED; 124 } 125 sleep(1); 126 if (waken_num < THREAD_NUM){ 127 fprintf(stderr,"[Main thread] Not all waiters were wakened\n"); 128 for (i=0; i<THREAD_NUM; i++) { 129 pthread_cancel(thread[i]); 130 } 131 return PTS_UNRESOLVED; 132 } 133 fprintf(stderr,"[Main thread] all waiters were wakened\n"); 134 135 /* join all secondary threads */ 136 for (i=0; i<THREAD_NUM; i++) { 137 if (pthread_join(thread[i], NULL) != 0) { 138 fprintf(stderr,"Fail to join thread[%d]\n", i); 139 return PTS_UNRESOLVED; 140 } 141 } 142 printf("Test PASSED\n"); 143 return PTS_PASS; 144} 145