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