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_spinlock_test.h"
32 #include "string.h"
33
34 static const int WRITE_THREAD_COUNT = 2; // 2, set test count.
35 static const int TEST_LOOP_COUNT = 1;
36 static const int TEST_COUNT = 2; // 2, set write thread count.
37 static pthread_spinlock_t g_spinlockLock;
38
39 static const int TEST_DATA_SIZE = 100000; // 100000, set test data size.
40 static int g_spinlockData[TEST_DATA_SIZE];
41 static int g_spinlockMask;
42 static volatile int g_isWriting[WRITE_THREAD_COUNT];
43 static volatile int g_isWriteExit[WRITE_THREAD_COUNT];
44 static int g_writePar[WRITE_THREAD_COUNT];
45
ThreadWriteFunc1(void * a)46 static void *ThreadWriteFunc1(void *a)
47 {
48 int ret;
49 int testCount = TEST_LOOP_COUNT;
50 int oldRwlockMask;
51 pthread_t thread = pthread_self();
52 int threadCount = *((int *)a);
53 int count;
54
55 ret = pthread_detach(thread);
56 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
57
58 while (testCount > 0) {
59 ret = pthread_spin_lock(&g_spinlockLock);
60 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
61
62 g_isWriting[threadCount] = 1;
63
64 oldRwlockMask = g_spinlockMask;
65
66 g_spinlockMask++;
67
68 for (count = 0; count < TEST_DATA_SIZE; count++) {
69 ICUNIT_GOTO_EQUAL(g_spinlockData[count], oldRwlockMask, g_spinlockData[count], EXIT);
70 g_spinlockData[count] = g_spinlockMask;
71 }
72
73 g_isWriting[threadCount] = 0;
74
75 ret = pthread_spin_unlock(&g_spinlockLock);
76 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
77
78 SLEEP_AND_YIELD(1);
79 testCount--;
80 }
81
82 g_isWriteExit[threadCount] = 6; // 6, The current possible value of the variable.
83
84 EXIT:
85 return nullptr;
86 }
87
ThreadWriteFunc(void * a)88 static void *ThreadWriteFunc(void *a)
89 {
90 int ret;
91 int testCount = TEST_LOOP_COUNT;
92 int oldRwlockMask;
93 pthread_t thread = pthread_self();
94 int threadCount = *((int *)a);
95 int count;
96
97 while (testCount > 0) {
98 ret = pthread_spin_lock(&g_spinlockLock);
99 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
100
101 g_isWriting[threadCount] = 1;
102
103 oldRwlockMask = g_spinlockMask;
104
105 g_spinlockMask++;
106
107 for (count = 0; count < TEST_DATA_SIZE; count++) {
108 ICUNIT_GOTO_EQUAL(g_spinlockData[count], oldRwlockMask, g_spinlockData[count], EXIT);
109 g_spinlockData[count] = g_spinlockMask;
110 }
111
112 g_isWriting[threadCount] = 0;
113
114 ret = pthread_spin_unlock(&g_spinlockLock);
115 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
116
117 SLEEP_AND_YIELD(1);
118 testCount--;
119 }
120
121 EXIT:
122 return nullptr;
123 }
124
Testcase(void)125 static int Testcase(void)
126 {
127 struct sched_param param = { 0 };
128 int ret;
129 void *res = nullptr;
130 pthread_attr_t a = { 0 };
131 pthread_t thread = pthread_self();
132 pthread_t newPthread, newPthread1;
133 pthread_rwlockattr_t rwlock;
134 int index = 0;
135 int curThreadPri, curThreadPolicy;
136
137 (void)memset_s((void *)g_spinlockData, sizeof(int) * TEST_DATA_SIZE, 0, sizeof(int) * TEST_DATA_SIZE);
138 g_spinlockMask = 0;
139
140 pthread_spin_init(&g_spinlockLock, 0);
141
142 ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m);
143 ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
144
145 curThreadPri = param.sched_priority;
146
147 while (index < TEST_COUNT) {
148 ret = pthread_attr_init(&a);
149 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
150 param.sched_priority = curThreadPri + 1;
151 pthread_attr_setschedparam(&a, ¶m);
152
153 g_writePar[0] = 0;
154 ret = pthread_create(&newPthread1, &a, ThreadWriteFunc, &g_writePar[0]);
155 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
156
157 g_writePar[1] = 1;
158 ret = pthread_create(&newPthread, &a, ThreadWriteFunc1, &g_writePar[1]);
159 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
160 g_isWriting[1] = 0;
161 g_isWriteExit[1] = 0;
162
163 ret = pthread_join(newPthread1, &res);
164 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
165 index++;
166 }
167
168 pthread_spin_destroy(&g_spinlockLock);
169
170 return 0;
171 }
172
ItTestPthreadSpinlock001(void)173 void ItTestPthreadSpinlock001(void)
174 {
175 TEST_ADD_CASE("IT_POSIX_PTHREAD_SPINLOCK_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
176 }
177