src/thread/irix/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1361 19418e4422cb
child 1499 ad887c988713
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@1361
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1361
    23
slouken@1361
    24
#include "SDL_thread.h"
slouken@1361
    25
#include "SDL_timer.h"
slouken@1361
    26
slouken@1361
    27
slouken@1361
    28
#include <stdio.h>
slouken@1361
    29
#include <stdlib.h>
slouken@1361
    30
#include <sys/types.h>
slouken@1361
    31
#include <sys/ipc.h>
slouken@1361
    32
#include <sys/sem.h>
slouken@1361
    33
#include <errno.h>
slouken@1361
    34
slouken@1361
    35
#include "SDL_error.h"
slouken@1361
    36
#include "SDL_thread.h"
slouken@1361
    37
slouken@1361
    38
slouken@1361
    39
struct SDL_semaphore {
slouken@1361
    40
	int id;
slouken@1361
    41
};
slouken@1361
    42
slouken@1361
    43
/* Not defined by many operating systems, use configure to detect */
slouken@1361
    44
/*
slouken@1361
    45
#if !defined(HAVE_SEMUN)
slouken@1361
    46
union semun {
slouken@1361
    47
	int val;
slouken@1361
    48
	struct semid_ds *buf;
slouken@1361
    49
	ushort *array;
slouken@1361
    50
};
slouken@1361
    51
#endif
slouken@1361
    52
*/
slouken@1361
    53
slouken@1361
    54
static struct sembuf op_trywait[2] = {
slouken@1361
    55
	{ 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
slouken@1361
    56
};
slouken@1361
    57
static struct sembuf op_wait[2] = {
slouken@1361
    58
	{ 0, -1, SEM_UNDO }		/* Decrement semaphore */
slouken@1361
    59
};
slouken@1361
    60
static struct sembuf op_post[1] = {
slouken@1361
    61
	{ 0, 1, (IPC_NOWAIT|SEM_UNDO) }	/* Increment semaphore */
slouken@1361
    62
};
slouken@1361
    63
slouken@1361
    64
/* Create a blockable semaphore */
slouken@1361
    65
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    66
{
slouken@1361
    67
	extern int _creating_thread_lock;	/* SDL_threads.c */
slouken@1361
    68
	SDL_sem *sem;
slouken@1361
    69
	union semun init;
slouken@1361
    70
	key_t key;
slouken@1361
    71
slouken@1361
    72
	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
slouken@1361
    73
	if ( sem == NULL ) {
slouken@1361
    74
		SDL_OutOfMemory();
slouken@1361
    75
		return(NULL);
slouken@1361
    76
	}
slouken@1361
    77
	/* This flag is true if we are creating the thread manager sem,
slouken@1361
    78
	   which is never freed.  This allows us to reuse the same sem.
slouken@1361
    79
	*/
slouken@1361
    80
	if ( _creating_thread_lock ) {
slouken@1361
    81
		key = 'S'+'D'+'L';
slouken@1361
    82
	} else {
slouken@1361
    83
		key = IPC_PRIVATE;
slouken@1361
    84
	}
slouken@1361
    85
	/* Keep trying to create sem while we don't own the requested key */
slouken@1361
    86
	do {
slouken@1361
    87
		if ( key != IPC_PRIVATE ) {
slouken@1361
    88
			++key;
slouken@1361
    89
		}
slouken@1361
    90
		sem->id = semget(key, 1, (0600|IPC_CREAT));
slouken@1361
    91
	} while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES));
slouken@1361
    92
slouken@1361
    93
	/* Report the error if we eventually failed */
slouken@1361
    94
	if ( sem->id < 0 ) {
slouken@1361
    95
		SDL_SetError("Couldn't create semaphore");
slouken@1361
    96
		SDL_free(sem);
slouken@1361
    97
		return(NULL);
slouken@1361
    98
	}
slouken@1361
    99
	init.val = initial_value;	/* Initialize semaphore */
slouken@1361
   100
	semctl(sem->id, 0, SETVAL, init);
slouken@1361
   101
	return(sem);
slouken@1361
   102
}
slouken@1361
   103
slouken@1361
   104
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
   105
{
slouken@1361
   106
	if ( sem ) {
slouken@1402
   107
#ifdef __IRIX__
slouken@1361
   108
		semctl(sem->id, 0, IPC_RMID);
slouken@1361
   109
#else
slouken@1361
   110
		union semun dummy;
slouken@1361
   111
		dummy.val = 0;
slouken@1361
   112
		semctl(sem->id, 0, IPC_RMID, dummy);
slouken@1361
   113
#endif
slouken@1361
   114
		SDL_free(sem);
slouken@1361
   115
	}
slouken@1361
   116
}
slouken@1361
   117
