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_once_t g_once = PTHREAD_ONCE_INIT;
22 static int32_t g_count = 0;
23
OnceRun(void)24 void OnceRun(void)
25 {
26 g_count++;
27 }
28
threadfuncA(void *arg)29 void *threadfuncA(void *arg)
30 {
31 int32_t ret = pthread_once(&g_once, OnceRun);
32 EXPECT_EQ("pthread_once_0100", ret, 0);
33 return arg;
34 }
35
threadfuncB(void *arg)36 void *threadfuncB(void *arg)
37 {
38 int32_t ret = pthread_once(&g_once, OnceRun);
39 EXPECT_EQ("pthread_once_0100", ret, 0);
40 return arg;
41 }
42
43 /**
44 * @tc.name: pthread_once_0100
45 * @tc.desc: Verify process pthread_mutex_once success.
46 * @tc.level: level 0.
47 */
pthread_once_0100(void)48 void pthread_once_0100(void)
49 {
50 pthread_t tid1, tid2;
51 pthread_create(&tid1, NULL, threadfuncA, NULL);
52 pthread_create(&tid2, NULL, threadfuncB, NULL);
53 pthread_join(tid1, NULL);
54 pthread_join(tid2, NULL);
55 }
56
main(void)57 int main(void)
58 {
59 pthread_once_0100();
60 return t_status;
61 }