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 int32_t g_sleep_time = 2;
22 static pthread_key_t g_key;
23
threadfuncA(void * arg)24 void *threadfuncA(void *arg)
25 {
26 sleep(g_sleep_time);
27 int32_t value = 0;
28 int32_t ret = pthread_setspecific(g_key, &value);
29 EXPECT_EQ("pthread_key_create_0100_threadfuncA", ret, 0);
30 int32_t *keyRet = (int32_t *)pthread_getspecific(g_key);
31 EXPECT_EQ("pthread_key_create_0100_threadfuncA", *keyRet, 0);
32 return arg;
33 }
34
threadfuncB(void * arg)35 void *threadfuncB(void *arg)
36 {
37 int32_t value = 0;
38 int32_t ret = pthread_setspecific(g_key, &value);
39 EXPECT_EQ("pthread_key_create_0200_threadfuncB", ret, 0);
40 int32_t *keyRet = (int32_t *)pthread_getspecific(g_key);
41 EXPECT_EQ("pthread_key_create_0200_threadfuncB", *keyRet, 0);
42 return arg;
43 }
44
testfunc(void * arg)45 void testfunc(void *arg)
46 {}
47
48 /**
49 * @tc.name: pthread_key_create_0100
50 * @tc.desc: Verify pthread_key_create process success when second args is null.
51 * @tc.desc: level 0
52 */
pthread_key_create_0100(void)53 void pthread_key_create_0100(void)
54 {
55 pthread_t tid1;
56 pthread_create(&tid1, NULL, threadfuncA, NULL);
57 int32_t ret = pthread_key_create(&g_key, NULL);
58 EXPECT_EQ("pthread_key_create_0100", ret, 0);
59 pthread_join(tid1, NULL);
60 pthread_key_delete(g_key);
61 }
62
63 /**
64 * @tc.name: pthread_key_create_0200
65 * @tc.desc: Verify pthread_key_create process success when second args is not null.
66 * @tc.desc: level 1
67 */
pthread_key_create_0200(void)68 void pthread_key_create_0200(void)
69 {
70 pthread_t tid2;
71 int32_t ret = pthread_key_create(&g_key, testfunc);
72 pthread_create(&tid2, NULL, threadfuncB, NULL);
73 pthread_join(tid2, NULL);
74 EXPECT_EQ("pthread_key_create_0200", ret, 0);
75 }
76
main(void)77 int main(void)
78 {
79 pthread_key_create_0100();
80 pthread_key_create_0200();
81 return t_status;
82 }