1 /*
2 * Copyright (c) 2022 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 "It_posix_mutex.h"
32
33 #define THREAD_NUM 5
34 #define LOOPS 4
35
36 static int g_value;
37 static pthread_mutex_t g_mutex036 = PTHREAD_MUTEX_INITIALIZER;
38
39 /* pthread_mutex_lock 1-1.c
40 * Test that pthread_mutex_lock()
41 * shall lock the mutex object referenced by 'mutex'. If the mutex is
42 * already locked, the calling thread shall block until the mutex becomes
43 * available. This operation shall return with the mutex object referenced
44 * by 'mutex' in the locked state with the calling thread as its owner.
45
46 * Steps:
47 * -- Initialize a mutex to protect a global variable 'value'
48 * -- Create N threads. Each is looped M times to acquire the mutex,
49 * increase the value, and then release the mutex.
50 * -- Check if the value has increased properly (M*N); a broken mutex
51 * implementation may cause lost augments.
52 *
53 */
54
TaskF01(void * parm)55 static void *TaskF01(void *parm)
56 {
57 int i, tmp;
58 int rc;
59
60 /* Loopd M times to acquire the mutex, increase the value,
61 and then release the mutex. */
62 for (i = 0; i < LOOPS; ++i) {
63 rc = pthread_mutex_lock(&g_mutex036);
64 if (rc != 0) {
65 return (void *)(LOS_NOK);
66 }
67
68 tmp = g_value;
69 tmp = tmp + 1;
70 sleep(1);
71 g_value = tmp;
72
73 rc = pthread_mutex_unlock(&g_mutex036);
74 if (rc != 0) {
75 return (void *)(LOS_NOK);
76 }
77 sleep(1);
78 }
79
80 return (void *)(LOS_OK);
81 }
82
Testcase(VOID)83 static UINT32 Testcase(VOID)
84 {
85 int i, rc;
86 pthread_attr_t pta;
87 pthread_t threads[THREAD_NUM];
88
89 g_value = 0;
90
91 rc = pthread_attr_init(&pta);
92 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
93
94 rc = pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE);
95 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
96
97 /* Create threads */
98 for (i = 0; i < THREAD_NUM; ++i) {
99 rc = pthread_create(&threads[i], &pta, TaskF01, NULL);
100 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
101 }
102
103 /* Wait to join all threads */
104 for (i = 0; i < THREAD_NUM; ++i) {
105 rc = pthread_join(threads[i], NULL);
106 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
107 }
108
109 rc = pthread_attr_destroy(&pta);
110 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
111
112 rc = pthread_mutex_destroy(&g_mutex036);
113 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
114
115 /* Check if the final value is as expected */
116 ICUNIT_ASSERT_EQUAL(g_value, (THREAD_NUM) * LOOPS, g_value);
117
118 return LOS_OK;
119 }
120
121 /**
122 * @tc.name: ItPosixMux036
123 * @tc.desc: Test interface pthread_mutex_unlock
124 * @tc.type: FUNC
125 * @tc.require: issueI5WZI6
126 */
127
ItPosixMux036(void)128 VOID ItPosixMux036(void)
129 {
130 TEST_ADD_CASE("ItPosixMux036", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
131 }
132