1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <pthread.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include "functionalext.h"
20
21 static pthread_key_t g_key;
22
threadfuncA(void *arg)23 void *threadfuncA(void *arg)
24 {
25 int32_t value = 0;
26 int32_t ret = pthread_setspecific(g_key, &value);
27 EXPECT_EQ("pthread_setspecific_0100", ret, 0);
28 int32_t *keyRet = (int32_t *)pthread_getspecific(g_key);
29 EXPECT_EQ("pthread_setspecific_0100", *keyRet, 0);
30 return arg;
31 }
32
threadfuncB(void *arg)33 void *threadfuncB(void *arg)
34 {
35 int32_t value = 0;
36 int32_t ret = pthread_setspecific(g_key, &value);
37 EXPECT_EQ("pthread_setspecific_0200", ret, 0);
38 ret = pthread_setspecific(g_key, &value);
39 EXPECT_EQ("pthread_setspecific_0200", ret, 0);
40 int32_t *keyRet = (int32_t *)pthread_getspecific(g_key);
41 EXPECT_EQ("pthread_setspecific_0200", *keyRet, 0);
42 return arg;
43 }
44
45 /**
46 * @tc.name: pthread_setspecific_0100
47 * @tc.desc: Verify call pthread_setspecific once process success.
48 * @tc.level: level 0.
49 */
pthread_setspecific_0100(void)50 void pthread_setspecific_0100(void)
51 {
52 pthread_key_create(&g_key, NULL);
53 pthread_t tid1;
54 pthread_create(&tid1, NULL, threadfuncA, NULL);
55 pthread_join(tid1, NULL);
56 pthread_key_delete(g_key);
57 }
58
59 /**
60 * @tc.name: pthread_setspecific_0200
61 * @tc.desc: Verify call pthread_setspecific twice process success.
62 * @tc.level: level 0
63 */
pthread_setspecific_0200(void)64 void pthread_setspecific_0200(void)
65 {
66 pthread_key_create(&g_key, NULL);
67 pthread_t tid2;
68 pthread_create(&tid2, NULL, threadfuncB, NULL);
69 pthread_join(tid2, NULL);
70 pthread_key_delete(g_key);
71 }
72
main(void)73 int main(void)
74 {
75 pthread_setspecific_0100();
76 pthread_setspecific_0200();
77 return t_status;
78 }
79