1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <pthread.h>
17570af302Sopenharmony_ci#include <stdio.h>
18570af302Sopenharmony_ci#include <stdlib.h>
19570af302Sopenharmony_ci#include <string.h>
20570af302Sopenharmony_ci#include "test.h"
21570af302Sopenharmony_ci
22570af302Sopenharmony_cistatic FILE *fp;
23570af302Sopenharmony_ciconst char *path = "/data/test.txt";
24570af302Sopenharmony_ci
25570af302Sopenharmony_civoid *do_child_one(void *a)
26570af302Sopenharmony_ci{
27570af302Sopenharmony_ci    puts("start do_child_one");
28570af302Sopenharmony_ci
29570af302Sopenharmony_ci    flockfile(stdout);
30570af302Sopenharmony_ci
31570af302Sopenharmony_ci    puts("after first flockfile");
32570af302Sopenharmony_ci
33570af302Sopenharmony_ci    flockfile(stdout);
34570af302Sopenharmony_ci
35570af302Sopenharmony_ci    puts("foo");
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci    funlockfile(stdout);
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci    puts("after first funlockfile");
40570af302Sopenharmony_ci
41570af302Sopenharmony_ci    funlockfile(stdout);
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci    puts("all done");
44570af302Sopenharmony_ci    return a;
45570af302Sopenharmony_ci}
46570af302Sopenharmony_ci
47570af302Sopenharmony_civoid *do_child_two(void *p)
48570af302Sopenharmony_ci{
49570af302Sopenharmony_ci    char buf[64];
50570af302Sopenharmony_ci
51570af302Sopenharmony_ci    flockfile(fp);
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci    if (fseek(fp, 0L, SEEK_SET) == -1) {
54570af302Sopenharmony_ci        t_error("%s fseek failed\n", __func__);
55570af302Sopenharmony_ci    }
56570af302Sopenharmony_ci    int ret = fread(buf, 64, 1, fp);
57570af302Sopenharmony_ci
58570af302Sopenharmony_ci    int count = atoi(buf);
59570af302Sopenharmony_ci    ++count;
60570af302Sopenharmony_ci    sprintf(buf, "%d", count);
61570af302Sopenharmony_ci    if (fseek(fp, 0L, SEEK_SET) == -1) {
62570af302Sopenharmony_ci        t_error("%s fseek failed\n", __func__);
63570af302Sopenharmony_ci    }
64570af302Sopenharmony_ci    ret = fwrite(buf, strlen(buf), 1, fp);
65570af302Sopenharmony_ci
66570af302Sopenharmony_ci    funlockfile(fp);
67570af302Sopenharmony_ci    return NULL;
68570af302Sopenharmony_ci}
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci/**
71570af302Sopenharmony_ci * @tc.name      : flockfile_0100
72570af302Sopenharmony_ci * @tc.desc      : Acquire for a thread ownership of a (FILE *) object (stdout)
73570af302Sopenharmony_ci * @tc.level     : Level 0
74570af302Sopenharmony_ci */
75570af302Sopenharmony_civoid flockfile_0100(void)
76570af302Sopenharmony_ci{
77570af302Sopenharmony_ci    pthread_t th;
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci    if (pthread_create(&th, NULL, do_child_one, NULL) != 0) {
80570af302Sopenharmony_ci        t_error("%s pthread_create failed\n", __func__);
81570af302Sopenharmony_ci    }
82570af302Sopenharmony_ci
83570af302Sopenharmony_ci    void *result;
84570af302Sopenharmony_ci    if (pthread_join(th, &result) != 0) {
85570af302Sopenharmony_ci        t_error("%s pthread_join failed\n", __func__);
86570af302Sopenharmony_ci    } else if (result != NULL) {
87570af302Sopenharmony_ci        t_error("%s wrong return value: %p, expected %p\n", __func__, result, NULL);
88570af302Sopenharmony_ci    }
89570af302Sopenharmony_ci}
90570af302Sopenharmony_ci
91570af302Sopenharmony_ci/**
92570af302Sopenharmony_ci * @tc.name      : flockfile_0200
93570af302Sopenharmony_ci * @tc.desc      : Shared FILE* to implements mutual exclusion
94570af302Sopenharmony_ci * @tc.level     : Level 1
95570af302Sopenharmony_ci */
96570af302Sopenharmony_civoid flockfile_0200(void)
97570af302Sopenharmony_ci{
98570af302Sopenharmony_ci    pthread_t tid[100];
99570af302Sopenharmony_ci
100570af302Sopenharmony_ci    fp = fopen(path, "w+");
101570af302Sopenharmony_ci    if (fp == NULL) {
102570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
103570af302Sopenharmony_ci    }
104570af302Sopenharmony_ci
105570af302Sopenharmony_ci    for (int i = 0; i < 100; i++) {
106570af302Sopenharmony_ci        if (pthread_create(tid + i, NULL, do_child_two, NULL) != 0) {
107570af302Sopenharmony_ci            t_error("%s pthread_create failed\n", __func__);
108570af302Sopenharmony_ci        }
109570af302Sopenharmony_ci    }
110570af302Sopenharmony_ci
111570af302Sopenharmony_ci    for (int j = 0; j < 100; j++) {
112570af302Sopenharmony_ci        if (pthread_join(tid[j], NULL) != 0) {
113570af302Sopenharmony_ci            t_error("%s pthread_join failed\n", __func__);
114570af302Sopenharmony_ci        }
115570af302Sopenharmony_ci    }
116570af302Sopenharmony_ci
117570af302Sopenharmony_ci    fclose(fp);
118570af302Sopenharmony_ci    remove(path);
119570af302Sopenharmony_ci}
120570af302Sopenharmony_ci
121570af302Sopenharmony_ciint main(int argc, char *argv[])
122570af302Sopenharmony_ci{
123570af302Sopenharmony_ci    flockfile_0100();
124570af302Sopenharmony_ci    flockfile_0200();
125570af302Sopenharmony_ci    return t_status;
126570af302Sopenharmony_ci}