src/thread/riscos/SDL_syssem.c
changeset 955 d74fbf56f2f6
child 1099 1ccbb2b7d905
equal deleted inserted replaced
954:3acd16ea0180 955:d74fbf56f2f6
       
     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 doesn't have semiphores so use code based on generic implementation */
       
    24 
       
    25 #ifdef SAVE_RCSID
       
    26 static char rcsid =
       
    27  "@(#) $Id$";
       
    28 #endif
       
    29 
       
    30 /* An implementation of semaphores using mutexes and condition variables */
       
    31 
       
    32 #include <stdlib.h>
       
    33 
       
    34 #include "SDL_error.h"
       
    35 #include "SDL_timer.h"
       
    36 #include "SDL_thread.h"
       
    37 
       
    38 #include "SDL_systhread_c.h"
       
    39 
       
    40 #ifdef DISABLE_THREADS
       
    41 
       
    42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
       
    43 {
       
    44 	SDL_SetError("SDL not configured with thread support");
       
    45 	return (SDL_sem *)0;
       
    46 }
       
    47 
       
    48 void SDL_DestroySemaphore(SDL_sem *sem)
       
    49 {
       
    50 	return;
       
    51 }
       
    52 
       
    53 int SDL_SemTryWait(SDL_sem *sem)
       
    54 {
       
    55 	SDL_SetError("SDL not configured with thread support");
       
    56 	return -1;
       
    57 }
       
    58 
       
    59 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
       
    60 {
       
    61 	SDL_SetError("SDL not configured with thread support");
       
    62 	return -1;
       
    63 }
       
    64 
       
    65 int SDL_SemWait(SDL_sem *sem)
       
    66 {
       
    67 	SDL_SetError("SDL not configured with thread support");
       
    68 	return -1;
       
    69 }
       
    70 
       
    71 Uint32 SDL_SemValue(SDL_sem *sem)
       
    72 {
       
    73 	return 0;
       
    74 }
       
    75 
       
    76 int SDL_SemPost(SDL_sem *sem)
       
    77 {
       
    78 	SDL_SetError("SDL not configured with thread support");
       
    79 	return -1;
       
    80 }
       
    81 
       
    82 #else
       
    83 
       
    84 struct SDL_semaphore
       
    85 {
       
    86 	Uint32 count;
       
    87 	Uint32 waiters_count;
       
    88 	SDL_mutex *count_lock;
       
    89 	SDL_cond *count_nonzero;
       
    90 };
       
    91 
       
    92 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
       
    93 {
       
    94 	SDL_sem *sem;
       
    95 
       
    96 	sem = (SDL_sem *)malloc(sizeof(*sem));
       
    97 	if ( ! sem ) {
       
    98 		SDL_OutOfMemory();
       
    99 		return(0);
       
   100 	}
       
   101 	sem->count = initial_value;
       
   102 	sem->waiters_count = 0;
       
   103 
       
   104 	sem->count_lock = SDL_CreateMutex();
       
   105 	sem->count_nonzero = SDL_CreateCond();
       
   106 	if ( ! sem->count_lock || ! sem->count_nonzero ) {
       
   107 		SDL_DestroySemaphore(sem);
       
   108 		return(0);
       
   109 	}
       
   110 
       
   111 	return(sem);
       
   112 }
       
   113 
       
   114 /* WARNING:
       
   115    You cannot call this function when another thread is using the semaphore.
       
   116 */
       
   117 void SDL_DestroySemaphore(SDL_sem *sem)
       
   118 {
       
   119 	if ( sem ) {
       
   120 		sem->count = 0xFFFFFFFF;
       
   121 		while ( sem->waiters_count > 0) {
       
   122 			SDL_CondSignal(sem->count_nonzero);
       
   123 			SDL_Delay(10);
       
   124 		}
       
   125 		SDL_DestroyCond(sem->count_nonzero);
       
   126 		SDL_mutexP(sem->count_lock);
       
   127 		SDL_mutexV(sem->count_lock);
       
   128 		SDL_DestroyMutex(sem->count_lock);
       
   129 		free(sem);
       
   130 	}
       
   131 }
       
   132 
       
   133 int SDL_SemTryWait(SDL_sem *sem)
       
   134 {
       
   135 	int retval;
       
   136 
       
   137 	if ( ! sem ) {
       
   138 		SDL_SetError("Passed a NULL semaphore");
       
   139 		return -1;
       
   140 	}
       
   141 
       
   142 	retval = SDL_MUTEX_TIMEDOUT;
       
   143 	SDL_LockMutex(sem->count_lock);
       
   144 	if ( sem->count > 0 ) {
       
   145 		--sem->count;
       
   146 		retval = 0;
       
   147 	}
       
   148 	SDL_UnlockMutex(sem->count_lock);
       
   149 
       
   150 	return retval;
       
   151 }
       
   152 
       
   153 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
       
   154 {
       
   155 	int retval;
       
   156 
       
   157 	if ( ! sem ) {
       
   158 		SDL_SetError("Passed a NULL semaphore");
       
   159 		return -1;
       
   160 	}
       
   161 
       
   162 	/* A timeout of 0 is an easy case */
       
   163 	if ( timeout == 0 ) {
       
   164 		return SDL_SemTryWait(sem);
       
   165 	}
       
   166 
       
   167 	SDL_LockMutex(sem->count_lock);
       
   168 	++sem->waiters_count;
       
   169 	retval = 0;
       
   170 	while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
       
   171 		retval = SDL_CondWaitTimeout(sem->count_nonzero,
       
   172 		                             sem->count_lock, timeout);
       
   173 	}
       
   174 	--sem->waiters_count;
       
   175 	--sem->count;
       
   176 	SDL_UnlockMutex(sem->count_lock);
       
   177 
       
   178 	return retval;
       
   179 }
       
   180 
       
   181 int SDL_SemWait(SDL_sem *sem)
       
   182 {
       
   183 	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
       
   184 }
       
   185 
       
   186 Uint32 SDL_SemValue(SDL_sem *sem)
       
   187 {
       
   188 	Uint32 value;
       
   189 	
       
   190 	value = 0;
       
   191 	if ( sem ) {
       
   192 		SDL_LockMutex(sem->count_lock);
       
   193 		value = sem->count;
       
   194 		SDL_UnlockMutex(sem->count_lock);
       
   195 	}
       
   196 	return value;
       
   197 }
       
   198 
       
   199 int SDL_SemPost(SDL_sem *sem)
       
   200 {
       
   201 	if ( ! sem ) {
       
   202 		SDL_SetError("Passed a NULL semaphore");
       
   203 		return -1;
       
   204 	}
       
   205 
       
   206 	SDL_LockMutex(sem->count_lock);
       
   207 	if ( sem->waiters_count > 0 ) {
       
   208 		SDL_CondSignal(sem->count_nonzero);
       
   209 	}
       
   210 	++sem->count;
       
   211 	SDL_UnlockMutex(sem->count_lock);
       
   212 
       
   213 	return 0;
       
   214 }
       
   215 
       
   216 #endif /* DISABLE_THREADS */