src/thread/riscos/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 04:48:11 +0000
changeset 3578 0d1b16ee0bca
parent 2859 99210400e8b9
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #741

The thread ID is an unsigned long so it can hold pthread_t so people can do naughty things with it.

I'm going to be adding additional useful thread API functions, but this should prevent crashes in people's existing code on 64-bit architectures.
slouken@955
     1
/*
slouken@955
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@955
     4
slouken@955
     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@955
     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@955
     9
slouken@955
    10
    This library is distributed in the hope that it will be useful,
slouken@955
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@955
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@955
    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@955
    18
slouken@955
    19
    Sam Lantinga
slouken@955
    20
    slouken@libsdl.org
slouken@955
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@955
    23
slouken@955
    24
/* RISC OS version based on pthreads linux source */
slouken@955
    25
slouken@955
    26
#include "SDL_thread.h"
slouken@1361
    27
#include "../SDL_systhread.h"
slouken@955
    28
slouken@1361
    29
#if SDL_THREADS_DISABLED
slouken@955
    30
slouken@1895
    31
int
slouken@1895
    32
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@955
    33
{
slouken@1895
    34
    SDL_SetError
slouken@1895
    35
        ("Threads have not been compiled into this version of the library");
slouken@1895
    36
    return (-1);
slouken@955
    37
}
slouken@955
    38
slouken@1895
    39
void
slouken@1895
    40
SDL_SYS_SetupThread(void)
slouken@955
    41
{
slouken@1895
    42
    return;
slouken@955
    43
}
slouken@955
    44
slouken@3578
    45
SDL_threadID
slouken@1895
    46
SDL_ThreadID(void)
slouken@955
    47
{
slouken@1895
    48
    return (0);
slouken@955
    49
}
slouken@955
    50
slouken@1895
    51
void
slouken@1895
    52
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@955
    53
{
slouken@1895
    54
    return;
slouken@955
    55
}
slouken@955
    56
slouken@955
    57
#else
slouken@955
    58
slouken@955
    59
#include <signal.h>
slouken@955
    60
slouken@955
    61
/* List of signals to mask in the subthreads */
slouken@955
    62
static int sig_list[] = {
slouken@1895
    63
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@1895
    64
    SIGVTALRM, SIGPROF, 0
slouken@955
    65
};
slouken@955
    66
slouken@955
    67
#include <pthread.h>
slouken@955
    68
slouken@955
    69
int riscos_using_threads = 0;
slouken@1895
    70
Uint32 riscos_main_thread = 0;  /* Thread running events */
slouken@955
    71
slouken@1895
    72
static void *
slouken@1895
    73
RunThread(void *data)
slouken@955
    74
{
slouken@1895
    75
    SDL_RunThread(data);
slouken@1895
    76
    pthread_exit((void *) 0);
slouken@1895
    77
    return ((void *) 0);        /* Prevent compiler warning */
slouken@955
    78
}
slouken@955
    79
slouken@1895
    80
int
slouken@1895
    81
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@955
    82
{
slouken@1895
    83
    pthread_attr_t type;
slouken@955
    84
slouken@1895
    85
    /* Set the thread attributes */
slouken@1895
    86
    if (pthread_attr_init(&type) != 0) {
slouken@1895
    87
        SDL_SetError("Couldn't initialize pthread attributes");
slouken@1895
    88
        return (-1);
slouken@1895
    89
    }
slouken@1895
    90
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
slouken@955
    91
slouken@1895
    92
    /* Create the thread and go! */
slouken@1895
    93
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
slouken@1895
    94
        SDL_SetError("Not enough resources to create thread");
slouken@1895
    95
        return (-1);
slouken@1895
    96
    }
slouken@955
    97
slouken@1895
    98
    if (riscos_using_threads == 0) {
slouken@1895
    99
        riscos_using_threads = 1;
slouken@1895
   100
        riscos_main_thread = SDL_ThreadID();
slouken@1895
   101
    }
slouken@1895
   102
slouken@1895
   103
    return (0);
slouken@955
   104
}
slouken@955
   105
slouken@1895
   106
void
slouken@1895
   107
SDL_SYS_SetupThread(void)
slouken@955
   108
{
slouken@1895
   109
    int i;
slouken@1895
   110
    sigset_t mask;
slouken@955
   111
slouken@1895
   112
    /* Mask asynchronous signals for this thread */
slouken@1895
   113
    sigemptyset(&mask);
slouken@1895
   114
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
   115
        sigaddset(&mask, sig_list[i]);
slouken@1895
   116
    }
slouken@1895
   117
    pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@1895
   118
slouken@955
   119
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@1895
   120
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
   121
    {
slouken@1895
   122
        int oldstate;
slouken@1895
   123
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@1895
   124
    }
slouken@955
   125
#endif
slouken@955
   126
}
slouken@955
   127
slouken@1895
   128
Uint32
slouken@1895
   129
SDL_ThreadID(void)
slouken@955
   130
{
slouken@1895
   131
    return ((Uint32) pthread_self());
slouken@955
   132
}
slouken@955
   133
slouken@1895
   134
void
slouken@1895
   135
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@955
   136
{
slouken@1895
   137
    pthread_join(thread->handle, 0);
slouken@955
   138
}
slouken@955
   139
slouken@955
   140
#endif
slouken@1895
   141
/* vi: set ts=4 sw=4 expandtab: */