This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_systhread.c
114 lines (94 loc) · 2.86 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#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 */
32
static const 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
SDL_threadID riscos_main_thread = 0; /* Thread running events */
42
#endif
43
44
45
46
static void *
RunThread(void *data)
47
{
48
49
50
SDL_RunThread(data);
pthread_exit((void *) 0);
return ((void *) 0); /* Prevent compiler warning */
51
52
}
53
54
int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
55
{
56
57
58
59
60
61
62
63
64
65
66
67
68
69
pthread_attr_t type;
/* Set the thread attributes */
if (pthread_attr_init(&type) != 0) {
SDL_SetError("Couldn't initialize pthread attributes");
return (-1);
}
pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
/* Create the thread and go! */
if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
SDL_SetError("Not enough resources to create thread");
return (-1);
}
70
#ifdef __RISCOS__
71
72
73
74
if (riscos_using_threads == 0) {
riscos_using_threads = 1;
riscos_main_thread = SDL_ThreadID();
}
75
76
#endif
77
return (0);
78
79
}
80
81
void
SDL_SYS_SetupThread(void)
82
{
83
84
int i;
sigset_t mask;
85
86
87
88
89
90
91
/* Mask asynchronous signals for this thread */
sigemptyset(&mask);
for (i = 0; sig_list[i]; ++i) {
sigaddset(&mask, sig_list[i]);
}
pthread_sigmask(SIG_BLOCK, &mask, 0);
92
93
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
94
95
96
97
98
/* Allow ourselves to be asynchronously cancelled */
{
int oldstate;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
}
99
100
101
#endif
}
102
SDL_threadID
103
SDL_ThreadID(void)
104
{
105
return ((SDL_threadID) pthread_self());
106
107
}
108
109
void
SDL_SYS_WaitThread(SDL_Thread * thread)
110
{
111
pthread_join(thread->handle, 0);
112
113
}
114
/* vi: set ts=4 sw=4 expandtab: */