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_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 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	pthread_t  thread[THREAD_NUM];
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	while (waken_num < THREAD_NUM) { /* waiting for all threads wakened */
125		fprintf(stderr,"[Main thread] signals a condition\n");
126		if (pthread_cond_signal(&td.cond) != 0) {
127			fprintf(stderr,"Main failed to signal the condition\n");
128			return PTS_UNRESOLVED;
129		}
130		sleep(1);
131	}
132
133	for (i=0; i<THREAD_NUM; i++) {
134	    	if (pthread_join(thread[i], NULL) != 0) {
135			fprintf(stderr,"Fail to join thread[%d]\n", i);
136			return PTS_UNRESOLVED;
137		}
138	}
139	printf("Test PASSED\n");
140	return PTS_PASS;
141}
142