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