src/thread/irix/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
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@1361
    22
slouken@1361
    23
#include "SDL_thread.h"
slouken@1361
    24
#include "SDL_timer.h"
slouken@1361
    25
slouken@1361
    26
slouken@1361
    27
#include <stdio.h>
slouken@1361
    28
#include <stdlib.h>
slouken@1361
    29
#include <sys/types.h>
slouken@1361
    30
#include <sys/ipc.h>
slouken@1361
    31
#include <sys/sem.h>
slouken@1361
    32
#include <errno.h>
slouken@1361
    33
slouken@1361
    34
#include "SDL_error.h"
slouken@1361
    35
#include "SDL_thread.h"
slouken@1361
    36
slouken@1361
    37
slouken@1361
    38
struct SDL_semaphore {
slouken@1361
    39
	int id;
slouken@1361
    40
};
slouken@1361
    41
slouken@1361
    42
/* Not defined by many operating systems, use configure to detect */
slouken@1361
    43
/*
slouken@1361
    44
#if !defined(HAVE_SEMUN)
slouken@1361
    45
union semun {
slouken@1361
    46
	int val;
slouken@1361
    47
	struct semid_ds *buf;
slouken@1361
    48
	ushort *array;
slouken@1361
    49
};
slouken@1361
    50
#endif
slouken@1361
    51
*/
slouken@1361
    52
slouken@1361
    53
static struct sembuf op_trywait[2] = {
slouken@1361
    54
	{ 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
slouken@1361
    55
};
slouken@1361
    56
static struct sembuf op_wait[2] = {
slouken@1361
    57
	{ 0, -1, SEM_UNDO }		/* Decrement semaphore */
slouken@1361
    58
};
slouken@1361
    59
static struct sembuf op_post[1] = {
slouken@1361
    60
	{ 0, 1, (IPC_NOWAIT|SEM_UNDO) }	/* Increment semaphore */
slouken@1361
    61
};
slouken@1361
    62
slouken@1361
    63
/* Create a blockable semaphore */
slouken@1361
    64
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    65
{
slouken@1361
    66
	extern int _creating_thread_lock;	/* SDL_threads.c */
slouken@1361
    67
	SDL_sem *sem;
slouken@1361
    68
	union semun init;
slouken@1361
    69
	key_t key;
slouken@1361
    70
slouken@1361
    71
	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
slouken@1361
    72
	if ( sem == NULL ) {
slouken@1361
    73
		SDL_OutOfMemory();
slouken@1361
    74
		return(NULL);
slouken@1361
    75
	}
slouken@1361
    76
	/* This flag is true if we are creating the thread manager sem,
slouken@1361
    77
	   which is never freed.  This allows us to reuse the same sem.
slouken@1361
    78
	*/
slouken@1361
    79
	if ( _creating_thread_lock ) {
slouken@1361
    80
		key = 'S'+'D'+'L';
slouken@1361
    81
	} else {
slouken@1361
    82
		key = IPC_PRIVATE;
slouken@1361
    83
	}
slouken@1361
    84
	/* Keep trying to create sem while we don't own the requested key */
slouken@1361
    85
	do {
slouken@1361
    86
		if ( key != IPC_PRIVATE ) {
slouken@1361
    87
			++key;
slouken@1361
    88
		}
slouken@1361
    89
		sem->id = semget(key, 1, (0600|IPC_CREAT));
slouken@1361
    90
	} while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES));
slouken@1361
    91
slouken@1361
    92
	/* Report the error if we eventually failed */
slouken@1361
    93
	if ( sem->id < 0 ) {
slouken@1361
    94
		SDL_SetError("Couldn't create semaphore");
slouken@1361
    95
		SDL_free(sem);
slouken@1361
    96
		return(NULL);
slouken@1361
    97
	}
slouken@1361
    98
	init.val = initial_value;	/* Initialize semaphore */
slouken@1361
    99
	semctl(sem->id, 0, SETVAL, init);
slouken@1361
   100
	return(sem);
slouken@1361
   101
}
slouken@1361
   102
slouken@1361
   103
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
   104
{
slouken@1361
   105
	if ( sem ) {
slouken@1361
   106
#ifdef _SGI_SOURCE
slouken@1361
   107
		semctl(sem->id, 0, IPC_RMID);
slouken@1361
   108
#else
slouken@1361
   109
		union semun dummy;
slouken@1361
   110
		dummy.val = 0;
slouken@1361
   111
		semctl(sem->id, 0, IPC_RMID, dummy);
slouken@1361
   112
#endif
slouken@1361
   113
		SDL_free(sem);
slouken@1361
   114
	}
slouken@1361
   115
}
slouken@1361
   116
