src/thread/generic/SDL_syscond.c
author Sam Lantinga
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
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@0
    23
/* An implementation of condition variables using semaphores and mutexes */
slouken@0
    24
/*
slouken@0
    25
   This implementation borrows heavily from the BeOS condition variable
slouken@0
    26
   implementation, written by Christopher Tate and Owen Smith.  Thanks!
slouken@0
    27
 */
slouken@0
    28
slouken@0
    29
#include "SDL_error.h"
slouken@0
    30
#include "SDL_thread.h"
slouken@1330
    31
#include "SDL_stdlib.h"
slouken@0
    32
slouken@0
    33
struct SDL_cond
slouken@0
    34
{
slouken@0
    35
	SDL_mutex *lock;
slouken@0
    36
	int waiting;
slouken@0
    37
	int signals;
slouken@0
    38
	SDL_sem *wait_sem;
slouken@0
    39
	SDL_sem *wait_done;
slouken@0
    40
};
slouken@0
    41
slouken@0
    42
/* Create a condition variable */
slouken@0
    43
SDL_cond * SDL_CreateCond(void)
slouken@0
    44
{
slouken@0
    45
	SDL_cond *cond;
slouken@0
    46
slouken@0
    47
	cond = (SDL_cond *) malloc(sizeof(SDL_cond));
slouken@0
    48
	if ( cond ) {
slouken@0
    49
		cond->lock = SDL_CreateMutex();
slouken@0
    50
		cond->wait_sem = SDL_CreateSemaphore(0);
slouken@0
    51
		cond->wait_done = SDL_CreateSemaphore(0);
slouken@0
    52
		cond->waiting = cond->signals = 0;
slouken@0
    53
		if ( ! cond->lock || ! cond->wait_sem || ! cond->wait_done ) {
slouken@0
    54
			SDL_DestroyCond(cond);
slouken@0
    55
			cond = NULL;
slouken@0
    56
		}
slouken@0
    57
	} else {
slouken@0
    58
		SDL_OutOfMemory();
slouken@0
    59
	}
slouken@0
    60
	return(cond);
slouken@0
    61
}
slouken@0
    62
slouken@0
    63
/* Destroy a condition variable */
slouken@0
    64
void SDL_DestroyCond(SDL_cond *cond)
slouken@0
    65
{
slouken@0
    66
	if ( cond ) {
slouken@0
    67
		if ( cond->wait_sem ) {
slouken@0
    68
			SDL_DestroySemaphore(cond->wait_sem);
slouken@0
    69
		}
slouken@0
    70
		if ( cond->wait_done ) {
slouken@0
    71
			SDL_DestroySemaphore(cond->wait_done);
slouken@0
    72
		}
slouken@0
    73
		if ( cond->lock ) {
slouken@0
    74
			SDL_DestroyMutex(cond->lock);
slouken@0
    75
		}
slouken@0
    76
		free(cond);
slouken@0
    77
	}
slouken@0
    78
}
slouken@0
    79
slouken@0
    80
/* Restart one of the threads that are waiting on the condition variable */
slouken@0
    81
