1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci// utils for multi-thread and multi-process test
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ci#ifndef KERNEL_LITE_MT_UTILS
19f6603c60Sopenharmony_ci#define KERNEL_LITE_MT_UTILS
20f6603c60Sopenharmony_ci
21f6603c60Sopenharmony_ci#include <stdint.h>
22f6603c60Sopenharmony_ci
23f6603c60Sopenharmony_ci// count all primes numbers below 'maxNumber', used for keep cpu busy
24f6603c60Sopenharmony_ciint CountPrimes(uint32_t maxNumber);
25f6603c60Sopenharmony_ci
26f6603c60Sopenharmony_ci/**
27f6603c60Sopenharmony_ci * desc:   keep cpu run for 'ms' miliseconds, without any syscall or pend operation
28f6603c60Sopenharmony_ci * input:  miliseconds value, only some specific value is supported, as below:
29f6603c60Sopenharmony_ci *         -- 10, 20, 30, 60, 100, 150, 200, 300, 400, 600, 1000, 2000
30f6603c60Sopenharmony_ci * output: useless, only used for avoid the whole function is optimized by gcc
31f6603c60Sopenharmony_ci * note:   the actual elasped time is not so accurate
32f6603c60Sopenharmony_ci */
33f6603c60Sopenharmony_ciint BusyRun(uint32_t ms);
34f6603c60Sopenharmony_ci
35f6603c60Sopenharmony_ci/**
36f6603c60Sopenharmony_ci * desc:   used for check if the code is runned as expected, in multi-thread or multi-process scenario
37f6603c60Sopenharmony_ci * input:  step -- from 1 to 15(0xf).
38f6603c60Sopenharmony_ci * output: the actual steps the this function is called.
39f6603c60Sopenharmony_ci Example:
40f6603c60Sopenharmony_ci    LOG("step=%lx", CheckStep(1));  // may called in thread 1
41f6603c60Sopenharmony_ci    LOG("step=%lx", CheckStep(2));  // may called in thread 2
42f6603c60Sopenharmony_ci    LOG("step=%lx", CheckStep(3));  // may called in thread 3
43f6603c60Sopenharmony_ci    LOG("step=%lx", CheckStep(4));  // may called in thread 2
44f6603c60Sopenharmony_ci    uint64_t step = CheckStep(5);   // may called in thread 1
45f6603c60Sopenharmony_ci    if (step == 0x12345) {
46f6603c60Sopenharmony_ci        LOG("everything is ok");
47f6603c60Sopenharmony_ci    } else {
48f6603c60Sopenharmony_ci        LOG("code not run as expected!");
49f6603c60Sopenharmony_ci    }
50f6603c60Sopenharmony_ci Output:
51f6603c60Sopenharmony_ci    step=1
52f6603c60Sopenharmony_ci    step=12
53f6603c60Sopenharmony_ci    step=123
54f6603c60Sopenharmony_ci    step=1234
55f6603c60Sopenharmony_ci    everything is ok
56f6603c60Sopenharmony_ci */
57f6603c60Sopenharmony_ciuint64_t CheckStep(int step);
58f6603c60Sopenharmony_ci
59f6603c60Sopenharmony_ci
60f6603c60Sopenharmony_ci/**
61f6603c60Sopenharmony_ci * desc:  used for synchronize in multi-process scenario, not suitable for multi-thread.
62f6603c60Sopenharmony_ci * usage:
63f6603c60Sopenharmony_ci InitPipe();
64f6603c60Sopenharmony_ci fork();
65f6603c60Sopenharmony_ci if parent:
66f6603c60Sopenharmony_ci    ...
67f6603c60Sopenharmony_ci    BlockOnPipe();  // will block until UnBlockPipe is called in another process.
68f6603c60Sopenharmony_ci    ...
69f6603c60Sopenharmony_ci if child:
70f6603c60Sopenharmony_ci    ...
71f6603c60Sopenharmony_ci    UnBlockPipe();
72f6603c60Sopenharmony_ci    ...
73f6603c60Sopenharmony_ci */
74f6603c60Sopenharmony_ci// return -1 if pipe-init failed, user should check return code
75f6603c60Sopenharmony_ciint InitPipe();
76f6603c60Sopenharmony_civoid BlockOnPipe();
77f6603c60Sopenharmony_civoid UnBlockPipe();
78f6603c60Sopenharmony_ci
79f6603c60Sopenharmony_ci/**
80f6603c60Sopenharmony_ci * desc:    get and set a global variable, can be used in multi-process scenario
81f6603c60Sopenharmony_ci * usage:
82f6603c60Sopenharmony_ci *    InitGlobalVariable();
83f6603c60Sopenharmony_ci *    fork();
84f6603c60Sopenharmony_ci *    if child:
85f6603c60Sopenharmony_ci *        ...
86f6603c60Sopenharmony_ci *        SetGlobalVariable(x);
87f6603c60Sopenharmony_ci *        ...
88f6603c60Sopenharmony_ci *    if parent:
89f6603c60Sopenharmony_ci *        ...
90f6603c60Sopenharmony_ci *        SetGlobalVariable(y);
91f6603c60Sopenharmony_ci *        ...
92f6603c60Sopenharmony_ci *        int i = GetGlobalVariable();
93f6603c60Sopenharmony_ci *        // check if i is ok
94f6603c60Sopenharmony_ci *        ...
95f6603c60Sopenharmony_ci *    DeleteGlobalVariable(); // Call before exit
96f6603c60Sopenharmony_ci *    exit...
97f6603c60Sopenharmony_ci */
98f6603c60Sopenharmony_ci// output:  return -1 if the operation fails, user should check return code
99f6603c60Sopenharmony_ciint InitGlobalVariable();
100f6603c60Sopenharmony_ci// return -1 if the operation fails
101f6603c60Sopenharmony_ciint SetGlobalVariable(int value);
102f6603c60Sopenharmony_ci// return -1 if the operation fails.
103f6603c60Sopenharmony_ciint GetGlobalVariable();
104f6603c60Sopenharmony_ci// return -1 if the operation fails
105f6603c60Sopenharmony_ciint DeleteGlobalVariable();
106f6603c60Sopenharmony_ci
107f6603c60Sopenharmony_ci#endif
108