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.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* RISC OS implementations uses pthreads based on linux code */
    24 
    25 #ifdef SAVE_RCSID
    26 static char rcsid =
    27  "@(#) $Id$";
    28 #endif
    29 
    30 #ifdef DISABLE_THREADS
    31 #include "../generic/SDL_sysmutex.c"
    32 #else
    33 
    34 #include <stdlib.h>
    35 #include <stdio.h>
    36 #include <pthread.h>
    37 
    38 #include "SDL_error.h"
    39 #include "SDL_thread.h"
    40 
    41 struct SDL_mutex {
    42 	pthread_mutex_t id;
    43 #ifdef PTHREAD_NO_RECURSIVE_MUTEX
    44 	int recursive;
    45 	pthread_t owner;
    46 #endif
    47 };
    48 
    49 SDL_mutex *SDL_CreateMutex (void)
    50 {
    51 	SDL_mutex *mutex;
    52 	pthread_mutexattr_t attr;
    53 
    54 	/* Allocate the structure */
    55 	mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
    56 	if ( mutex ) {
    57 		pthread_mutexattr_init(&attr);
    58 #ifdef PTHREAD_NO_RECURSIVE_MUTEX
    59 		/* No extra attributes necessary */
    60 #else
    61 		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    62 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */
    63 		if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
    64 			SDL_SetError("pthread_mutex_init() failed");
    65 			free(mutex);
    66 			mutex = NULL;
    67 		}
    68 	} else {
    69 		SDL_OutOfMemory();
    70 	}
    71 	return(mutex);
    72 }
    73 
    74 void SDL_DestroyMutex(SDL_mutex *mutex)
    75 {
    76 	if ( mutex ) {
    77 		pthread_mutex_destroy(&mutex->id);
    78 		free(mutex);
    79 	}
    80 }
    81 
    82 /* Lock the mutex */
    83 int SDL_mutexP(SDL_mutex *mutex)
    84 {
    85 	int retval;
    86 #ifdef PTHREAD_NO_RECURSIVE_MUTEX
    87 	pthread_t this_thread;
    88 #endif
    89 
    90 	if ( mutex == NULL ) {
    91 		SDL_SetError("Passed a NULL mutex");
    92 		return -1;
    93 	}
    94 
    95 	retval = 0;
    96 #ifdef PTHREAD_NO_RECURSIVE_MUTEX
    97 	this_thread = pthread_self();
    98 	if ( mutex->owner == this_thread ) {
    99 		++mutex->recursive;
   100 	} else {
   101 		/* The order of operations is important.
   102 		   We set the locking thread id after we obtain the lock
   103 		   so unlocks from other threads will fail.
   104 		*/
   105 		if ( pthread_mutex_lock(&mutex->id) == 0 ) {
   106 			mutex->owner = this_thread;
   107 			mutex->recursive = 0;
   108 		} else {
   109 			SDL_SetError("pthread_mutex_lock() failed");
   110 			retval = -1;
   111 		}
   112 	}
   113 #else
   114 	if ( pthread_mutex_lock(&mutex->id) < 0 ) {
   115 		SDL_SetError("pthread_mutex_lock() failed");
   116 		retval = -1;
   117 	}
   118 #endif
   119 	return retval;
   120 }
   121 
   122 int SDL_mutexV(SDL_mutex *mutex)
   123 {
   124 	int retval;
   125 
   126 	if ( mutex == NULL ) {
   127 		SDL_SetError("Passed a NULL mutex");
   128 		return -1;
   129 	}
   130 
   131 	retval = 0;
   132 #ifdef PTHREAD_NO_RECURSIVE_MUTEX
   133 	/* We can only unlock the mutex if we own it */
   134 	if ( pthread_self() == mutex->owner ) {
   135 		if ( mutex->recursive ) {
   136 			--mutex->recursive;
   137 		} else {
   138 			/* The order of operations is important.
   139 			   First reset the owner so another thread doesn't lock
   140 			   the mutex and set the ownership before we reset it,
   141 			   then release the lock semaphore.
   142 			 */
   143 			mutex->owner = 0;
   144 			pthread_mutex_unlock(&mutex->id);
   145 		}
   146 	} else {
   147 		SDL_SetError("mutex not owned by this thread");
   148 		retval = -1;
   149 	}
   150 
   151 #else
   152 	if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
   153 		SDL_SetError("pthread_mutex_unlock() failed");
   154 		retval = -1;
   155 	}
   156 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */
   157 
   158 	return retval;
   159 }
   160 #endif