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 <signal.h> 17#include <stdlib.h> 18#include <threads.h> 19#include "test.h" 20 21static int count = 0; 22const char *src = "Hello"; 23 24void exception_handler(int sig) 25{ 26 exit(t_status); 27} 28 29int threadfunc(void *arg) 30{ 31 signal(SIGSEGV, exception_handler); 32 33 count++; 34 thrd_exit(thrd_success); 35} 36 37/** 38 * @tc.name : thrd_create_0100 39 * @tc.desc : Creates a new thread executing the function func 40 * @tc.level : Level 0 41 */ 42void thrd_create_0100(void) 43{ 44 thrd_t id; 45 int result; 46 result = thrd_create(&id, threadfunc, NULL); 47 if (result != thrd_success) { 48 t_error("%s thrd_create failed", __func__); 49 } 50 51 result = thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL); 52 if (result != 0) { 53 t_error("%s thrd_sleep failed", __func__); 54 } 55 56 result = thrd_join(id, NULL); 57 if (result != thrd_success) { 58 t_error("%s thrd_join failed", __func__); 59 } 60 61 if (count != 1) { 62 t_error("%s failed, count is %d", __func__, count); 63 } 64 65 count = 0; 66} 67 68/** 69 * @tc.name : thrd_create_0200 70 * @tc.desc : Creates a new thread executing the function func(arg) 71 * @tc.level : Level 1 72 */ 73void thrd_create_0200(void) 74{ 75 thrd_t id; 76 int result; 77 result = thrd_create(&id, threadfunc, (char *)src); 78 if (result != thrd_success) { 79 t_error("%s thrd_create failed", __func__); 80 } 81 82 result = thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL); 83 if (result != 0) { 84 t_error("%s thrd_sleep failed", __func__); 85 } 86 87 result = thrd_join(id, NULL); 88 if (result != thrd_success) { 89 t_error("%s thrd_join failed", __func__); 90 } 91 92 if (count != 1) { 93 t_error("%s failed, count is %d", __func__, count); 94 } 95 count = 0; 96} 97 98/** 99 * @tc.name : thrd_create_0300 100 * @tc.desc : exception test 101 * @tc.level : Level 2 102 */ 103void thrd_create_0300(void) 104{ 105 thrd_create(NULL, threadfunc, NULL); 106} 107 108int main(int argc, char *argv[]) 109{ 110 thrd_create_0100(); 111 thrd_create_0200(); 112 thrd_create_0300(); 113 return t_status; 114}