src/thread/generic/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Apr 2006 14:04:13 +0000
changeset 1627 aee7ea396f59
parent 1402 d910939febfa
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Fixed bug #179

SDL_SemValue() always returns 0. That's because the underlying POSIX
sem_getvalue() is implemented as a noop in Mac OS X. Apart from that,
semaphores do work properly (at least according to test/testsem), so I'm not
sure if this is worth fixing at all.
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@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* An implementation of semaphores using mutexes and condition variables */
slouken@0
    25
slouken@0
    26
#include "SDL_timer.h"
slouken@0
    27
#include "SDL_thread.h"
slouken@0
    28
#include "SDL_systhread_c.h"
slouken@0
    29
slouken@0
    30
slouken@1361
    31
#if SDL_THREADS_DISABLED
slouken@0
    32
slouken@0
    33
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    34
{
slouken@0
    35
	SDL_SetError("SDL not configured with thread support");
slouken@0
    36
	return (SDL_sem *)0;
slouken@0
    37
}
slouken@0
    38
slouken@0
    39
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@0
    40
{
slouken@0
    41
	return;
slouken@0
    42
}
slouken@0
    43
slouken@0
    44
int SDL_SemTryWait(SDL_sem *sem)
slouken@0
    45
{
slouken@0
    46
	SDL_SetError("SDL not configured with thread support");
slouken@0
    47
	return -1;
slouken@0
    48
}
slouken@0
    49
slouken@0
    50
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@0
    51
{
slouken@0
    52
	SDL_SetError("SDL not configured with thread support");
slouken@0
    53
	return -1;
slouken@0
    54
}
slouken@0
    55
slouken@0
    56
