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_signal()
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 <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <signal.h>
21#include "posixtest.h"
22
23#define THREAD_NUM  3
24
25struct testdata
26{
27	pthread_mutex_t mutex;
28	pthread_cond_t  cond;
29} td;
30
31pthread_t  thread[THREAD_NUM];
32
33int start_num = 0;
34int waken_num = 0;
35
36/* Alarm handler */
37void alarm_handler(int signo)
38{
39	int i;
40	printf("Error: failed to wakeup all threads\n");
41	for (i=0; i<THREAD_NUM; i++) {	/* cancel threads */
42	    	pthread_cancel(thread[i]);
43	}
44
45	exit(PTS_UNRESOLVED);
46}
47void *thr_func(void *arg)
48{
49	int rc;
50	pthread_t self = pthread_self();
51
52	if (pthread_mutex_lock(&td.mutex) != 0) {
53		fprintf(stderr,"[Thread 0x%p] failed to acquire the mutex\n", (void*)self);
54		exit(PTS_UNRESOLVED);
55	}
56	fprintf(stderr,"[Thread 0x%p] started and locked the mutex\n", (void*)self);
57	start_num ++;
58
59	fprintf(stderr,"[Thread 0x%p] is waiting for the cond\n", (void*)self);
60	rc = pthread_cond_wait(&td.cond, &td.mutex);
61	if(rc != 0) {
62		fprintf(stderr,"pthread_cond_wait return %d\n", rc);
63                exit(PTS_UNRESOLVED);
64	}
65
66	if (pthread_mutex_trylock(&td.mutex) != 0) {
67		fprintf(stderr,"[Thread 0x%p] should be able to lock the recursive 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	if (pthread_mutex_unlock(&td.mutex) != 0) {
81		fprintf(stderr,"[Thread 0x%p] did not owned the mutex after the cond wait\n", (void*)self);
82                printf("Test FAILED\n");
83		exit(PTS_FAIL);
84	}
85	fprintf(stderr,"[Thread 0x%p] released the mutex\n", (void*)self);
86	return NULL;
87}
88
89int main()
90{
91	int i;
92	struct sigaction act;
93	pthread_mutexattr_t ma;
94
95	if (pthread_mutexattr_init(&ma) != 0) {
96		fprintf(stderr,"Fail to initialize mutex attribute\n");
97		return PTS_UNRESOLVED;
98	}
99	if (pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) != 0) {
100		fprintf(stderr,"Fail to set the mutex attribute\n");
101		return PTS_UNRESOLVED;
102	}
103
104	if (pthread_mutex_init(&td.mutex, &ma) != 0) {
105		fprintf(stderr,"Fail to initialize mutex\n");
106		return PTS_UNRESOLVED;
107	}
108	if (pthread_cond_init(&td.cond, NULL) != 0) {
109		fprintf(stderr,"Fail to initialize cond\n");
110		return PTS_UNRESOLVED;
111	}
112
113	for (i=0; i<THREAD_NUM; i++) {
114	    	if (pthread_create(&thread[i], NULL, thr_func, NULL) != 0) {
115			fprintf(stderr,"Fail to create thread[%d]\n", i);
116			return PTS_UNRESOLVED;
117		}
118	}
119	while (start_num < THREAD_NUM)	/* waiting for all threads started */
120		usleep(100);
121
122	sleep(1);
123
124	/* Setup alarm handler */
125	act.sa_handler=alarm_handler;
126	act.sa_flags=0;
127	sigemptyset(&act.sa_mask);
128	sigaction(SIGALRM, &act, 0);
129	alarm(5);
130
131	while (waken_num < THREAD_NUM) { /* waiting for all threads wakened */
132		fprintf(stderr,"[Main thread] signals a condition\n");
133		if (pthread_cond_signal(&td.cond) != 0) {
134			fprintf(stderr,"Main failed to signal the condition\n");
135			return PTS_UNRESOLVED;
136		}
137		usleep(100);
138	}
139
140	for (i=0; i<THREAD_NUM; i++) {
141	    	if (pthread_join(thread[i], NULL) != 0) {
142			fprintf(stderr,"Fail to join thread[%d]\n", i);
143			return PTS_UNRESOLVED;
144		}
145	}
146	printf("Test PASSED\n");
147	return PTS_PASS;
148}
149