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