SDL

Форк
0
/
testerror.c 
120 строк · 3.1 Кб
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 and error handling */
14

15
#include <stdlib.h>
16

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

21
static int alive = 0;
22

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

34
static int SDLCALL
35
ThreadFunc(void *data)
36
{
37
    /* Set the child thread error string */
38
    SDL_SetError("Thread %s (%" SDL_PRIu64 ") had a problem: %s",
39
                 (char *)data, SDL_GetCurrentThreadID(), "nevermind");
40
    while (alive) {
41
        SDL_Log("Thread '%s' is alive!\n", (char *)data);
42
        SDL_Delay(1 * 1000);
43
    }
44
    SDL_Log("Child thread error string: %s\n", SDL_GetError());
45
    return 0;
46
}
47

48
int main(int argc, char *argv[])
49
{
50
    SDL_Thread *thread;
51
    SDLTest_CommonState *state;
52
    int i;
53
    SDL_bool enable_threads = SDL_TRUE;
54

55
    /* Initialize test framework */
56
    state = SDLTest_CommonCreateState(argv, 0);
57
    if (!state) {
58
        return 1;
59
    }
60

61
    /* Enable standard application logging */
62
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
63

64
    /* Parse commandline */
65
    for (i = 1; i < argc;) {
66
        int consumed;
67

68
        consumed = SDLTest_CommonArg(state, i);
69
        if (consumed == 0) {
70
            consumed = -1;
71
            if (SDL_strcasecmp(argv[i], "--no-threads") == 0) {
72
                enable_threads = SDL_FALSE;
73
                consumed = 1;
74
            }
75
        }
76
        if (consumed < 0) {
77
            static const char *options[] = {
78
                "[--no-threads]",
79
                NULL
80
            };
81
            SDLTest_CommonLogUsage(state, argv[0], options);
82
            return 1;
83
        }
84
        i += consumed;
85
    }
86

87
    /* Load the SDL library */
88
    if (SDL_Init(0) < 0) {
89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
90
        return 1;
91
    }
92

93
    /* Set the error value for the main thread */
94
    SDL_SetError("No worries");
95

96
    if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
97
        SDL_Log("Not running slower tests");
98
        SDL_Quit();
99
        return 0;
100
    }
101

102
    if (enable_threads) {
103
        alive = 1;
104
        thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
105
        if (!thread) {
106
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
107
            quit(1);
108
        }
109
        SDL_Delay(5 * 1000);
110
        SDL_Log("Waiting for thread #1\n");
111
        alive = 0;
112
        SDL_WaitThread(thread, NULL);
113
    }
114

115
    SDL_Log("Main thread error string: %s\n", SDL_GetError());
116

117
    SDL_Quit();
118
    SDLTest_CommonDestroyState(state);
119
    return 0;
120
}
121

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

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

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

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