src/thread/pth/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1826 cf7528eded83
child 2779 4436464c4f51
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@185
     1
/*
slouken@185
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@185
     4
slouken@185
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@185
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@185
     9
slouken@185
    10
    This library is distributed in the hope that it will be useful,
slouken@185
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@185
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@185
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@185
    18
slouken@185
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@185
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@185
    23
slouken@329
    24
/*
slouken@329
    25
 *	GNU pth threads
slouken@329
    26
 *
slouken@329
    27
 *	Patrice Mandin
slouken@329
    28
 */
slouken@185
    29
slouken@1361
    30
#include <pth.h>
slouken@185
    31
#include <signal.h>
slouken@185
    32
patmandin@1826
    33
#include "SDL_thread.h"
patmandin@1826
    34
#include "../SDL_thread_c.h"
patmandin@1826
    35
#include "../SDL_systhread.h"
patmandin@1826
    36
slouken@185
    37
/* List of signals to mask in the subthreads */
slouken@185
    38
static int sig_list[] = {
slouken@1895
    39
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@1895
    40
    SIGVTALRM, SIGPROF, 0
slouken@185
    41
};
slouken@185
    42
slouken@1895
    43
static void *
slouken@1895
    44
RunThread(void *data)
slouken@185
    45
{
slouken@1895
    46
    SDL_RunThread(data);
slouken@1895
    47
    pth_exit((void *) 0);
slouken@1895
    48
    return ((void *) 0);        /* Prevent compiler warning */
slouken@185
    49
}
slouken@185
    50
slouken@1895
    51
int
slouken@1895
    52
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@185
    53
{
slouken@1895
    54
    pth_attr_t type;
slouken@185
    55
slouken@1895
    56
    /* Create a new attribute */
slouken@1895
    57
    type = pth_attr_new();
slouken@1895
    58
    if (type == NULL) {
slouken@1895
    59
        SDL_SetError("Couldn't initialize pth attributes");
slouken@1895
    60
        return (-1);
slouken@1895
    61
    }
slouken@1895
    62
    pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
slouken@185
    63
slouken@1895
    64
    /* Create the thread and go! */
slouken@1895
    65
    thread->handle = pth_spawn(type, RunThread, args);
slouken@1895
    66
    if (thread->handle == NULL) {
slouken@1895
    67
        SDL_SetError("Not enough resources to create thread");
slouken@1895
    68
        return (-1);
slouken@1895
    69
    }
slouken@1895
    70
    return (0);
slouken@185
    71
}
slouken@185
    72
slouken@1895
    73
void
slouken@1895
    74
SDL_SYS_SetupThread(void)
slouken@185
    75
{
slouken@1895
    76
    int i;
slouken@1895
    77
    sigset_t mask;
slouken@1895
    78
    int oldstate;
slouken@185
    79
slouken@1895
    80
    /* Mask asynchronous signals for this thread */
slouken@1895
    81
    sigemptyset(&mask);
slouken@1895
    82
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
    83
        sigaddset(&mask, sig_list[i]);
slouken@1895
    84
    }
slouken@1895
    85
    pth_sigmask(SIG_BLOCK, &mask, 0);
slouken@185
    86
slouken@1895
    87
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
    88
    pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@185
    89
}
slouken@185
    90
slouken@185
    91
/* WARNING:  This may not work for systems with 64-bit pid_t */
slouken@1895
    92
Uint32
slouken@1895
    93
SDL_ThreadID(void)
slouken@185
    94
{
slouken@1895
    95
    return ((Uint32) pth_self());
slouken@185
    96
}
slouken@185
    97
slouken@1895
    98
void
slouken@1895
    99
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@185
   100
{
slouken@1895
   101
    pth_join(thread->handle, NULL);
slouken@185
   102
}
slouken@185
   103
slouken@1895
   104
void
slouken@1895
   105
SDL_SYS_KillThread(SDL_Thread * thread)
slouken@185
   106
{
slouken@1895
   107
    pth_cancel(thread->handle);
slouken@1895
   108
    pth_join(thread->handle, NULL);
slouken@185
   109
}
slouken@1895
   110
slouken@1895
   111
/* vi: set ts=4 sw=4 expandtab: */