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 "multithread_constructor.h" 17 18#include <pthread.h> 19#include <signal.h> 20#include <stdio.h> 21#include <unistd.h> 22#include "stdio.h" 23#include "stdlib.h" 24 25const static unsigned int SLEEP_TIMEOUT = 360000; 26 27static void CreateThread(int *argv) 28{ 29 int threadID = *argv; 30 printf("create MultiThread %d\n", threadID); 31 TestFunc1(); 32 return; 33} 34 35static void CreateThreadForCrash(const int *argv) 36{ 37 int threadID = *argv; 38 printf("create ThreadForCrash %d\n", threadID); 39 int ret = raise(SIGSEGV); 40 if (ret != 0) { 41 printf("remove failed!"); 42 } 43 return; 44} 45 46NOINLINE int MultiThreadConstructor(const int threadNum) 47{ 48 pthread_t t[threadNum]; 49 int threadID[threadNum]; 50 51 for (int i = 0; i < threadNum; ++i) { 52 threadID[i] = i; 53 pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]); 54 pthread_detach(t[i]); 55 } 56 57 while (1) { 58 continue; 59 } 60 61 return 0; 62} 63 64NOINLINE int MultiThreadConstructorForThreadCrash(const int threadNum) 65{ 66 pthread_t t[threadNum]; 67 int threadID[threadNum]; 68 pthread_t threadCrash; 69 int threadIDCrash = threadNum; 70 71 for (int i = 0; i < threadNum; ++i) { 72 threadID[i] = i; 73 pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]); 74 pthread_detach(t[i]); 75 } 76 pthread_create(&threadCrash, NULL, (void *(*)(void *))CreateThreadForCrash, &threadIDCrash); 77 pthread_detach(threadCrash); 78 79 sleep(5); // 5 : wait 5s, then exit process 80 81 return 0; 82} 83 84NOINLINE int TestFunc70(void) 85{ 86 sleep(SLEEP_TIMEOUT); 87 return 0; 88} 89 90#ifndef UNITTEST 91int main(int argc, char* argv[]) 92{ 93 const int argumentLimit = 2; 94 if (argc != argumentLimit) { 95 printf("invalid input argument.\n"); 96 return 0; 97 } 98 MultiThreadConstructor(atoi(argv[1])); 99 return 0; 100} 101#endif 102 103// auto gen function 104GEN_TEST_FUNCTION(0, 1) 105GEN_TEST_FUNCTION(1, 2) 106GEN_TEST_FUNCTION(2, 3) 107GEN_TEST_FUNCTION(3, 4) 108GEN_TEST_FUNCTION(4, 5) 109GEN_TEST_FUNCTION(5, 6) 110GEN_TEST_FUNCTION(6, 7) 111GEN_TEST_FUNCTION(7, 8) 112GEN_TEST_FUNCTION(8, 9) 113GEN_TEST_FUNCTION(9, 10) 114 115GEN_TEST_FUNCTION(10, 11) 116GEN_TEST_FUNCTION(11, 12) 117GEN_TEST_FUNCTION(12, 13) 118GEN_TEST_FUNCTION(13, 14) 119GEN_TEST_FUNCTION(14, 15) 120GEN_TEST_FUNCTION(15, 16) 121GEN_TEST_FUNCTION(16, 17) 122GEN_TEST_FUNCTION(17, 18) 123GEN_TEST_FUNCTION(18, 19) 124GEN_TEST_FUNCTION(19, 20) 125 126GEN_TEST_FUNCTION(20, 21) 127GEN_TEST_FUNCTION(21, 22) 128GEN_TEST_FUNCTION(22, 23) 129GEN_TEST_FUNCTION(23, 24) 130GEN_TEST_FUNCTION(24, 25) 131GEN_TEST_FUNCTION(25, 26) 132GEN_TEST_FUNCTION(26, 27) 133GEN_TEST_FUNCTION(27, 28) 134GEN_TEST_FUNCTION(28, 29) 135GEN_TEST_FUNCTION(29, 30) 136 137GEN_TEST_FUNCTION(30, 31) 138GEN_TEST_FUNCTION(31, 32) 139GEN_TEST_FUNCTION(32, 33) 140GEN_TEST_FUNCTION(33, 34) 141GEN_TEST_FUNCTION(34, 35) 142GEN_TEST_FUNCTION(35, 36) 143GEN_TEST_FUNCTION(36, 37) 144GEN_TEST_FUNCTION(37, 38) 145GEN_TEST_FUNCTION(38, 39) 146GEN_TEST_FUNCTION(39, 40) 147 148GEN_TEST_FUNCTION(40, 41) 149GEN_TEST_FUNCTION(41, 42) 150GEN_TEST_FUNCTION(42, 43) 151GEN_TEST_FUNCTION(43, 44) 152GEN_TEST_FUNCTION(44, 45) 153GEN_TEST_FUNCTION(45, 46) 154GEN_TEST_FUNCTION(46, 47) 155GEN_TEST_FUNCTION(47, 48) 156GEN_TEST_FUNCTION(48, 49) 157GEN_TEST_FUNCTION(49, 50) 158 159GEN_TEST_FUNCTION(50, 51) 160GEN_TEST_FUNCTION(51, 52) 161GEN_TEST_FUNCTION(52, 53) 162GEN_TEST_FUNCTION(53, 54) 163GEN_TEST_FUNCTION(54, 55) 164GEN_TEST_FUNCTION(55, 56) 165GEN_TEST_FUNCTION(56, 57) 166GEN_TEST_FUNCTION(57, 58) 167GEN_TEST_FUNCTION(58, 59) 168GEN_TEST_FUNCTION(59, 60) 169 170GEN_TEST_FUNCTION(60, 61) 171GEN_TEST_FUNCTION(61, 62) 172GEN_TEST_FUNCTION(62, 63) 173GEN_TEST_FUNCTION(63, 64) 174GEN_TEST_FUNCTION(64, 65) 175GEN_TEST_FUNCTION(65, 66) 176GEN_TEST_FUNCTION(66, 67) 177GEN_TEST_FUNCTION(67, 68) 178GEN_TEST_FUNCTION(68, 69) 179GEN_TEST_FUNCTION(69, 70) 180