src/thread/pth/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 /*
    25  *	GNU pth threads
    26  *
    27  *	Patrice Mandin
    28  */
    29 
    30 #include <pth.h>
    31 #include <signal.h>
    32 
    33 #include "SDL_thread.h"
    34 #include "../SDL_thread_c.h"
    35 #include "../SDL_systhread.h"
    36 
    37 /* List of signals to mask in the subthreads */
    38 static int sig_list[] = {
    39     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    40     SIGVTALRM, SIGPROF, 0
    41 };
    42 
    43 static void *
    44 RunThread(void *data)
    45 {
    46     SDL_RunThread(data);
    47     pth_exit((void *) 0);
    48     return ((void *) 0);        /* Prevent compiler warning */
    49 }
    50 
    51 int
    52 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    53 {
    54     pth_attr_t type;
    55 
    56     /* Create a new attribute */
    57     type = pth_attr_new();
    58     if (type == NULL) {
    59         SDL_SetError("Couldn't initialize pth attributes");
    60         return (-1);
    61     }
    62     pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
    63 
    64     /* Create the thread and go! */
    65     thread->handle = pth_spawn(type, RunThread, args);
    66     if (thread->handle == NULL) {
    67         SDL_SetError("Not enough resources to create thread");
    68         return (-1);
    69     }
    70     return (0);
    71 }
    72 
    73 void
    74 SDL_SYS_SetupThread(void)
    75 {
    76     int i;
    77     sigset_t mask;
    78     int oldstate;
    79 
    80     /* Mask asynchronous signals for this thread */
    81     sigemptyset(&mask);
    82     for (i = 0; sig_list[i]; ++i) {
    83         sigaddset(&mask, sig_list[i]);
    84     }
    85     pth_sigmask(SIG_BLOCK, &mask, 0);
    86 
    87     /* Allow ourselves to be asynchronously cancelled */
    88     pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    89 }
    90 
    91 /* WARNING:  This may not work for systems with 64-bit pid_t */
    92 Uint32
    93 SDL_ThreadID(void)
    94 {
    95     return ((Uint32) pth_self());
    96 }
    97 
    98 void
    99 SDL_SYS_WaitThread(SDL_Thread * thread)
   100 {
   101     pth_join(thread->handle, NULL);
   102 }
   103 
   104 /* vi: set ts=4 sw=4 expandtab: */