1 /* 2 * Copyright (c) 2021 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 // utils for multi-thread and multi-process test 17 18 #ifndef KERNEL_LITE_MT_UTILS 19 #define KERNEL_LITE_MT_UTILS 20 21 #include <stdint.h> 22 23 // count all primes numbers below 'maxNumber', used for keep cpu busy 24 int CountPrimes(uint32_t maxNumber); 25 26 /** 27 * desc: keep cpu run for 'ms' miliseconds, without any syscall or pend operation 28 * input: miliseconds value, only some specific value is supported, as below: 29 * -- 10, 20, 30, 60, 100, 150, 200, 300, 400, 600, 1000, 2000 30 * output: useless, only used for avoid the whole function is optimized by gcc 31 * note: the actual elasped time is not so accurate 32 */ 33 int BusyRun(uint32_t ms); 34 35 /** 36 * desc: used for check if the code is runned as expected, in multi-thread or multi-process scenario 37 * input: step -- from 1 to 15(0xf). 38 * output: the actual steps the this function is called. 39 Example: 40 LOG("step=%lx", CheckStep(1)); // may called in thread 1 41 LOG("step=%lx", CheckStep(2)); // may called in thread 2 42 LOG("step=%lx", CheckStep(3)); // may called in thread 3 43 LOG("step=%lx", CheckStep(4)); // may called in thread 2 44 uint64_t step = CheckStep(5); // may called in thread 1 45 if (step == 0x12345) { 46 LOG("everything is ok"); 47 } else { 48 LOG("code not run as expected!"); 49 } 50 Output: 51 step=1 52 step=12 53 step=123 54 step=1234 55 everything is ok 56 */ 57 uint64_t CheckStep(int step); 58 59 60 /** 61 * desc: used for synchronize in multi-process scenario, not suitable for multi-thread. 62 * usage: 63 InitPipe(); 64 fork(); 65 if parent: 66 ... 67 BlockOnPipe(); // will block until UnBlockPipe is called in another process. 68 ... 69 if child: 70 ... 71 UnBlockPipe(); 72 ... 73 */ 74 // return -1 if pipe-init failed, user should check return code 75 int InitPipe(); 76 void BlockOnPipe(); 77 void UnBlockPipe(); 78 79 /** 80 * desc: get and set a global variable, can be used in multi-process scenario 81 * usage: 82 * InitGlobalVariable(); 83 * fork(); 84 * if child: 85 * ... 86 * SetGlobalVariable(x); 87 * ... 88 * if parent: 89 * ... 90 * SetGlobalVariable(y); 91 * ... 92 * int i = GetGlobalVariable(); 93 * // check if i is ok 94 * ... 95 * DeleteGlobalVariable(); // Call before exit 96 * exit... 97 */ 98 // output: return -1 if the operation fails, user should check return code 99 int InitGlobalVariable(); 100 // return -1 if the operation fails 101 int SetGlobalVariable(int value); 102 // return -1 if the operation fails. 103 int GetGlobalVariable(); 104 // return -1 if the operation fails 105 int DeleteGlobalVariable(); 106 107 #endif 108