SDL

Форк
0
/
torturethread.c 
129 строк · 3.2 Кб
1
/*
2
  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
3

4
  This software is provided 'as-is', without any express or implied
5
  warranty.  In no event will the authors be held liable for any damages
6
  arising from the use of this software.
7

8
  Permission is granted to anyone to use this software for any purpose,
9
  including commercial applications, and to alter it and redistribute it
10
  freely.
11
*/
12

13
/* Simple test of the SDL threading code */
14

15
#include <stdlib.h>
16
#include <signal.h>
17

18
#include <SDL3/SDL.h>
19
#include <SDL3/SDL_main.h>
20
#include <SDL3/SDL_test.h>
21

22
#define NUMTHREADS 10
23

24
static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS];
25

26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
27
static void
28
quit(int rc)
29
{
30
    SDL_Quit();
31
    /* Let 'main()' return normally */
32
    if (rc != 0) {
33
        exit(rc);
34
    }
35
}
36

37
static int SDLCALL
38
SubThreadFunc(void *data)
39
{
40
    while (!*(int volatile *)data) {
41
        SDL_Delay(10);
42
    }
43
    return 0;
44
}
45

46
static int SDLCALL
47
ThreadFunc(void *data)
48
{
49
    SDL_Thread *sub_threads[NUMTHREADS];
50
    int flags[NUMTHREADS];
51
    int i;
52
    int tid = (int)(uintptr_t)data;
53

54
    SDL_Log("Creating Thread %d\n", tid);
55

56
    for (i = 0; i < NUMTHREADS; i++) {
57
        char name[64];
58
        (void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
59
        flags[i] = 0;
60
        sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
61
    }
62

63
    SDL_Log("Thread '%d' waiting for signal\n", tid);
64
    while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
65
        ; /* do nothing */
66
    }
67

68
    SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
69
    for (i = 0; i < NUMTHREADS; i++) {
70
        flags[i] = 1;
71
        SDL_WaitThread(sub_threads[i], NULL);
72
    }
73

74
    SDL_Log("Thread '%d' exiting!\n", tid);
75

76
    return 0;
77
}
78

79
int main(int argc, char *argv[])
80
{
81
    SDL_Thread *threads[NUMTHREADS];
82
    int i;
83
    SDLTest_CommonState *state;
84

85
    /* Initialize test framework */
86
    state = SDLTest_CommonCreateState(argv, 0);
87
    if (!state) {
88
        return 1;
89
    }
90

91
    /* Enable standard application logging */
92
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
93

94
    /* Load the SDL library */
95
    if (SDL_Init(0) < 0) {
96
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
97
        return 1;
98
    }
99

100
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
101
        SDL_Quit();
102
        SDLTest_CommonDestroyState(state);
103
        return 1;
104
    }
105

106
    (void)signal(SIGSEGV, SIG_DFL);
107
    for (i = 0; i < NUMTHREADS; i++) {
108
        char name[64];
109
        (void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
110
        SDL_AtomicSet(&time_for_threads_to_die[i], 0);
111
        threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
112

113
        if (threads[i] == NULL) {
114
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
115
            quit(1);
116
        }
117
    }
118

119
    for (i = 0; i < NUMTHREADS; i++) {
120
        SDL_AtomicSet(&time_for_threads_to_die[i], 1);
121
    }
122

123
    for (i = 0; i < NUMTHREADS; i++) {
124
        SDL_WaitThread(threads[i], NULL);
125
    }
126
    SDL_Quit();
127
    SDLTest_CommonDestroyState(state);
128
    return 0;
129
}
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.