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_wait()
9 *   Upon successful completion, a value of zero shall be returned.
10 */
11
12#include <pthread.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <errno.h>
17#include <signal.h>
18#include "posixtest.h"
19
20struct testdata
21{
22	pthread_mutex_t mutex;
23	pthread_cond_t  cond;
24} td;
25
26pthread_t  thread1;
27
28int t1_start = 0;
29int signaled = 0;
30
31/* Alarm handler */
32void alarm_handler(int signo)
33{
34	printf("Error: failed to wakeup thread\n");
35	pthread_cancel(thread1);
36	exit(PTS_UNRESOLVED);
37}
38
39void *t1_func(void *arg)
40{
41	int rc;
42
43	if (pthread_mutex_lock(&td.mutex) != 0) {
44		fprintf(stderr,"Thread1 failed to acquire the mutex\n");
45		exit(PTS_UNRESOLVED);
46	}
47	fprintf(stderr,"Thread1 started\n");
48	t1_start = 1;	/* let main thread continue */
49
50	fprintf(stderr,"Thread1 is waiting for the cond\n");
51	rc = pthread_cond_wait(&td.cond, &td.mutex);
52	if(rc != 0) {
53		if (rc == EINVAL) {
54			fprintf(stderr,"pthread_cond_wait returns EINVAL\n");
55			exit(PTS_UNRESOLVED);
56		}
57		else if (rc == EPERM) {
58			fprintf(stderr,"pthread_cond_wait returns EPERM\n");
59			exit(PTS_UNRESOLVED);
60		}
61		fprintf(stderr,"pthread_cond_wait returns %d\n", rc);
62                exit(PTS_UNRESOLVED);
63	}
64
65	if(signaled == 0) {
66		fprintf(stderr,"Thread1 waked up before being notified\n");
67                exit(PTS_UNRESOLVED);
68	}
69	fprintf(stderr,"Thread1 wakened up and got returned value 0\n");
70
71	if (pthread_mutex_unlock(&td.mutex) != 0) {
72		fprintf(stderr,"Thread1 failed to release the mutex\n");
73                exit(PTS_UNRESOLVED);
74	}
75	fprintf(stderr,"Thread1 released the mutex\n");
76	return NULL;
77}
78
79int main()
80{
81
82	struct sigaction act;
83
84	if (pthread_mutex_init(&td.mutex, NULL) != 0) {
85		fprintf(stderr,"Fail to initialize mutex\n");
86		return PTS_UNRESOLVED;
87	}
88	if (pthread_cond_init(&td.cond, NULL) != 0) {
89		fprintf(stderr,"Fail to initialize cond\n");
90		return PTS_UNRESOLVED;
91	}
92
93	if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) {
94		fprintf(stderr,"Fail to create thread 1\n");
95		return PTS_UNRESOLVED;
96	}
97
98	sleep(2);
99
100	/* Setup alarm handler */
101	act.sa_handler=alarm_handler;
102	act.sa_flags=0;
103	sigemptyset(&act.sa_mask);
104	sigaction(SIGALRM, &act, 0);
105	alarm(5);
106
107	fprintf(stderr,"To wake up thread1 by broadcasting its waited condition\n");
108	signaled = 1;
109	if (pthread_cond_broadcast(&td.cond) != 0) {
110		fprintf(stderr,"Main failed to broadcast the condition\n");
111		return PTS_UNRESOLVED;
112	}
113
114	pthread_join(thread1, NULL);
115	printf("Test PASSED\n");
116	return PTS_PASS;
117}
118