• 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_pthdCondTest1;
35 
PthreadF01(void * t)36 static void *PthreadF01(void *t)
37 {
38     int rc;
39 
40     rc = pthread_mutex_lock(&g_pthreadMuxTest1);
41     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
42 
43     g_testCount++;
44     LosTaskDelay(100);
45     ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT); // 2, here assert the result.
46     g_testCount++;
47 
48     rc = pthread_cond_wait(&g_pthdCondTest1, &g_pthreadMuxTest1);
49     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
50 
51     ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); // 5, here assert the result.
52     rc = pthread_mutex_unlock(&g_pthreadMuxTest1);
53     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
54 
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     ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
65     g_testCount++;
66     rc = pthread_mutex_lock(&g_pthreadMuxTest1);
67     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
68 
69     ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, here assert the result.
70     g_testCount++;
71     rc = pthread_cond_signal(&g_pthdCondTest1);
72     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
73 
74     ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 4, here assert the result.
75     g_testCount++;
76 
77     rc = pthread_mutex_unlock(&g_pthreadMuxTest1);
78     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
79     LosTaskDelay(2); // 2, delay for Timing control.
80 
81 EXIT:
82     pthread_exit(NULL);
83 }
84 
TestCase(void)85 static unsigned int TestCase(void)
86 {
87     int i;
88     long t1 = 1;
89     long t2 = 2;
90     int rc;
91     pthread_t threads[3]; // 3, need 3 pthread for test.
92     pthread_attr_t attr;
93     const int loopNum = 2;
94 
95     g_testCount = 0;
96     pthread_mutex_init(&g_pthreadMuxTest1, NULL);
97     pthread_cond_init(&g_pthdCondTest1, NULL);
98 
99     rc = pthread_create(&threads[0], NULL, PthreadF01, (void *)t1);
100     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
101 
102     rc = pthread_create(&threads[1], NULL, PthreadF02, (void *)t2);
103     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
104 
105     for (i = 0; i < loopNum; i++) {
106         rc = pthread_join(threads[i], NULL);
107         ICUNIT_ASSERT_EQUAL(rc, 0, rc);
108     }
109 
110     rc = pthread_attr_destroy(&attr);
111     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
112 
113     rc = pthread_mutex_destroy(&g_pthreadMuxTest1);
114     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
115     rc = pthread_cond_destroy(&g_pthdCondTest1);
116     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
117 
118     return 0;
119 }
120 
ItTestPthreadCond001(void)121 void ItTestPthreadCond001(void)
122 {
123     TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_001", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
124 }
125