author  Ryan C. Gordon <icculus@icculus.org> 
Fri, 12 Aug 2016 19:59:00 0400  
changeset 10266  c09f06c4e8c8 
parent 9998  f67cf37e9cd4 
permissions  rwrr 
8582
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

1 
/* 
9998
f67cf37e9cd4
Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents:
9922
diff
changeset

2 
Copyright (C) 19972016 Sam Lantinga <slouken@libsdl.org> 
8582
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

3 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

4 
This software is provided 'asis', without any express or implied 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

5 
warranty. In no event will the authors be held liable for any damages 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

6 
arising from the use of this software. 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

7 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

8 
Permission is granted to anyone to use this software for any purpose, 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

9 
including commercial applications, and to alter it and redistribute it 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

10 
freely. 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

11 
*/ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

12 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

13 
/* Simple test of the SDL threading code */ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

14 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

15 
#include <stdio.h> 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

16 
#include <stdlib.h> 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

17 
#include <signal.h> 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

18 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

19 
#include "SDL.h" 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

20 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

21 
static SDL_TLSID tls; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

22 
static int alive = 0; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

23 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

24 
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

25 
static void 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

26 
quit(int rc) 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

27 
{ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

28 
SDL_Quit(); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

29 
exit(rc); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

30 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

31 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

32 
int SDLCALL 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

33 
ThreadFunc(void *data) 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

34 
{ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

35 
SDL_TLSSet(tls, "baby thread", NULL); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

36 
SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n", 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

37 
(char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls)); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

38 
while (alive) { 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

39 
SDL_Log("Thread '%s' is alive!\n", (char *) data); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

40 
SDL_Delay(1 * 1000); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

41 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

42 
SDL_Log("Thread '%s' exiting!\n", (char *) data); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

43 
return (0); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

44 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

45 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

46 
static void 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

47 
killed(int sig) 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

48 
{ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

49 
SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n"); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

50 
SDL_Delay(5 * 1000); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

51 
alive = 0; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

52 
quit(0); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

53 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

54 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

55 
int 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

56 
main(int argc, char *argv[]) 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

57 
{ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

58 
SDL_Thread *thread; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

59 

9922
b066d5fd5fa7
Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents:
9619
diff
changeset

60 
/* Enable standard application logging */ 
8582
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

61 
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

62 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

63 
/* Load the SDL library */ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

64 
if (SDL_Init(0) < 0) { 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

65 
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

66 
return (1); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

67 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

68 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

69 
tls = SDL_TLSCreate(); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

70 
SDL_assert(tls); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

71 
SDL_TLSSet(tls, "main thread", NULL); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

72 
SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls)); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

73 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

74 
alive = 1; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

75 
thread = SDL_CreateThread(ThreadFunc, "One", "#1"); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

76 
if (thread == NULL) { 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

77 
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError()); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

78 
quit(1); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

79 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

80 
SDL_Delay(5 * 1000); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

81 
SDL_Log("Waiting for thread #1\n"); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

82 
alive = 0; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

83 
SDL_WaitThread(thread, NULL); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

84 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

85 
SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls)); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

86 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

87 
alive = 1; 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

88 
signal(SIGTERM, killed); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

89 
thread = SDL_CreateThread(ThreadFunc, "Two", "#2"); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

90 
if (thread == NULL) { 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

91 
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError()); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

92 
quit(1); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

93 
} 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

94 
raise(SIGTERM); 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

95 

c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

96 
SDL_Quit(); /* Never reached */ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

97 
return (0); /* Never reached */ 
c3e9a2b93517
Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents:
8535
diff
changeset

98 
} 