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