src/thread/riscos/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2779 4436464c4f51
child 3578 0d1b16ee0bca
permissions -rw-r--r--
Updated copyright date
     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
    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 #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 *
    73 RunThread(void *data)
    74 {
    75     SDL_RunThread(data);
    76     pthread_exit((void *) 0);
    77     return ((void *) 0);        /* Prevent compiler warning */
    78 }
    79 
    80 int
    81 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    82 {
    83     pthread_attr_t type;
    84 
    85     /* Set the thread attributes */
    86     if (pthread_attr_init(&type) != 0) {
    87         SDL_SetError("Couldn't initialize pthread attributes");
    88         return (-1);
    89     }
    90     pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    91 
    92     /* Create the thread and go! */
    93     if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
    94         SDL_SetError("Not enough resources to create thread");
    95         return (-1);
    96     }
    97 
    98     if (riscos_using_threads == 0) {
    99         riscos_using_threads = 1;
   100         riscos_main_thread = SDL_ThreadID();
   101     }
   102 
   103     return (0);
   104 }
   105 
   106 void
   107 SDL_SYS_SetupThread(void)
   108 {
   109     int i;
   110     sigset_t mask;
   111 
   112     /* Mask asynchronous signals for this thread */
   113     sigemptyset(&mask);
   114     for (i = 0; sig_list[i]; ++i) {
   115         sigaddset(&mask, sig_list[i]);
   116     }
   117     pthread_sigmask(SIG_BLOCK, &mask, 0);
   118 
   119 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   120     /* Allow ourselves to be asynchronously cancelled */
   121     {
   122         int oldstate;
   123         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
   124     }
   125 #endif
   126 }
   127 
   128 Uint32
   129 SDL_ThreadID(void)
   130 {
   131     return ((Uint32) pthread_self());
   132 }
   133 
   134 void
   135 SDL_SYS_WaitThread(SDL_Thread * thread)
   136 {
   137     pthread_join(thread->handle, 0);
   138 }
   139 
   140 #endif
   141 /* vi: set ts=4 sw=4 expandtab: */