slouken@1361
   118
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
   119
{
slouken@1361
   120
	int retval;
slouken@1361
   121
slouken@1361
   122
	if ( ! sem ) {
slouken@1361
   123
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   124
		return -1;
slouken@1361
   125
	}
slouken@1361
   126
slouken@1361
   127
	retval = 0;
slouken@1361
   128
  tryagain:
slouken@1361
   129
	if ( semop(sem->id, op_trywait, 1) < 0 ) {
slouken@1361
   130
		if ( errno == EINTR ) {
slouken@1361
   131
			goto tryagain;
slouken@1361
   132
		}
slouken@1361
   133
		retval = SDL_MUTEX_TIMEDOUT;
slouken@1361
   134
	}
slouken@1361
   135
	return retval;
slouken@1361
   136
}
slouken@1361
   137
slouken@1361
   138
int SDL_SemWait(SDL_sem *sem)
slouken@1361
   139
{
slouken@1361
   140
	int retval;
slouken@1361
   141
slouken@1361
   142
	if ( ! sem ) {
slouken@1361
   143
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   144
		return -1;
slouken@1361
   145
	}
slouken@1361
   146
slouken@1361
   147
	retval = 0;
slouken@1361
   148
  tryagain:
slouken@1361
   149
	if ( semop(sem->id, op_wait, 1) < 0 ) {
slouken@1361
   150
		if ( errno == EINTR ) {
slouken@1361
   151
			goto tryagain;
slouken@1361
   152
		}
slouken@1361
   153
		SDL_SetError("Semaphore operation error");
slouken@1361
   154
		retval = -1;
slouken@1361
   155
	}
slouken@1361
   156
	return retval;
slouken@1361
   157
}
slouken@1361
   158
slouken@1361
   159
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
   160
{
slouken@1361
   161
	int retval;
slouken@1361
   162
slouken@1361
   163
	if ( ! sem ) {
slouken@1361
   164
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   165
		return -1;
slouken@1361
   166
	}
slouken@1361
   167
slouken@1361
   168
	/* Try the easy cases first */
slouken@1361
   169
	if ( timeout == 0 ) {
slouken@1361
   170
		return SDL_SemTryWait(sem);
slouken@1361
   171
	}
slouken@1361
   172
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   173
		return SDL_SemWait(sem);
slouken@1361
   174
	}
slouken@1361
   175
slouken@1361
   176
	/* Ack!  We have to busy wait... */
slouken@1361
   177
	timeout += SDL_GetTicks();
slouken@1361
   178
	do {
slouken@1361
   179
		retval = SDL_SemTryWait(sem);
slouken@1361
   180
		if ( retval == 0 ) {
slouken@1361
   181
			break;
slouken@1361
   182
		}
slouken@1361
   183
		SDL_Delay(1);
slouken@1361
   184
	} while ( SDL_GetTicks() < timeout );
slouken@1361
   185
slouken@1361
   186
	return retval;
slouken@1361
   187
}
slouken@1361
   188
slouken@1361
   189
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   190
{
slouken@1361
   191
	int semval;
slouken@1361
   192
	Uint32 value;
slouken@1361
   193
	
slouken@1361
   194
	value = 0;
slouken@1361
   195
	if ( sem ) {
slouken@1361
   196
	  tryagain:
slouken@1402
   197
#ifdef __IRIX__
slouken@1361
   198
		semval = semctl(sem->id, 0, GETVAL);
slouken@1361
   199
#else
slouken@1361
   200
		{
slouken@1361
   201
		union semun arg;
slouken@1361
   202
		arg.val = 0;
slouken@1361
   203
		semval = semctl(sem->id, 0, GETVAL, arg);
slouken@1361
   204
		}
slouken@1361
   205
#endif
slouken@1361
   206
		if ( semval < 0 ) {
slouken@1361
   207
			if ( errno == EINTR ) {
slouken@1361
   208
				goto tryagain;
slouken@1361
   209
			}
slouken@1361
   210
		} else {
slouken@1361
   211
			value = (Uint32)semval;
slouken@1361
   212
		}
slouken@1361
   213
	}
slouken@1361
   214
	return value;
slouken@1361
   215
}
slouken@1361
   216
slouken@1361
   217
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   218
{
slouken@1361
   219
	int retval;
slouken@1361
   220
slouken@1361
   221
	if ( ! sem ) {
slouken@1361
   222
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   223
		return -1;
slouken@1361
   224
	}
slouken@1361
   225
slouken@1361
   226
	retval = 0;
slouken@1361
   227
  tryagain:
slouken@1361
   228
	if ( semop(sem->id, op_post, 1) < 0 ) {
slouken@1361
   229
		if ( errno == EINTR ) {
slouken@1361
   230
			goto tryagain;
slouken@1361
   231
		}
slouken@1361
   232
		SDL_SetError("Semaphore operation error");
slouken@1361
   233
		retval = -1;
slouken@1361
   234
	}
slouken@1361
   235
	return retval;
slouken@1361
   236
}