src/thread/amigaos/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@21
    23
/* An implementation of semaphores using mutexes and condition variables */
slouken@0
    24
slouken@0
    25
#include "SDL_thread.h"
slouken@0
    26
#include "SDL_systhread_c.h"
slouken@0
    27
slouken@0
    28
slouken@0
    29
struct SDL_semaphore
slouken@0
    30
{
slouken@0
    31
	struct SignalSemaphore Sem;
slouken@0
    32
};
slouken@0
    33
slouken@21
    34
#undef D
slouken@0
    35
slouken@0
    36
#define D(x)
slouken@0
    37
slouken@0
    38
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    39
{
slouken@0
    40
	SDL_sem *sem;
slouken@0
    41
slouken@1336
    42
	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
slouken@21
    43
slouken@0
    44
	if ( ! sem ) {
slouken@0
    45
		SDL_OutOfMemory();
slouken@0
    46
		return(0);
slouken@0
    47
	}
slouken@0
    48
slouken@0
    49
	D(bug("Creating semaphore %lx...\n",sem));
slouken@0
    50
slouken@1336
    51
	SDL_memset(sem,0,sizeof(*sem));
slouken@21
    52
slouken@0
    53
	InitSemaphore(&sem->Sem);
slouken@255
    54
slouken@0
    55
	return(sem);
slouken@0
    56
}
slouken@0
    57
slouken@0
    58
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@0
    59
{
slouken@0
    60
	D(bug("Destroying semaphore %lx...\n",sem));
slouken@0
    61
slouken@0
    62
	if ( sem ) {
slouken@0
    63
// Condizioni per liberare i task in attesa?
slouken@1336
    64
		SDL_free(sem);
slouken@0
    65
	}
slouken@0
    66
}
slouken@0
    67
slouken@0
    68
int SDL_SemTryWait(SDL_sem *sem)
slouken@0
    69
{
slouken@0
    70
	if ( ! sem ) {
slouken@0
    71
		SDL_SetError("Passed a NULL semaphore");
slouken@0
    72
		return -1;
slouken@0
    73
	}
slouken@0
    74
slouken@0
    75
	D(bug("TryWait semaphore...%lx\n",sem));
slouken@0
    76
slouken@21
    77
	ObtainSemaphore(&sem->Sem);
slouken@21
    78
//	ReleaseSemaphore(&sem->Sem);
slouken@21
    79
slouken@21
    80
	return 1;
slouken@0
    81
}
slouken@0
    82
slouken@0
    83
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@0
    84
{
slouken@0
    85
	int retval;
slouken@0
    86
slouken@21
    87
slouken@0
    88
	if ( ! sem ) {
slouken@0
    89
		SDL_SetError("Passed a NULL semaphore");
slouken@0
    90
		return -1;
slouken@0
    91
	}
slouken@0
    92
slouken@0
    93
	D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem));
slouken@0
    94
slouken@21
    95
	/* A timeout of 0 is an easy case */
slouken@21
    96
	if ( timeout == 0 ) {
slouken@1361
    97
		ObtainSemaphore(&sem->Sem);
slouken@1361
    98
		return 1;
slouken@21
    99
	}
slouken@0
   100
	if(!(retval=AttemptSemaphore(&sem->Sem)))
slouken@0
   101
	{
slouken@0
   102
		SDL_Delay(timeout);
slouken@0
   103
		retval=AttemptSemaphore(&sem->Sem);
slouken@0
   104
	}
slouken@0
   105
slouken@0
   106
	if(retval==TRUE)
slouken@0
   107
	{
slouken@0
   108
//		ReleaseSemaphore(&sem->Sem);
slouken@0
   109
		retval=1;
slouken@0
   110
	}
slouken@21
   111
slouken@0
   112
	return retval;
slouken@0
   113
}
slouken@0
   114
slouken@0
   115
int SDL_SemWait(SDL_sem *sem)
slouken@0
   116
{
slouken@0
   117
	ObtainSemaphore(&sem->Sem);
slouken@21
   118
	return 0;
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@0
   122
{
slouken@0
   123
	Uint32 value;
slouken@255
   124
slouken@0
   125
	value = 0;
slouken@0
   126
	if ( sem ) {
slouken@255
   127
		#ifdef STORMC4_WOS
slouken@255
   128
		value = sem->Sem.ssppc_SS.ss_NestCount;
slouken@255
   129
		#else
slouken@0
   130
		value = sem->Sem.ss_NestCount;
slouken@255
   131
		#endif
slouken@0
   132
	}
slouken@0
   133
	return value;
slouken@0
   134
}
slouken@0
   135
slouken@0
   136
int SDL_SemPost(SDL_sem *sem)
slouken@0
   137
{
slouken@0
   138
	if ( ! sem ) {
slouken@0
   139
		SDL_SetError("Passed a NULL semaphore");
slouken@0
   140
		return -1;
slouken@0
   141
	}
slouken@0
   142
	D(bug("SemPost semaphore...%lx\n",sem));
slouken@0
   143
slouken@0
   144
	ReleaseSemaphore(&sem->Sem);
slouken@0
   145
	return 0;
slouken@0
   146
}
slouken@0
   147