int SDL_SemWait(SDL_sem *sem)
slouken@0
    57
{
slouken@0
    58
	SDL_SetError("SDL not configured with thread support");
slouken@0
    59
	return -1;
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@0
    63
{
slouken@0
    64
	return 0;
slouken@0
    65
}
slouken@0
    66
slouken@0
    67
int SDL_SemPost(SDL_sem *sem)
slouken@0
    68
{
slouken@0
    69
	SDL_SetError("SDL not configured with thread support");
slouken@0
    70
	return -1;
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
#else
slouken@0
    74
slouken@0
    75
struct SDL_semaphore
slouken@0
    76
{
slouken@0
    77
	Uint32 count;
slouken@0
    78
	Uint32 waiters_count;
slouken@0
    79
	SDL_mutex *count_lock;
slouken@0
    80
	SDL_cond *count_nonzero;
slouken@0
    81
};
slouken@0
    82
slouken@0
    83
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    84
{
slouken@0
    85
	SDL_sem *sem;
slouken@0
    86
slouken@1336
    87
	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
slouken@0
    88
	if ( ! sem ) {
slouken@0
    89
		SDL_OutOfMemory();
slouken@1627
    90
		return NULL;
slouken@0
    91
	}
slouken@0
    92
	sem->count = initial_value;
slouken@0
    93
	sem->waiters_count = 0;
slouken@0
    94
slouken@0
    95
	sem->count_lock = SDL_CreateMutex();
slouken@0
    96
	sem->count_nonzero = SDL_CreateCond();
slouken@0
    97
	if ( ! sem->count_lock || ! sem->count_nonzero ) {
slouken@0
    98
		SDL_DestroySemaphore(sem);
slouken@1627
    99
		return NULL;
slouken@0
   100
	}
slouken@0
   101
slouken@1627
   102
	return sem;
slouken@0
   103
}
slouken@0
   104
slouken@0
   105
/* WARNING:
slouken@0
   106
   You cannot call this function when another thread is using the semaphore.
slouken@0
   107
*/
slouken@0
   108
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@0
   109
{
slouken@0
   110
	if ( sem ) {
slouken@0
   111
		sem->count = 0xFFFFFFFF;
slouken@0
   112
		while ( sem->waiters_count > 0) {
slouken@0
   113
			SDL_CondSignal(sem->count_nonzero);
slouken@0
   114
			SDL_Delay(10);
slouken@0
   115
		}
slouken@0
   116
		SDL_DestroyCond(sem->count_nonzero);
slouken@1627
   117
		if ( sem->count_lock ) {
slouken@1627
   118
			SDL_mutexP(sem->count_lock);
slouken@1627
   119
			SDL_mutexV(sem->count_lock);
slouken@1627
   120
			SDL_DestroyMutex(sem->count_lock);
slouken@1627
   121
		}
slouken@1336
   122
		SDL_free(sem);
slouken@0
   123
	}
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
int SDL_SemTryWait(SDL_sem *sem)
slouken@0
   127
{
slouken@0
   128
	int retval;
slouken@0
   129
slouken@0
   130
	if ( ! sem ) {
slouken@0
   131
		SDL_SetError("Passed a NULL semaphore");
slouken@0
   132
		return -1;
slouken@0
   133
	}
slouken@0
   134
slouken@0
   135
	retval = SDL_MUTEX_TIMEDOUT;
slouken@0
   136
	SDL_LockMutex(sem->count_lock);
slouken@0
   137
	if ( sem->count > 0 ) {
slouken@0
   138
		--sem->count;
slouken@0
   139
		retval = 0;
slouken@0
   140
	}
slouken@0
   141
	SDL_UnlockMutex(sem->count_lock);
slouken@0
   142
slouken@0
   143
	return retval;
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@0
   147
{
slouken@0
   148
	int retval;
slouken@0
   149
slouken@0
   150
	if ( ! sem ) {
slouken@0
   151
		SDL_SetError("Passed a NULL semaphore");
slouken@0
   152
		return -1;
slouken@0
   153
	}
slouken@0
   154
slouken@0
   155
	/* A timeout of 0 is an easy case */
slouken@0
   156
	if ( timeout == 0 ) {
slouken@0
   157
		return SDL_SemTryWait(sem);
slouken@0
   158
	}
slouken@0
   159
slouken@0
   160
	SDL_LockMutex(sem->count_lock);
slouken@0
   161
	++sem->waiters_count;
slouken@0
   162
	retval = 0;
slouken@0
   163
	while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
slouken@0
   164
		retval = SDL_CondWaitTimeout(sem->count_nonzero,
slouken@0
   165
		                             sem->count_lock, timeout);
slouken@0
   166
	}
slouken@0
   167
	--sem->waiters_count;
slouken@0
   168
	--sem->count;
slouken@0
   169
	SDL_UnlockMutex(sem->count_lock);
slouken@0
   170
slouken@0
   171
	return retval;
slouken@0
   172
}
slouken@0
   173
slouken@0
   174
int SDL_SemWait(SDL_sem *sem)
slouken@0
   175
{
slouken@0
   176
	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@0
   180
{
slouken@0
   181
	Uint32 value;
slouken@0
   182
	
slouken@0
   183
	value = 0;
slouken@0
   184
	if ( sem ) {
slouken@0
   185
		SDL_LockMutex(sem->count_lock);
slouken@0
   186
		value = sem->count;
slouken@0
   187
		SDL_UnlockMutex(sem->count_lock);
slouken@0
   188
	}
slouken@0
   189
	return value;
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
int SDL_SemPost(SDL_sem *sem)
slouken@0
   193
{
slouken@0
   194
	if ( ! sem ) {
slouken@0
   195
		SDL_SetError("Passed a NULL semaphore");
slouken@0
   196
		return -1;
slouken@0
   197
	}
slouken@0
   198
slouken@0
   199
	SDL_LockMutex(sem->count_lock);
slouken@0
   200
	if ( sem->waiters_count > 0 ) {
slouken@0
   201
		SDL_CondSignal(sem->count_nonzero);
slouken@0
   202
	}
slouken@0
   203
	++sem->count;
slouken@0
   204
	SDL_UnlockMutex(sem->count_lock);
slouken@0
   205
slouken@0
   206
	return 0;
slouken@0
   207
}
slouken@0
   208
slouken@1361
   209
#endif /* SDL_THREADS_DISABLED */