src/thread/irix/SDL_syssem.c
changeset 1361 19418e4422cb
child 1402 d910939febfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/thread/irix/SDL_syssem.c	Thu Feb 16 10:11:48 2006 +0000
     1.3 @@ -0,0 +1,235 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#include "SDL_thread.h"
    1.27 +#include "SDL_timer.h"
    1.28 +
    1.29 +
    1.30 +#include <stdio.h>
    1.31 +#include <stdlib.h>
    1.32 +#include <sys/types.h>
    1.33 +#include <sys/ipc.h>
    1.34 +#include <sys/sem.h>
    1.35 +#include <errno.h>
    1.36 +
    1.37 +#include "SDL_error.h"
    1.38 +#include "SDL_thread.h"
    1.39 +
    1.40 +
    1.41 +struct SDL_semaphore {
    1.42 +	int id;
    1.43 +};
    1.44 +
    1.45 +/* Not defined by many operating systems, use configure to detect */
    1.46 +/*
    1.47 +#if !defined(HAVE_SEMUN)
    1.48 +union semun {
    1.49 +	int val;
    1.50 +	struct semid_ds *buf;
    1.51 +	ushort *array;
    1.52 +};
    1.53 +#endif
    1.54 +*/
    1.55 +
    1.56 +static struct sembuf op_trywait[2] = {
    1.57 +	{ 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
    1.58 +};
    1.59 +static struct sembuf op_wait[2] = {
    1.60 +	{ 0, -1, SEM_UNDO }		/* Decrement semaphore */
    1.61 +};
    1.62 +static struct sembuf op_post[1] = {
    1.63 +	{ 0, 1, (IPC_NOWAIT|SEM_UNDO) }	/* Increment semaphore */
    1.64 +};
    1.65 +
    1.66 +/* Create a blockable semaphore */
    1.67 +SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.68 +{
    1.69 +	extern int _creating_thread_lock;	/* SDL_threads.c */
    1.70 +	SDL_sem *sem;
    1.71 +	union semun init;
    1.72 +	key_t key;
    1.73 +
    1.74 +	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
    1.75 +	if ( sem == NULL ) {
    1.76 +		SDL_OutOfMemory();
    1.77 +		return(NULL);
    1.78 +	}
    1.79 +	/* This flag is true if we are creating the thread manager sem,
    1.80 +	   which is never freed.  This allows us to reuse the same sem.
    1.81 +	*/
    1.82 +	if ( _creating_thread_lock ) {
    1.83 +		key = 'S'+'D'+'L';
    1.84 +	} else {
    1.85 +		key = IPC_PRIVATE;
    1.86 +	}
    1.87 +	/* Keep trying to create sem while we don't own the requested key */
    1.88 +	do {
    1.89 +		if ( key != IPC_PRIVATE ) {
    1.90 +			++key;
    1.91 +		}
    1.92 +		sem->id = semget(key, 1, (0600|IPC_CREAT));
    1.93 +	} while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES));
    1.94 +
    1.95 +	/* Report the error if we eventually failed */
    1.96 +	if ( sem->id < 0 ) {
    1.97 +		SDL_SetError("Couldn't create semaphore");
    1.98 +		SDL_free(sem);
    1.99 +		return(NULL);
   1.100 +	}
   1.101 +	init.val = initial_value;	/* Initialize semaphore */
   1.102 +	semctl(sem->id, 0, SETVAL, init);
   1.103 +	return(sem);
   1.104 +}
   1.105 +
   1.106 +void SDL_DestroySemaphore(SDL_sem *sem)
   1.107 +{
   1.108 +	if ( sem ) {
   1.109 +#ifdef _SGI_SOURCE
   1.110 +		semctl(sem->id, 0, IPC_RMID);
   1.111 +#else
   1.112 +		union semun dummy;
   1.113 +		dummy.val = 0;
   1.114 +		semctl(sem->id, 0, IPC_RMID, dummy);
   1.115 +#endif
   1.116 +		SDL_free(sem);
   1.117 +	}
   1.118 +}
   1.119 +
   1.120 +int SDL_SemTryWait(SDL_sem *sem)
   1.121 +{
   1.122 +	int retval;
   1.123 +
   1.124 +	if ( ! sem ) {
   1.125 +		SDL_SetError("Passed a NULL semaphore");
   1.126 +		return -1;
   1.127 +	}
   1.128 +
   1.129 +	retval = 0;
   1.130 +  tryagain:
   1.131 +	if ( semop(sem->id, op_trywait, 1) < 0 ) {
   1.132 +		if ( errno == EINTR ) {
   1.133 +			goto tryagain;
   1.134 +		}
   1.135 +		retval = SDL_MUTEX_TIMEDOUT;
   1.136 +	}
   1.137 +	return retval;
   1.138 +}
   1.139 +
   1.140 +int SDL_SemWait(SDL_sem *sem)
   1.141 +{
   1.142 +	int retval;
   1.143 +
   1.144 +	if ( ! sem ) {
   1.145 +		SDL_SetError("Passed a NULL semaphore");
   1.146 +		return -1;
   1.147 +	}
   1.148 +
   1.149 +	retval = 0;
   1.150 +  tryagain:
   1.151 +	if ( semop(sem->id, op_wait, 1) < 0 ) {
   1.152 +		if ( errno == EINTR ) {
   1.153 +			goto tryagain;
   1.154 +		}
   1.155 +		SDL_SetError("Semaphore operation error");
   1.156 +		retval = -1;
   1.157 +	}
   1.158 +	return retval;
   1.159 +}
   1.160 +
   1.161 +int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   1.162 +{
   1.163 +	int retval;
   1.164 +
   1.165 +	if ( ! sem ) {
   1.166 +		SDL_SetError("Passed a NULL semaphore");
   1.167 +		return -1;
   1.168 +	}
   1.169 +
   1.170 +	/* Try the easy cases first */
   1.171 +	if ( timeout == 0 ) {
   1.172 +		return SDL_SemTryWait(sem);
   1.173 +	}
   1.174 +	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   1.175 +		return SDL_SemWait(sem);
   1.176 +	}
   1.177 +
   1.178 +	/* Ack!  We have to busy wait... */
   1.179 +	timeout += SDL_GetTicks();
   1.180 +	do {
   1.181 +		retval = SDL_SemTryWait(sem);
   1.182 +		if ( retval == 0 ) {
   1.183 +			break;
   1.184 +		}
   1.185 +		SDL_Delay(1);
   1.186 +	} while ( SDL_GetTicks() < timeout );
   1.187 +
   1.188 +	return retval;
   1.189 +}
   1.190 +
   1.191 +Uint32 SDL_SemValue(SDL_sem *sem)
   1.192 +{
   1.193 +	int semval;
   1.194 +	Uint32 value;
   1.195 +	
   1.196 +	value = 0;
   1.197 +	if ( sem ) {
   1.198 +	  tryagain:
   1.199 +#ifdef _SGI_SOURCE
   1.200 +		semval = semctl(sem->id, 0, GETVAL);
   1.201 +#else
   1.202 +		{
   1.203 +		union semun arg;
   1.204 +		arg.val = 0;
   1.205 +		semval = semctl(sem->id, 0, GETVAL, arg);
   1.206 +		}
   1.207 +#endif
   1.208 +		if ( semval < 0 ) {
   1.209 +			if ( errno == EINTR ) {
   1.210 +				goto tryagain;
   1.211 +			}
   1.212 +		} else {
   1.213 +			value = (Uint32)semval;
   1.214 +		}
   1.215 +	}
   1.216 +	return value;
   1.217 +}
   1.218 +
   1.219 +int SDL_SemPost(SDL_sem *sem)
   1.220 +{
   1.221 +	int retval;
   1.222 +
   1.223 +	if ( ! sem ) {
   1.224 +		SDL_SetError("Passed a NULL semaphore");
   1.225 +		return -1;
   1.226 +	}
   1.227 +
   1.228 +	retval = 0;
   1.229 +  tryagain:
   1.230 +	if ( semop(sem->id, op_post, 1) < 0 ) {
   1.231 +		if ( errno == EINTR ) {
   1.232 +			goto tryagain;
   1.233 +		}
   1.234 +		SDL_SetError("Semaphore operation error");
   1.235 +		retval = -1;
   1.236 +	}
   1.237 +	return retval;
   1.238 +}