This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_systhread.c
128 lines (107 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
26
27
28
29
30
31
32
#include <pthread.h>
#include <signal.h>
#include "SDL_thread.h"
#include "../SDL_thread_c.h"
#include "../SDL_systhread.h"
/* List of signals to mask in the subthreads */
static int sig_list[] = {
33
34
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
SIGVTALRM, SIGPROF, 0
35
36
};
37
38
39
40
#ifdef __RISCOS__
/* RISC OS needs to know the main thread for
* it's timer and event processing. */
int riscos_using_threads = 0;
41
Uint32 riscos_main_thread = 0; /* Thread running events */
42
#endif
43
44
45
static void *
46
RunThread(void *data)
47
{
48
49
SDL_RunThread(data);
pthread_exit((void *) 0);
50
return ((void *) 0); /* Prevent compiler warning */
51
52
}
53
int
54
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
55
{
56
57
58
pthread_attr_t type;
/* Set the thread attributes */
59
60
if (pthread_attr_init(&type) != 0) {
SDL_SetError("Couldn't initialize pthread attributes");
61
62
return (-1);
}
63
pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
64
65
/* Create the thread and go! */
66
67
if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
SDL_SetError("Not enough resources to create thread");
68
69
return (-1);
}
70
#ifdef __RISCOS__
71
72
if (riscos_using_threads == 0) {
riscos_using_threads = 1;
73
riscos_main_thread = SDL_ThreadID();
74
}
75
76
#endif
77
return (0);
78
79
}
80
void
81
SDL_SYS_SetupThread(void)
82
{
83
84
int i;
sigset_t mask;
85
86
/* Mask asynchronous signals for this thread */
87
sigemptyset(&mask);
88
for (i = 0; sig_list[i]; ++i) {
89
sigaddset(&mask, sig_list[i]);
90
}
91
pthread_sigmask(SIG_BLOCK, &mask, 0);
92
93
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
94
95
96
/* Allow ourselves to be asynchronously cancelled */
{
int oldstate;
97
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
98
}
99
100
101
102
#endif
}
/* WARNING: This may not work for systems with 64-bit pid_t */
103
Uint32
104
SDL_ThreadID(void)
105
{
106
return ((Uint32) pthread_self());
107
108
}
109
void
110
SDL_SYS_WaitThread(SDL_Thread * thread)
111
{
112
pthread_join(thread->handle, 0);
113
114
}
115
void
116
SDL_SYS_KillThread(SDL_Thread * thread)
117
118
{
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
119
pthread_cancel(thread->handle);
120
#else
121
#ifdef __FREEBSD__
122
123
#warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
#endif
124
pthread_kill(thread->handle, SIGKILL);
125
126
#endif
}
127
128
/* vi: set ts=4 sw=4 expandtab: */