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_test_shm.h"
32 #include "pthread.h"
33
34 #define TEXT_SZ 8
35 static int g_threadCount = 0;
36
37 struct shared_use_st {
38 int written;
39 char text[TEXT_SZ];
40 };
41
ShmReadFunc(VOID * ptr)42 VOID *ShmReadFunc(VOID *ptr)
43 {
44 void *shm = nullptr;
45 struct shared_use_st *shared = nullptr;
46 int shmid;
47 int ret;
48
49 shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
50 ICUNIT_ASSERT_NOT_EQUAL_NULL_VOID(shmid, -1, shmid);
51
52 shm = shmat(shmid, 0, 0);
53 ICUNIT_ASSERT_NOT_EQUAL_NULL_VOID(shm, INVALID_PTR, shm);
54
55 printf("Memory attached at %p\n", shm);
56
57 shared = (struct shared_use_st *)shm;
58 while (1) {
59 if (shared->written == 1) {
60 printf("You wrote: %s\n", shared->text);
61 sleep(1);
62
63 shared->written = 0;
64 if (strncmp(shared->text, "end", 3) == 0) {
65 break;
66 }
67 } else {
68 sleep(1);
69 }
70 }
71
72 ret = shmdt(shm);
73 ICUNIT_ASSERT_EQUAL_NULL_VOID(ret, 0, ret);
74
75 ret = shmctl(shmid, IPC_RMID, 0);
76 ICUNIT_ASSERT_EQUAL_NULL_VOID(ret, 0, ret);
77
78 g_threadCount++;
79 return nullptr;
80 }
81
ShmWriteFunc(VOID * ptr)82 VOID *ShmWriteFunc(VOID *ptr)
83 {
84 void *shm = nullptr;
85 struct shared_use_st *shared = nullptr;
86 char buffer[BUFSIZ + 1] = {'\0'};
87 int shmid;
88 int ret;
89 static int count = 0;
90
91 shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
92 ICUNIT_ASSERT_NOT_EQUAL_NULL_VOID(shmid, -1, shmid);
93
94 shm = shmat(shmid, nullptr, 0);
95 ICUNIT_ASSERT_NOT_EQUAL_NULL_VOID(shm, INVALID_PTR, shm);
96
97 printf("Memory attached at %p\n", shm);
98
99 shared = (struct shared_use_st *)shm;
100 while (1) {
101 while (shared->written == 1) {
102 sleep(1);
103 printf("%s %d, Waiting...\n", __FUNCTION__, __LINE__);
104 }
105
106 printf("Enter some text: ");
107 if (count == 0) {
108 (void)snprintf_s(buffer, BUFSIZ, BUFSIZ + 1, "test");
109 } else {
110 (void)snprintf_s(buffer, BUFSIZ, BUFSIZ + 1, "end");
111 }
112 count++;
113 (void)strncpy_s(shared->text, TEXT_SZ, buffer, TEXT_SZ - 1);
114
115 shared->written = 1;
116 if (strncmp(buffer, "end", 3) == 0) {
117 break;
118 }
119 }
120
121 ret = shmdt(shm);
122 ICUNIT_ASSERT_EQUAL_NULL_VOID(ret, 0, ret);
123
124 sleep(1);
125 g_threadCount++;
126 return nullptr;
127 }
128
129
Testcase(VOID)130 static int Testcase(VOID)
131 {
132 pthread_t newPthread[2];
133 int curThreadPri, curThreadPolicy;
134 pthread_attr_t a = { 0 };
135 struct sched_param param = { 0 };
136 int ret;
137 int i, j;
138
139 g_threadCount = 0;
140
141 ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m);
142 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143
144 curThreadPri = param.sched_priority;
145 ret = pthread_attr_init(&a);
146 param.sched_priority = curThreadPri;
147 pthread_attr_setschedparam(&a, ¶m);
148
149 ret = pthread_create(&newPthread[0], &a, ShmReadFunc, NULL);
150 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151
152 ret = pthread_create(&newPthread[1], &a, ShmWriteFunc, NULL);
153 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
154
155 ret = pthread_join(newPthread[0], nullptr);
156 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
157
158 ret = pthread_join(newPthread[1], nullptr);
159 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
160
161 ICUNIT_ASSERT_EQUAL(g_threadCount, 2, g_threadCount);
162
163 return 0;
164 }
165
ItTestShm001(void)166 void ItTestShm001(void)
167 {
168 TEST_ADD_CASE("IT_MEM_SHM_001", Testcase, TEST_LOS, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
169 }
170