int SDL_CondSignal(SDL_cond *cond)
slouken@0
    82
{
slouken@0
    83
	if ( ! cond ) {
slouken@0
    84
		SDL_SetError("Passed a NULL condition variable");
slouken@0
    85
		return -1;
slouken@0
    86
	}
slouken@0
    87
slouken@0
    88
	/* If there are waiting threads not already signalled, then
slouken@0
    89
	   signal the condition and wait for the thread to respond.
slouken@0
    90
	*/
slouken@0
    91
	SDL_LockMutex(cond->lock);
slouken@0
    92
	if ( cond->waiting > cond->signals ) {
slouken@0
    93
		++cond->signals;
slouken@0
    94
		SDL_SemPost(cond->wait_sem);
slouken@0
    95
		SDL_UnlockMutex(cond->lock);
slouken@0
    96
		SDL_SemWait(cond->wait_done);
slouken@0
    97
	} else {
slouken@0
    98
		SDL_UnlockMutex(cond->lock);
slouken@0
    99
	}
slouken@0
   100
slouken@0
   101
	return 0;
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
/* Restart all threads that are waiting on the condition variable */
slouken@0
   105
int SDL_CondBroadcast(SDL_cond *cond)
slouken@0
   106
{
slouken@0
   107
	if ( ! cond ) {
slouken@0
   108
		SDL_SetError("Passed a NULL condition variable");
slouken@0
   109
		return -1;
slouken@0
   110
	}
slouken@0
   111
slouken@0
   112
	/* If there are waiting threads not already signalled, then
slouken@0
   113
	   signal the condition and wait for the thread to respond.
slouken@0
   114
	*/
slouken@0
   115
	SDL_LockMutex(cond->lock);
slouken@0
   116
	if ( cond->waiting > cond->signals ) {
slouken@0
   117
		int i, num_waiting;
slouken@0
   118
slouken@0
   119
		num_waiting = (cond->waiting - cond->signals);
slouken@0
   120
		cond->signals = cond->waiting;
slouken@0
   121
		for ( i=0; i<num_waiting; ++i ) {
slouken@0
   122
			SDL_SemPost(cond->wait_sem);
slouken@0
   123
		}
slouken@0
   124
		/* Now all released threads are blocked here, waiting for us.
slouken@0
   125
		   Collect them all (and win fabulous prizes!) :-)
slouken@0
   126
		 */
slouken@0
   127
		SDL_UnlockMutex(cond->lock);
slouken@0
   128
		for ( i=0; i<num_waiting; ++i ) {
slouken@0
   129
			SDL_SemWait(cond->wait_done);
slouken@0
   130
		}
slouken@0
   131
	} else {
slouken@0
   132
		SDL_UnlockMutex(cond->lock);
slouken@0
   133
	}
slouken@0
   134
slouken@0
   135
	return 0;
slouken@0
   136
}
slouken@0
   137
slouken@0
   138
/* Wait on the condition variable for at most 'ms' milliseconds.
slouken@0
   139
   The mutex must be locked before entering this function!
slouken@0
   140
   The mutex is unlocked during the wait, and locked again after the wait.
slouken@0
   141
slouken@0
   142
Typical use:
slouken@0
   143
slouken@0
   144
Thread A:
slouken@0
   145
	SDL_LockMutex(lock);
slouken@0
   146
	while ( ! condition ) {
slouken@0
   147
		SDL_CondWait(cond);
slouken@0
   148
	}
slouken@0
   149
	SDL_UnlockMutex(lock);
slouken@0
   150
slouken@0
   151
Thread B:
slouken@0
   152
	SDL_LockMutex(lock);
slouken@0
   153
	...
slouken@0
   154
	condition = true;
slouken@0
   155
	...
slouken@0
   156
	SDL_UnlockMutex(lock);
slouken@0
   157
 */
slouken@0
   158
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
slouken@0
   159
{
slouken@0
   160
	int retval;
slouken@0
   161
slouken@0
   162
	if ( ! cond ) {
slouken@0
   163
		SDL_SetError("Passed a NULL condition variable");
slouken@0
   164
		return -1;
slouken@0
   165
	}
slouken@0
   166
slouken@0
   167
	/* Obtain the protection mutex, and increment the number of waiters.
slouken@0
   168
	   This allows the signal mechanism to only perform a signal if there
slouken@0
   169
	   are waiting threads.
slouken@0
   170
	 */
slouken@0
   171
	SDL_LockMutex(cond->lock);
slouken@0
   172
	++cond->waiting;
slouken@0
   173
	SDL_UnlockMutex(cond->lock);
slouken@0
   174
slouken@0
   175
	/* Unlock the mutex, as is required by condition variable semantics */
slouken@0
   176
	SDL_UnlockMutex(mutex);
slouken@0
   177
slouken@0
   178
	/* Wait for a signal */
slouken@0
   179
	if ( ms == SDL_MUTEX_MAXWAIT ) {
slouken@0
   180
		retval = SDL_SemWait(cond->wait_sem);
slouken@0
   181
	} else {
slouken@0
   182
		retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
slouken@0
   183
	}
slouken@0
   184
slouken@0
   185
	/* Let the signaler know we have completed the wait, otherwise
slouken@0
   186
           the signaler can race ahead and get the condition semaphore
slouken@0
   187
           if we are stopped between the mutex unlock and semaphore wait,
slouken@0
   188
           giving a deadlock.  See the following URL for details:
slouken@0
   189
        http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
slouken@0
   190
	*/
slouken@0
   191
	SDL_LockMutex(cond->lock);
slouken@0
   192
	if ( cond->signals > 0 ) {
slouken@0
   193
		/* If we timed out, we need to eat a condition signal */
slouken@0
   194
		if ( retval > 0 ) {
slouken@0
   195
			SDL_SemWait(cond->wait_sem);
slouken@0
   196
		}
slouken@0
   197
		/* We always notify the signal thread that we are done */
slouken@0
   198
		SDL_SemPost(cond->wait_done);
slouken@0
   199
slouken@0
   200
		/* Signal handshake complete */
slouken@0
   201
		--cond->signals;
slouken@0
   202
	}
slouken@0
   203
	--cond->waiting;
slouken@0
   204
	SDL_UnlockMutex(cond->lock);
slouken@0
   205
slouken@0
   206
	/* Lock the mutex, as is required by condition variable semantics */
slouken@0
   207
	SDL_LockMutex(mutex);
slouken@0
   208
slouken@0
   209
	return retval;
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
/* Wait on the condition variable forever */
slouken@0
   213
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
slouken@0
   214
{
slouken@0
   215
	return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
slouken@0
   216
}