• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_pthread_test.h"
32 
33 static pthread_mutex_t g_pthreadMuxTest1;
34 static pthread_cond_t g_pthrdCondTest1;
35 static unsigned int g_pthreadExit = 0;
36 
PthreadF01(void * t)37 static void *PthreadF01(void *t)
38 {
39     int rc;
40     unsigned int count = 0;
41     const int testLoop = 2000;
42     while (count < testLoop) {
43         rc = pthread_mutex_lock(&g_pthreadMuxTest1);
44         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
45 
46         rc = pthread_cond_wait(&g_pthrdCondTest1, &g_pthreadMuxTest1);
47         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
48 
49         rc = pthread_mutex_unlock(&g_pthreadMuxTest1);
50         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
51         count++;
52     }
53 
54     g_pthreadExit = 1;
55 EXIT:
56     return NULL;
57 }
58 
PthreadF02(void * t)59 static void *PthreadF02(void *t)
60 {
61     int i;
62     int rc;
63 
64     while (g_pthreadExit != 1) {
65         rc = pthread_mutex_lock(&g_pthreadMuxTest1);
66         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
67 
68         rc = pthread_cond_signal(&g_pthrdCondTest1);
69         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
70 
71         rc = pthread_mutex_unlock(&g_pthreadMuxTest1);
72         ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
73     }
74 
75 EXIT:
76     pthread_exit(NULL);
77 }
78 
TestCase(void)79 static unsigned int TestCase(void)
80 {
81     int i;
82     long t1 = 1;
83     long t2 = 2;
84     int rc;
85     pthread_t threads[3];
86     pthread_attr_t attr;
87     const int loopNum = 2;
88 
89     g_pthreadExit = 0;
90     g_testCount = 0;
91     pthread_mutex_init(&g_pthreadMuxTest1, NULL);
92     pthread_cond_init(&g_pthrdCondTest1, NULL);
93 
94     rc = pthread_create(&threads[0], NULL, PthreadF01, (void *)t1);
95     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
96 
97     rc = pthread_create(&threads[1], NULL, PthreadF02, (void *)t2);
98     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
99 
100     for (i = 0; i < loopNum; i++) {
101         rc = pthread_join(threads[i], NULL);
102         ICUNIT_ASSERT_EQUAL(rc, 0, rc);
103     }
104 
105     rc = pthread_attr_destroy(&attr);
106     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
107 
108     rc = pthread_mutex_destroy(&g_pthreadMuxTest1);
109     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
110     rc = pthread_cond_destroy(&g_pthrdCondTest1);
111     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
112 
113     return 0;
114 }
115 
ItTestPthreadCond002(void)116 void ItTestPthreadCond002(void)
117 {
118     TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_002", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
119 }
120