• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 const static unsigned int SLEEP_TIMEOUT = 360000;
26 
CreateThread(int * argv)27 static void CreateThread(int *argv)
28 {
29     int threadID = *argv;
30     printf("create MultiThread %d\n", threadID);
31     TestFunc1();
32     return;
33 }
34 
CreateThreadForCrash(const int * argv)35 static 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 
MultiThreadConstructor(const int threadNum)46 NOINLINE 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 
MultiThreadConstructorForThreadCrash(const int threadNum)64 NOINLINE 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 
TestFunc70(void)84 NOINLINE int TestFunc70(void)
85 {
86     sleep(SLEEP_TIMEOUT);
87     return 0;
88 }
89 
90 #ifndef UNITTEST
main(int argc,char * argv[])91 int 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