src/thread/riscos/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Sep 2004 13:20:10 +0000
changeset 955 d74fbf56f2f6
child 1312 c9b51268668f
permissions -rw-r--r--
Date: Fri, 25 Jun 2004 13:29:15 +0100
From: "alan buckley"
Subject: Modification for RISC OS version of SDL

Ive attached a zip file with the changes to this email, it contains the
following:

The file sdldiff.txt is the output from cvs diff u. .
The directory thread/riscos contains all the new files to support threading.

Readme.riscos is a new readme file to add.
slouken@955
     1
/*
slouken@955
     2
    SDL - Simple DirectMedia Layer
slouken@955
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@955
     4
slouken@955
     5
    This library is free software; you can redistribute it and/or
slouken@955
     6
    modify it under the terms of the GNU Library General Public
slouken@955
     7
    License as published by the Free Software Foundation; either
slouken@955
     8
    version 2 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@955
    13
    Library General Public License for more details.
slouken@955
    14
slouken@955
    15
    You should have received a copy of the GNU Library General Public
slouken@955
    16
    License along with this library; if not, write to the Free
slouken@955
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@955
    18
slouken@955
    19
    Sam Lantinga
slouken@955
    20
    slouken@libsdl.org
slouken@955
    21
*/
slouken@955
    22
slouken@955
    23
/* RISC OS version based on pthreads linux source */
slouken@955
    24
slouken@955
    25
#ifdef SAVE_RCSID
slouken@955
    26
static char rcsid =
slouken@955
    27
 "@(#) $Id$";
slouken@955
    28
#endif
slouken@955
    29
slouken@955
    30
#include "SDL_error.h"
slouken@955
    31
#include "SDL_thread.h"
slouken@955
    32
#include "SDL_systhread.h"
slouken@955
    33
slouken@955
    34
#ifdef DISABLE_THREADS
slouken@955
    35
slouken@955
    36
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@955
    37
{
slouken@955
    38
	SDL_SetError("Threads have not been compiled into this version of the library");
slouken@955
    39
	return(-1);
slouken@955
    40
}
slouken@955
    41
slouken@955
    42
void SDL_SYS_SetupThread(void)
slouken@955
    43
{
slouken@955
    44
	return;
slouken@955
    45
}
slouken@955
    46
slouken@955
    47
Uint32 SDL_ThreadID(void)
slouken@955
    48
{
slouken@955
    49
	return(0);
slouken@955
    50
}
slouken@955
    51
slouken@955
    52
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@955
    53
{
slouken@955
    54
	return;
slouken@955
    55
}
slouken@955
    56
slouken@955
    57
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@955
    58
{
slouken@955
    59
	return;
slouken@955
    60
}
slouken@955
    61
slouken@955
    62
#else
slouken@955
    63
slouken@955
    64
#include <signal.h>
slouken@955
    65
slouken@955
    66
/* List of signals to mask in the subthreads */
slouken@955
    67
static int sig_list[] = {
slouken@955
    68
	SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@955
    69
	SIGVTALRM, SIGPROF, 0
slouken@955
    70
};
slouken@955
    71
slouken@955
    72
#include <pthread.h>
slouken@955
    73
slouken@955
    74
int riscos_using_threads = 0;
slouken@955
    75
Uint32 riscos_main_thread = 0; /* Thread running events */
slouken@955
    76
slouken@955
    77
static void *RunThread(void *data)
slouken@955
    78
{
slouken@955
    79
	SDL_RunThread(data);
slouken@955
    80
	pthread_exit((void*)0);
slouken@955
    81
	return((void *)0);		/* Prevent compiler warning */
slouken@955
    82
}
slouken@955
    83
slouken@955
    84
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@955
    85
{
slouken@955
    86
	pthread_attr_t type;
slouken@955
    87
slouken@955
    88
	/* Set the thread attributes */
slouken@955
    89
	if ( pthread_attr_init(&type) != 0 ) {
slouken@955
    90
		SDL_SetError("Couldn't initialize pthread attributes");
slouken@955
    91
		return(-1);
slouken@955
    92
	}
slouken@955
    93
	pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
slouken@955
    94
slouken@955
    95
	/* Create the thread and go! */
slouken@955
    96
	if ( pthread_create(&thread->handle, &type, RunThread, args) != 0 ) {
slouken@955
    97
		SDL_SetError("Not enough resources to create thread");
slouken@955
    98
		return(-1);
slouken@955
    99
	}
slouken@955
   100
slouken@955
   101
        if (riscos_using_threads == 0)
slouken@955
   102
        {
slouken@955
   103
           riscos_using_threads = 1;
slouken@955
   104
           riscos_main_thread = SDL_ThreadID();
slouken@955
   105
        }
slouken@955
   106
      
slouken@955
   107
	return(0);
slouken@955
   108
}
slouken@955
   109
slouken@955
   110
void SDL_SYS_SetupThread(void)
slouken@955
   111
{
slouken@955
   112
	int i;
slouken@955
   113
	sigset_t mask;
slouken@955
   114
slouken@955
   115
	/* Mask asynchronous signals for this thread */
slouken@955
   116
	sigemptyset(&mask);
slouken@955
   117
	for ( i=0; sig_list[i]; ++i ) {
slouken@955
   118
		sigaddset(&mask, sig_list[i]);
slouken@955
   119
	}
slouken@955
   120
	pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@955
   121
	
slouken@955
   122
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@955
   123
	/* Allow ourselves to be asynchronously cancelled */
slouken@955
   124
	{ int oldstate;
slouken@955
   125
		pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@955
   126
	}
slouken@955
   127
#endif
slouken@955
   128
}
slouken@955
   129
slouken@955
   130
Uint32 SDL_ThreadID(void)
slouken@955
   131
{
slouken@955
   132
	return((Uint32)pthread_self());
slouken@955
   133
}
slouken@955
   134
slouken@955
   135
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@955
   136
{
slouken@955
   137
	pthread_join(thread->handle, 0);
slouken@955
   138
}
slouken@955
   139
slouken@955
   140
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@955
   141
{
slouken@955
   142
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@955
   143
	pthread_cancel(thread->handle);
slouken@955
   144
#else
slouken@955
   145
	pthread_kill(thread->handle, SIGKILL);
slouken@955
   146
#endif
slouken@955
   147
}
slouken@955
   148
slouken@955
   149
#endif