/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "multithread_constructor.h"

#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "stdio.h"
#include "stdlib.h"

const static unsigned int SLEEP_TIMEOUT = 360000;

static void CreateThread(int *argv)
{
    int threadID = *argv;
    printf("create MultiThread %d\n", threadID);
    TestFunc1();
    return;
}

static void CreateThreadForCrash(const int *argv)
{
    int threadID = *argv;
    printf("create ThreadForCrash %d\n", threadID);
    int ret = raise(SIGSEGV);
    if (ret != 0) {
        printf("remove failed!");
    }
    return;
}

NOINLINE int MultiThreadConstructor(const int threadNum)
{
    pthread_t t[threadNum];
    int threadID[threadNum];

    for (int i = 0; i < threadNum; ++i) {
        threadID[i] = i;
        pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
        pthread_detach(t[i]);
    }

    while (1) {
        continue;
    }

    return 0;
}

NOINLINE int MultiThreadConstructorForThreadCrash(const int threadNum)
{
    pthread_t t[threadNum];
    int threadID[threadNum];
    pthread_t threadCrash;
    int threadIDCrash = threadNum;

    for (int i = 0; i < threadNum; ++i) {
        threadID[i] = i;
        pthread_create(&t[i], NULL, (void *(*)(void *))CreateThread, &threadID[i]);
        pthread_detach(t[i]);
    }
    pthread_create(&threadCrash, NULL, (void *(*)(void *))CreateThreadForCrash, &threadIDCrash);
    pthread_detach(threadCrash);

    sleep(5); // 5 : wait 5s, then exit process

    return 0;
}

NOINLINE int TestFunc70(void)
{
    sleep(SLEEP_TIMEOUT);
    return 0;
}

#ifndef UNITTEST
int main(int argc, char* argv[])
{
    const int argumentLimit = 2;
    if (argc != argumentLimit) {
        printf("invalid input argument.\n");
        return 0;
    }
    MultiThreadConstructor(atoi(argv[1]));
    return 0;
}
#endif

// auto gen function
GEN_TEST_FUNCTION(0, 1)
GEN_TEST_FUNCTION(1, 2)
GEN_TEST_FUNCTION(2, 3)
GEN_TEST_FUNCTION(3, 4)
GEN_TEST_FUNCTION(4, 5)
GEN_TEST_FUNCTION(5, 6)
GEN_TEST_FUNCTION(6, 7)
GEN_TEST_FUNCTION(7, 8)
GEN_TEST_FUNCTION(8, 9)
GEN_TEST_FUNCTION(9, 10)

GEN_TEST_FUNCTION(10, 11)
GEN_TEST_FUNCTION(11, 12)
GEN_TEST_FUNCTION(12, 13)
GEN_TEST_FUNCTION(13, 14)
GEN_TEST_FUNCTION(14, 15)
GEN_TEST_FUNCTION(15, 16)
GEN_TEST_FUNCTION(16, 17)
GEN_TEST_FUNCTION(17, 18)
GEN_TEST_FUNCTION(18, 19)
GEN_TEST_FUNCTION(19, 20)

GEN_TEST_FUNCTION(20, 21)
GEN_TEST_FUNCTION(21, 22)
GEN_TEST_FUNCTION(22, 23)
GEN_TEST_FUNCTION(23, 24)
GEN_TEST_FUNCTION(24, 25)
GEN_TEST_FUNCTION(25, 26)
GEN_TEST_FUNCTION(26, 27)
GEN_TEST_FUNCTION(27, 28)
GEN_TEST_FUNCTION(28, 29)
GEN_TEST_FUNCTION(29, 30)

GEN_TEST_FUNCTION(30, 31)
GEN_TEST_FUNCTION(31, 32)
GEN_TEST_FUNCTION(32, 33)
GEN_TEST_FUNCTION(33, 34)
GEN_TEST_FUNCTION(34, 35)
GEN_TEST_FUNCTION(35, 36)
GEN_TEST_FUNCTION(36, 37)
GEN_TEST_FUNCTION(37, 38)
GEN_TEST_FUNCTION(38, 39)
GEN_TEST_FUNCTION(39, 40)

GEN_TEST_FUNCTION(40, 41)
GEN_TEST_FUNCTION(41, 42)
GEN_TEST_FUNCTION(42, 43)
GEN_TEST_FUNCTION(43, 44)
GEN_TEST_FUNCTION(44, 45)
GEN_TEST_FUNCTION(45, 46)
GEN_TEST_FUNCTION(46, 47)
GEN_TEST_FUNCTION(47, 48)
GEN_TEST_FUNCTION(48, 49)
GEN_TEST_FUNCTION(49, 50)

GEN_TEST_FUNCTION(50, 51)
GEN_TEST_FUNCTION(51, 52)
GEN_TEST_FUNCTION(52, 53)
GEN_TEST_FUNCTION(53, 54)
GEN_TEST_FUNCTION(54, 55)
GEN_TEST_FUNCTION(55, 56)
GEN_TEST_FUNCTION(56, 57)
GEN_TEST_FUNCTION(57, 58)
GEN_TEST_FUNCTION(58, 59)
GEN_TEST_FUNCTION(59, 60)

GEN_TEST_FUNCTION(60, 61)
GEN_TEST_FUNCTION(61, 62)
GEN_TEST_FUNCTION(62, 63)
GEN_TEST_FUNCTION(63, 64)
GEN_TEST_FUNCTION(64, 65)
GEN_TEST_FUNCTION(65, 66)
GEN_TEST_FUNCTION(66, 67)
GEN_TEST_FUNCTION(67, 68)
GEN_TEST_FUNCTION(68, 69)
GEN_TEST_FUNCTION(69, 70)