slouken@1361
   117
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
   118
{
slouken@1361
   119
	int retval;
slouken@1361
   120
slouken@1361
   121
	if ( ! sem ) {
slouken@1361
   122
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   123
		return -1;
slouken@1361
   124
	}
slouken@1361
   125
slouken@1361
   126
	retval = 0;
slouken@1361
   127
  tryagain:
slouken@1361
   128
	if ( semop(sem->id, op_trywait, 1) < 0 ) {
slouken@1361
   129
		if ( errno == EINTR ) {
slouken@1361
   130
			goto tryagain;
slouken@1361
   131
		}
slouken@1361
   132
		retval = SDL_MUTEX_TIMEDOUT;
slouken@1361
   133
	}
slouken@1361
   134
	return retval;
slouken@1361
   135
}
slouken@1361
   136
slouken@1361
   137
int SDL_SemWait(SDL_sem *sem)
slouken@1361
   138
{
slouken@1361
   139
	int retval;
slouken@1361
   140
slouken@1361
   141
	if ( ! sem ) {
slouken@1361
   142
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   143
		return -1;
slouken@1361
   144
	}
slouken@1361
   145
slouken@1361
   146
	retval = 0;
slouken@1361
   147
  tryagain:
slouken@1361
   148
	if ( semop(sem->id, op_wait, 1) < 0 ) {
slouken@1361
   149
		if ( errno == EINTR ) {
slouken@1361
   150
			goto tryagain;
slouken@1361
   151
		}
slouken@1361
   152
		SDL_SetError("Semaphore operation error");
slouken@1361
   153
		retval = -1;
slouken@1361
   154
	}
slouken@1361
   155
	return retval;
slouken@1361
   156
}
slouken@1361
   157
slouken@1361
   158
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
   159
{
slouken@1361
   160
	int retval;
slouken@1361
   161
slouken@1361
   162
	if ( ! sem ) {
slouken@1361
   163
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   164
		return -1;
slouken@1361
   165
	}
slouken@1361
   166
slouken@1361
   167
	/* Try the easy cases first */
slouken@1361
   168
	if ( timeout == 0 ) {
slouken@1361
   169
		return SDL_SemTryWait(sem);
slouken@1361
   170
	}
slouken@1361
   171
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   172
		return SDL_SemWait(sem);
slouken@1361
   173
	}
slouken@1361
   174
slouken@1361
   175
	/* Ack!  We have to busy wait... */
slouken@1361
   176
	timeout += SDL_GetTicks();
slouken@1361
   177
	do {
slouken@1361
   178
		retval = SDL_SemTryWait(sem);
slouken@1361
   179
		if ( retval == 0 ) {
slouken@1361
   180
			break;
slouken@1361
   181
		}
slouken@1361
   182
		SDL_Delay(1);
slouken@1361
   183
	} while ( SDL_GetTicks() < timeout );
slouken@1361
   184
slouken@1361
   185
	return retval;
slouken@1361
   186
}
slouken@1361
   187
slouken@1361
   188
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   189
{
slouken@1361
   190
	int semval;
slouken@1361
   191
	Uint32 value;
slouken@1361
   192
	
slouken@1361
   193
	value = 0;
slouken@1361
   194
	if ( sem ) {
slouken@1361
   195
	  tryagain:
slouken@1361
   196
#ifdef _SGI_SOURCE
slouken@1361
   197
		semval = semctl(sem->id, 0, GETVAL);
slouken@1361
   198
#else
slouken@1361
   199
		{
slouken@1361
   200
		union semun arg;
slouken@1361
   201
		arg.val = 0;
slouken@1361
   202
		semval = semctl(sem->id, 0, GETVAL, arg);
slouken@1361
   203
		}
slouken@1361
   204
#endif
slouken@1361
   205
		if ( semval < 0 ) {
slouken@1361
   206
			if ( errno == EINTR ) {
slouken@1361
   207
				goto tryagain;
slouken@1361
   208
			}
slouken@1361
   209
		} else {
slouken@1361
   210
			value = (Uint32)semval;
slouken@1361
   211
		}
slouken@1361
   212
	}
slouken@1361
   213
	return value;
slouken@1361
   214
}
slouken@1361
   215
slouken@1361
   216
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   217
{
slouken@1361
   218
	int retval;
slouken@1361
   219
slouken@1361
   220
	if ( ! sem ) {
slouken@1361
   221
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   222
		return -1;
slouken@1361
   223
	}
slouken@1361
   224
slouken@1361
   225
	retval = 0;
slouken@1361
   226
  tryagain:
slouken@1361
   227
	if ( semop(sem->id, op_post, 1) < 0 ) {
slouken@1361
   228
		if ( errno == EINTR ) {
slouken@1361
   229
			goto tryagain;
slouken@1361
   230
		}
slouken@1361
   231
		SDL_SetError("Semaphore operation error");
slouken@1361
   232
		retval = -1;
slouken@1361
   233
	}
slouken@1361
   234
	return retval;
slouken@1361
   235
}