src/thread/riscos/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1402 d910939febfa
child 2779 4436464c4f51
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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
    32 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    33 {
    34     SDL_SetError
    35         ("Threads have not been compiled into this version of the library");
    36     return (-1);
    37 }
    38 
    39 void
    40 SDL_SYS_SetupThread(void)
    41 {
    42     return;
    43 }
    44 
    45 Uint32
    46 SDL_ThreadID(void)
    47 {
    48     return (0);
    49 }
    50 
    51 void
    52 SDL_SYS_WaitThread(SDL_Thread * thread)
    53 {
    54     return;
    55 }
    56 
    57 void
    58 SDL_SYS_KillThread(SDL_Thread * thread)
    59 {
    60     return;
    61 }
    62 
    63 #else
    64 
    65 #include <signal.h>
    66 
    67 /* List of signals to mask in the subthreads */
    68 static int sig_list[] = {
    69     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    70     SIGVTALRM, SIGPROF, 0
    71 };
    72 
    73 #include <pthread.h>
    74 
    75 int riscos_using_threads = 0;
    76 Uint32 riscos_main_thread = 0;  /* Thread running events */
    77 
    78 static void *
    79 RunThread(void *data)
    80 {
    81     SDL_RunThread(data);
    82     pthread_exit((void *) 0);
    83     return ((void *) 0);        /* Prevent compiler warning */
    84 }
    85 
    86 int
    87 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    88 {
    89     pthread_attr_t type;
    90 
    91     /* Set the thread attributes */
    92     if (pthread_attr_init(&type) != 0) {
    93         SDL_SetError("Couldn't initialize pthread attributes");
    94         return (-1);
    95     }
    96     pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    97 
    98     /* Create the thread and go! */
    99     if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
   100         SDL_SetError("Not enough resources to create thread");
   101         return (-1);
   102     }
   103 
   104     if (riscos_using_threads == 0) {
   105         riscos_using_threads = 1;
   106         riscos_main_thread = SDL_ThreadID();
   107     }
   108 
   109     return (0);
   110 }
   111 
   112 void
   113 SDL_SYS_SetupThread(void)
   114 {
   115     int i;
   116     sigset_t mask;
   117 
   118     /* Mask asynchronous signals for this thread */
   119     sigemptyset(&mask);
   120     for (i = 0; sig_list[i]; ++i) {
   121         sigaddset(&mask, sig_list[i]);
   122     }
   123     pthread_sigmask(SIG_BLOCK, &mask, 0);
   124 
   125 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   126     /* Allow ourselves to be asynchronously cancelled */
   127     {
   128         int oldstate;
   129         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
   130     }
   131 #endif
   132 }
   133 
   134 Uint32
   135 SDL_ThreadID(void)
   136 {
   137     return ((Uint32) pthread_self());
   138 }
   139 
   140 void
   141 SDL_SYS_WaitThread(SDL_Thread * thread)
   142 {
   143     pthread_join(thread->handle, 0);
   144 }
   145 
   146 void
   147 SDL_SYS_KillThread(SDL_Thread * thread)
   148 {
   149 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   150     pthread_cancel(thread->handle);
   151 #else
   152     pthread_kill(thread->handle, SIGKILL);
   153 #endif
   154 }
   155 
   156 #endif
   157 /* vi: set ts=4 sw=4 expandtab: */