1 /*
2  * Copyright (c) 2004, QUALCOMM Inc. All rights reserved.
3  * Created by:  abisain REMOVE-THIS AT qualcomm 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 when  pthread_cond_destroy()
9  *   is called on a cond that some thread is waiting, then it returns
10  *   EBUSY
11 
12  * Steps:
13  * 1. Create a condvar
14  * 2. Create a thread and make it wait on the condvar
15  * 3. Try to destroy the cond var in main
16  * 4. Check that pthread_cond_destroy returns EBUSY
17  */
18 
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include "posixtest.h"
25 
26 #define TEST "4-1"
27 #define FUNCTION "pthread_cond_destroy"
28 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
29 
30 /* cond used by the two threads */
31 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
32 
33 /* cond used by the two threads */
34 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
35 
thread(void * tmp)36 void * thread(void *tmp)
37 {
38 	int    rc = 0;
39 
40 	/* acquire the mutex */
41 	rc  = pthread_mutex_lock(&mutex);
42 	if(rc != 0) {
43 		printf(ERROR_PREFIX "pthread_mutex_lock\n");
44 		exit(PTS_UNRESOLVED);
45 	}
46 
47 	/* Wait on the cond var. This will not return, as nobody signals*/
48 	rc = pthread_cond_wait(&cond, &mutex);
49 	if(rc != 0) {
50 		printf(ERROR_PREFIX "pthread_cond_wait\n");
51 		exit(PTS_UNRESOLVED);
52 	}
53 
54 	rc = pthread_mutex_unlock(&mutex);
55 	if(rc != 0) {
56 		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
57 		exit(PTS_UNRESOLVED);
58 	}
59 	return NULL;
60 }
61 
main()62 int main()
63 {
64 	pthread_t low_id;
65 	int       rc = 0;
66 
67 	/* Create a new thread with default attributes */
68 	rc = pthread_create(&low_id, NULL, thread, NULL);
69 	if(rc != 0) {
70 		printf(ERROR_PREFIX "pthread_create\n");
71 		exit(PTS_UNRESOLVED);
72 	}
73 
74 	/* Let the other thread run */
75 	sleep(2);
76 
77 	/* Try to destroy the cond var. This should return an error */
78 	rc = pthread_cond_destroy(&cond);
79 	if(rc != EBUSY) {
80 		printf(ERROR_PREFIX "Test PASS: Expected %d(EBUSY) got %d, "
81 			"though the standard states 'may' fail\n", EBUSY, rc);
82 
83 		exit(PTS_PASS);
84 	}
85 
86 	printf("Test PASS\n");
87 	exit(PTS_PASS);
88 }
89