src/thread/riscos/SDL_sysmutex.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 implementations uses pthreads based on linux code */
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
#ifdef DISABLE_THREADS
slouken@955
    31
#include "../generic/SDL_sysmutex.c"
slouken@955
    32
#else
slouken@955
    33
slouken@955
    34
#include <stdlib.h>
slouken@955
    35
#include <stdio.h>
slouken@955
    36
#include <pthread.h>
slouken@955
    37
slouken@955
    38
#include "SDL_error.h"
slouken@955
    39
#include "SDL_thread.h"
slouken@955
    40
slouken@955
    41
struct SDL_mutex {
slouken@955
    42
	pthread_mutex_t id;
slouken@955
    43
#ifdef PTHREAD_NO_RECURSIVE_MUTEX
slouken@955
    44
	int recursive;
slouken@955
    45
	pthread_t owner;
slouken@955
    46
#endif
slouken@955
    47
};
slouken@955
    48
slouken@955
    49
SDL_mutex *SDL_CreateMutex (void)
slouken@955
    50
{
slouken@955
    51
	SDL_mutex *mutex;
slouken@955
    52
	pthread_mutexattr_t attr;
slouken@955
    53
slouken@955
    54
	/* Allocate the structure */
slouken@955
    55
	mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
slouken@955
    56
	if ( mutex ) {
slouken@955
    57
		pthread_mutexattr_init(&attr);
slouken@955
    58
#ifdef PTHREAD_NO_RECURSIVE_MUTEX
slouken@955
    59
		/* No extra attributes necessary */
slouken@955
    60
#else
slouken@955
    61
		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
slouken@955
    62
#endif /* PTHREAD_NO_RECURSIVE_MUTEX */
slouken@955
    63
		if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
slouken@955
    64
			SDL_SetError("pthread_mutex_init() failed");
slouken@955
    65
			free(mutex);
slouken@955
    66
			mutex = NULL;
slouken@955
    67
		}
slouken@955
    68
	} else {
slouken@955
    69
		SDL_OutOfMemory();
slouken@955
    70
	}
slouken@955
    71
	return(mutex);
slouken@955
    72
}
slouken@955
    73
slouken@955
    74
void SDL_DestroyMutex(SDL_mutex *mutex)
slouken@955
    75
{
slouken@955
    76
	if ( mutex ) {
slouken@955
    77
		pthread_mutex_destroy(&mutex->id);
slouken@955
    78
		free(mutex);
slouken@955
    79
	}
slouken@955
    80
}
slouken@955
    81
slouken@955
    82
/* Lock the mutex */
slouken@955
    83
int SDL_mutexP(SDL_mutex *mutex)
slouken@955
    84
{
slouken@955
    85
	int retval;
slouken@955
    86
#ifdef PTHREAD_NO_RECURSIVE_MUTEX
slouken@955
    87
	pthread_t this_thread;
slouken@955
    88
#endif
slouken@955
    89
slouken@955
    90
	if ( mutex == NULL ) {
slouken@955
    91
		SDL_SetError("Passed a NULL mutex");
slouken@955
    92
		return -1;
slouken@955
    93
	}
slouken@955
    94
slouken@955
    95
	retval = 0;
slouken@955
    96
#ifdef PTHREAD_NO_RECURSIVE_MUTEX
slouken@955
    97
	this_thread = pthread_self();
slouken@955
    98
	if ( mutex->owner == this_thread ) {
slouken@955
    99
		++mutex->recursive;
slouken@955
   100
	} else {
slouken@955
   101
		/* The order of operations is important.
slouken@955
   102
		   We set the locking thread id after we obtain the lock
slouken@955
   103
		   so unlocks from other threads will fail.
slouken@955
   104
		*/
slouken@955
   105
		if ( pthread_mutex_lock(&mutex->id) == 0 ) {
slouken@955
   106
			mutex->owner = this_thread;
slouken@955
   107
			mutex->recursive = 0;
slouken@955
   108
		} else {
slouken@955
   109
			SDL_SetError("pthread_mutex_lock() failed");
slouken@955
   110
			retval = -1;
slouken@955
   111
		}
slouken@955
   112
	}
slouken@955
   113
#else
slouken@955
   114
	if ( pthread_mutex_lock(&mutex->id) < 0 ) {
slouken@955
   115
		SDL_SetError("pthread_mutex_lock() failed");
slouken@955
   116
		retval = -1;
slouken@955
   117
	}
slouken@955
   118
#endif
slouken@955
   119
	return retval;
slouken@955
   120
}
slouken@955
   121
slouken@955
   122
int SDL_mutexV(SDL_mutex *mutex)
slouken@955
   123
{
slouken@955
   124
	int retval;
slouken@955
   125
slouken@955
   126
	if ( mutex == NULL ) {
slouken@955
   127
		SDL_SetError("Passed a NULL mutex");
slouken@955
   128
		return -1;
slouken@955
   129
	}
slouken@955
   130
slouken@955
   131
	retval = 0;
slouken@955
   132
#ifdef PTHREAD_NO_RECURSIVE_MUTEX
slouken@955
   133
	/* We can only unlock the mutex if we own it */
slouken@955
   134
	if ( pthread_self() == mutex->owner ) {
slouken@955
   135
		if ( mutex->recursive ) {
slouken@955
   136
			--mutex->recursive;
slouken@955
   137
		} else {
slouken@955
   138
			/* The order of operations is important.
slouken@955
   139
			   First reset the owner so another thread doesn't lock
slouken@955
   140
			   the mutex and set the ownership before we reset it,
slouken@955
   141
			   then release the lock semaphore.
slouken@955
   142
			 */
slouken@955
   143
			mutex->owner = 0;
slouken@955
   144
			pthread_mutex_unlock(&mutex->id);
slouken@955
   145
		}
slouken@955
   146
	} else {
slouken@955
   147
		SDL_SetError("mutex not owned by this thread");
slouken@955
   148
		retval = -1;
slouken@955
   149
	}
slouken@955
   150
slouken@955
   151
#else
slouken@955
   152
	if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
slouken@955
   153
		SDL_SetError("pthread_mutex_unlock() failed");
slouken@955
   154
		retval = -1;
slouken@955
   155
	}
slouken@955
   156
#endif /* PTHREAD_NO_RECURSIVE_MUTEX */
slouken@955
   157
slouken@955
   158
	return retval;
slouken@955
   159
}
slouken@955
   160
#endif