1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "xts_ipc.h"
32
33 LITE_TEST_SUIT(FUTEX, PthreadMutexApiTest, PthreadMutexApiTestSuite);
34
PthreadMutexApiTestSuiteSetUp(void)35 static BOOL PthreadMutexApiTestSuiteSetUp(void)
36 {
37 return TRUE;
38 }
39
PthreadMutexApiTestSuiteTearDown(void)40 static BOOL PthreadMutexApiTestSuiteTearDown(void)
41 {
42 return TRUE;
43 }
44
45 /********************************************* Test case dividing line ***********************************************/
46
47 struct PthreadMutexCond {
48 int loopNum;
49 int countMax;
50 int count;
51 int top;
52 int bottom;
53 pthread_cond_t notfull;
54 pthread_cond_t notempty;
55 pthread_mutex_t mutex;
56 };
57
58 struct PthreadMutexCond g_st1;
59
PthreadProduce(void *arg)60 void *PthreadProduce(void *arg)
61 {
62 int ret;
63 for (int i = 0; i < g_st1.loopNum; i++) {
64 ret = pthread_mutex_lock(&g_st1.mutex);
65 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
66 // check full
67 if ((g_st1.top + 1) % g_st1.countMax == g_st1.bottom) { /* 1, common data for test, no special meaning */
68 ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex);
69 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
70 }
71 // Produce
72 g_st1.top = (g_st1.top + 1) % g_st1.countMax; /* 1, common data for test, no special meaning */
73 g_st1.count++;
74
75 ret = pthread_cond_signal(&g_st1.notempty);
76 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
77 ret = pthread_mutex_unlock(&g_st1.mutex);
78 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
79 usleep(10); /* 10, common data for test, no special meaning */
80 }
81 return arg;
82 }
83
PthreadConsume(void *arg)84 void *PthreadConsume(void *arg)
85 {
86 int ret;
87 for (int i = 0; i < g_st1.loopNum; i++) {
88 ret = pthread_mutex_lock(&g_st1.mutex);
89 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
90 // check empty
91 if (g_st1.top == g_st1.bottom) {
92 ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex);
93 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
94 }
95 // Consume
96 g_st1.bottom = (g_st1.bottom + 1) % g_st1.countMax;
97 g_st1.count--;
98
99 ret = pthread_cond_signal(&g_st1.notempty);
100 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
101 ret = pthread_mutex_unlock(&g_st1.mutex);
102 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
103
104 usleep(10); /* 10, common data for test, no special meaning */
105 }
106 return arg;
107 }
108
109 /**
110 * @tc.number SUB_KERNEL_FUTEX_MUTEX_ALL_0300
111 * @tc.name test pthread_mutex with condition variable, produce and consume
112 * @tc.desc [C- SOFTWARE -0200]
113 */
114 LITE_TEST_CASE(PthreadMutexApiTestSuite, testPthreadMutexCond, Function | MediumTest | Level3)
115 {
116 int ret;
117 pthread_t tid[2]; /* 2, common data for test, no special meaning */
118
119 g_st1.count = 0;
120 memset_s(tid, sizeof(tid), POSIX_IPC_NO_ERROR, sizeof(tid));
121
122 ret = pthread_create(&tid[0], NULL, PthreadProduce, NULL);
123 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
124 ret = pthread_create(&tid[1], NULL, PthreadConsume, NULL); /* 1, common data for test, no special meaning */
125 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
126 ret = pthread_join(tid[0], NULL);
127 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
128 ret = pthread_join(tid[1], NULL); /* 1, common data for test, no special meaning */
129 ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
130 ICUNIT_ASSERT_EQUAL(g_st1.count, POSIX_IPC_NO_ERROR, g_st1.count);
131 return 0;
132 }
133
134 RUN_TEST_SUITE(PthreadMutexApiTestSuite);
135
PosixFutexMutexTest(void)136 void PosixFutexMutexTest(void)
137 {
138 RUN_ONE_TESTCASE(testPthreadMutexCond);
139 }
140