src/thread/win32/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 5886 05fabaf401c9
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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
/* Semaphore functions using the Win32 API */
slouken@0
    25
slouken@1433
    26
#define WIN32_LEAN_AND_MEAN
slouken@1433
    27
#include <windows.h>
slouken@0
    28
slouken@0
    29
#include "SDL_thread.h"
slouken@36
    30
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    31
#include "win_ce_semaphore.h"
slouken@36
    32
#endif
slouken@0
    33
slouken@0
    34
slouken@0
    35
struct SDL_semaphore {
slouken@36
    36
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    37
	SYNCHHANDLE id;
slouken@36
    38
#else
slouken@0
    39
	HANDLE id;
slouken@36
    40
#endif
slouken@0
    41
	Uint32 volatile count;
slouken@0
    42
};
slouken@0
    43
slouken@0
    44
slouken@0
    45
/* Create a semaphore */
slouken@0
    46
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    47
{
slouken@0
    48
	SDL_sem *sem;
slouken@0
    49
slouken@0
    50
	/* Allocate sem memory */
slouken@1336
    51
	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
slouken@0
    52
	if ( sem ) {
slouken@0
    53
		/* Create the semaphore, with max value 32K */
slouken@36
    54
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    55
		sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL);
slouken@36
    56
#else
slouken@0
    57
		sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL);
slouken@36
    58
#endif
slouken@0
    59
		sem->count = initial_value;
slouken@0
    60
		if ( ! sem->id ) {
slouken@0
    61
			SDL_SetError("Couldn't create semaphore");
slouken@1336
    62
			SDL_free(sem);
slouken@0
    63
			sem = NULL;
slouken@0
    64
		}
slouken@0
    65
	} else {
slouken@0
    66
		SDL_OutOfMemory();
slouken@0
    67
	}
slouken@0
    68
	return(sem);
slouken@0
    69
}
slouken@0
    70
slouken@0
    71
/* Free the semaphore */
slouken@0
    72
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@0
    73
{
slouken@0
    74
	if ( sem ) {
slouken@0
    75
		if ( sem->id ) {
slouken@36
    76
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
    77
			CloseSynchHandle(sem->id);
slouken@36
    78
#else
slouken@0
    79
			CloseHandle(sem->id);
slouken@36
    80
#endif
slouken@0
    81
			sem->id = 0;
slouken@0
    82
		}
slouken@1336
    83
		SDL_free(sem);
slouken@0
    84
	}
slouken@0
    85
}
slouken@0
    86
slouken@0
    87
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@0
    88
{
slouken@0
    89
	int retval;
slouken@0
    90
	DWORD dwMilliseconds;
slouken@0
    91
slouken@0
    92
	if ( ! sem ) {
slouken@0
    93
		SDL_SetError("Passed a NULL sem");
slouken@0
    94
		return -1;
slouken@0
    95
	}
slouken@0
    96
slouken@0
    97
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@0
    98
		dwMilliseconds = INFINITE;
slouken@0
    99
	} else {
slouken@0
   100
		dwMilliseconds = (DWORD)timeout;
slouken@0
   101
	}
slouken@36
   102
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   103
	switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
slouken@36
   104
#else
slouken@0
   105
	switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
slouken@36
   106
#endif
slouken@0
   107
	    case WAIT_OBJECT_0:
slouken@4105
   108
		InterlockedDecrement(&sem->count);
slouken@0
   109
		retval = 0;
slouken@0
   110
		break;
slouken@0
   111
	    case WAIT_TIMEOUT:
slouken@0
   112
		retval = SDL_MUTEX_TIMEDOUT;
slouken@0
   113
		break;
slouken@0
   114
	    default:
slouken@0
   115
		SDL_SetError("WaitForSingleObject() failed");
slouken@0
   116
		retval = -1;
slouken@0
   117
		break;
slouken@0
   118
	}
slouken@0
   119
	return retval;
slouken@0
   120
}
slouken@0
   121
slouken@0
   122
int SDL_SemTryWait(SDL_sem *sem)
slouken@0
   123
{
slouken@0
   124
	return SDL_SemWaitTimeout(sem, 0);
slouken@0
   125
}
slouken@0
   126
slouken@0
   127
int SDL_SemWait(SDL_sem *sem)
slouken@0
   128
{
slouken@0
   129
	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
slouken@0
   130
}
slouken@0
   131
slouken@0
   132
/* Returns the current count of the semaphore */
slouken@0
   133
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@0
   134
{
slouken@0
   135
	if ( ! sem ) {
slouken@0
   136
		SDL_SetError("Passed a NULL sem");
slouken@0
   137
		return 0;
slouken@0
   138
	}
slouken@0
   139
	return sem->count;
slouken@0
   140
}
slouken@0
   141
slouken@0
   142
int SDL_SemPost(SDL_sem *sem)
slouken@0
   143
{
slouken@0
   144
	if ( ! sem ) {
slouken@0
   145
		SDL_SetError("Passed a NULL sem");
slouken@0
   146
		return -1;
slouken@0
   147
	}
slouken@0
   148
	/* Increase the counter in the first place, because
slouken@0
   149
	 * after a successful release the semaphore may
slouken@0
   150
	 * immediately get destroyed by another thread which
slouken@0
   151
	 * is waiting for this semaphore.
slouken@0
   152
	 */
slouken@4105
   153
	InterlockedIncrement(&sem->count);
slouken@36
   154
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
slouken@36
   155
	if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) {
slouken@36
   156
#else
slouken@0
   157
	if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) {
slouken@36
   158
#endif
slouken@4105
   159
		InterlockedDecrement(&sem->count);	/* restore */
slouken@0
   160
		SDL_SetError("ReleaseSemaphore() failed");
slouken@0
   161
		return -1;
slouken@0
   162
	}
slouken@0
   163
	return 0;
slouken@0
   164
}