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 <stdlib.h>
17 #include <threads.h>
18 #include "functionalext.h"
19
20 static char list[100];
21 static char buf[12] = "called once";
do_once(void)22 void do_once(void)
23 {
24 FILE *fp = fopen("call_once.txt", "a+");
25 fwrite(buf, sizeof(char), strlen(buf), fp);
26 fclose(fp);
27 FILE *fp2 = fopen("call_once.txt", "r");
28 fread(list, sizeof(list), 1, fp2);
29 fclose(fp2);
30 }
31 static once_flag flag = ONCE_FLAG_INIT;
func(void * data)32 void func(void *data)
33 {
34 call_once(&flag, do_once);
35 }
36
37 /**
38 * @tc.name : call_once_0100
39 * @tc.desc : Each parameter is valid, and the function func is called only once.
40 * @tc.level : Level 0
41 */
call_once_0100(void)42 void call_once_0100(void)
43 {
44 EXPECT_EQ("call_once_0100", strcmp(list, buf), 0);
45 }
46
main(void)47 int main(void)
48 {
49 thrd_t t1, t2, t3, t4;
50 thrd_create(&t1, (thrd_start_t)func, NULL);
51 thrd_create(&t2, (thrd_start_t)func, NULL);
52 thrd_create(&t3, (thrd_start_t)func, NULL);
53 thrd_create(&t4, (thrd_start_t)func, NULL);
54 thrd_join(t1, NULL);
55 thrd_join(t2, NULL);
56 thrd_join(t3, NULL);
57 thrd_join(t4, NULL);
58 call_once_0100();
59 remove("call_once.txt");
60 memset(list, 0x00, sizeof(list));
61 return t_status;
62 }