src/thread/pthread/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Feb 2012 23:51:08 -0500
branchSDL-1.2
changeset 6297 c787fb1b5699
parent 6218 f114f1434ff6
permissions -rw-r--r--
Fixed bug 1426 - SDL_SemWaitTimeout returns -1 and sets error instead of SDL_MUTEX_TIMEDOUT on time out

deraj 2012-02-19 19:01:08 PST

Fix to treat ETIMEDOUT as a time out instead of an error (and update the test)
slouken@1361
     1
/*
slouken@1361
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@1361
     4
slouken@1361
     5
    This library is free software; you can redistribute it and/or
slouken@1361
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1361
     7
    License as published by the Free Software Foundation; either
slouken@1361
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1361
     9
slouken@1361
    10
    This library is distributed in the hope that it will be useful,
slouken@1361
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1361
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1361
    13
    Lesser General Public License for more details.
slouken@1361
    14
slouken@1361
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1361
    16
    License along with this library; if not, write to the Free Software
slouken@1361
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1361
    18
slouken@1361
    19
    Sam Lantinga
slouken@1361
    20
    slouken@libsdl.org
slouken@1361
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@1361
    23
slouken@1361
    24
#include <pthread.h>
slouken@1361
    25
#include <semaphore.h>
icculus@1851
    26
#include <errno.h>
icculus@5985
    27
#include <sys/time.h>
slouken@1361
    28
slouken@1361
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "SDL_timer.h"
slouken@1361
    31
slouken@1361
    32
/* Wrapper around POSIX 1003.1b semaphores */
slouken@1361
    33
slouken@1402
    34
#ifdef __MACOSX__
slouken@1627
    35
/* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
slouken@1627
    36
#include "../generic/SDL_syssem.c"
slouken@1627
    37
#else
slouken@1361
    38
slouken@1361
    39
struct SDL_semaphore {
slouken@1630
    40
	sem_t sem;
slouken@1361
    41
};
slouken@1361
    42
slouken@1361
    43
/* Create a semaphore, initialized with value */
slouken@1361
    44
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
slouken@1361
    45
{
slouken@1361
    46
	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
slouken@1361
    47
	if ( sem ) {
slouken@1630
    48
		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
slouken@1361
    49
			SDL_SetError("sem_init() failed");
slouken@1361
    50
			SDL_free(sem);
slouken@1361
    51
			sem = NULL;
slouken@1361
    52
		}
slouken@1361
    53
	} else {
slouken@1361
    54
		SDL_OutOfMemory();
slouken@1361
    55
	}
slouken@1361
    56
	return sem;
slouken@1361
    57
}
slouken@1361
    58
slouken@1361
    59
void SDL_DestroySemaphore(SDL_sem *sem)
slouken@1361
    60
{
slouken@1361
    61
	if ( sem ) {
slouken@1630
    62
		sem_destroy(&sem->sem);
slouken@1361
    63
		SDL_free(sem);
slouken@1361
    64
	}
slouken@1361
    65
}
slouken@1361
    66
slouken@1361
    67
int SDL_SemTryWait(SDL_sem *sem)
slouken@1361
    68
{
slouken@1361
    69
	int retval;
slouken@1361
    70
slouken@1361
    71
	if ( ! sem ) {
slouken@1361
    72
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    73
		return -1;
slouken@1361
    74
	}
slouken@1361
    75
	retval = SDL_MUTEX_TIMEDOUT;
slouken@1630
    76
	if ( sem_trywait(&sem->sem) == 0 ) {
slouken@1361
    77
		retval = 0;
slouken@1361
    78
	}
slouken@1361
    79
	return retval;
slouken@1361
    80
}
slouken@1361
    81
slouken@1361
    82
int SDL_SemWait(SDL_sem *sem)
slouken@1361
    83
{
slouken@1361
    84
	int retval;
slouken@1361
    85
slouken@1361
    86
	if ( ! sem ) {
slouken@1361
    87
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
    88
		return -1;
slouken@1361
    89
	}
slouken@1361
    90
icculus@1851
    91
	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
slouken@1361
    92
	if ( retval < 0 ) {
slouken@1361
    93
		SDL_SetError("sem_wait() failed");
slouken@1361
    94
	}
slouken@1361
    95
	return retval;
slouken@1361
    96
}
slouken@1361
    97
slouken@1361
    98
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
slouken@1361
    99
{
slouken@1361
   100
	int retval;
slouken@6218
   101
#ifdef HAVE_SEM_TIMEDWAIT
slouken@5105
   102
	struct timeval now;
slouken@5105
   103
	struct timespec ts_timeout;
slouken@6218
   104
#else
slouken@6218
   105
	Uint32 end;
slouken@6218
   106
#endif
slouken@1361
   107
slouken@1361
   108
	if ( ! sem ) {
slouken@1361
   109
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   110
		return -1;
slouken@1361
   111
	}
slouken@1361
   112
slouken@1361
   113
	/* Try the easy cases first */
slouken@1361
   114
	if ( timeout == 0 ) {
slouken@1361
   115
		return SDL_SemTryWait(sem);
slouken@1361
   116
	}
slouken@1361
   117
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
slouken@1361
   118
		return SDL_SemWait(sem);
slouken@1361
   119
	}
slouken@1361
   120
slouken@6218
   121
#ifdef HAVE_SEM_TIMEDWAIT
slouken@5105
   122
	/* Setup the timeout. sem_timedwait doesn't wait for
slouken@5105
   123
	 * a lapse of time, but until we reach a certain time.
slouken@5105
   124
	 * This time is now plus the timeout.
slouken@5105
   125
	 */
slouken@5105
   126
	gettimeofday(&now, NULL);
slouken@5105
   127
slouken@5105
   128
	/* Add our timeout to current time */
slouken@5105
   129
	now.tv_usec += (timeout % 1000) * 1000;
slouken@5105
   130
	now.tv_sec += timeout / 1000;
slouken@5105
   131
slouken@5105
   132
	/* Wrap the second if needed */
slouken@5105
   133
	if ( now.tv_usec >= 1000000 ) {
slouken@5105
   134
		now.tv_usec -= 1000000;
slouken@5105
   135
		now.tv_sec ++;
slouken@5105
   136
	}
slouken@5105
   137
slouken@5105
   138
	/* Convert to timespec */
slouken@5105
   139
	ts_timeout.tv_sec = now.tv_sec;
slouken@5105
   140
	ts_timeout.tv_nsec = now.tv_usec * 1000;
slouken@5105
   141
slouken@5105
   142
	/* Wait. */
slouken@5105
   143
	do
slouken@5105
   144
		retval = sem_timedwait(&sem->sem, &ts_timeout);
slouken@5105
   145
	while (retval == -1 && errno == EINTR);
slouken@5105
   146
slouken@6297
   147
	if (retval == -1) {
slouken@6297
   148
		if (errno == ETIMEDOUT) {
slouken@6297
   149
			retval = SDL_MUTEX_TIMEDOUT;
slouken@6297
   150
		}
slouken@6297
   151
		else {
slouken@6297
   152
			SDL_SetError(strerror(errno));
slouken@6297
   153
		}
slouken@6297
   154
	}
slouken@6218
   155
#else
slouken@6218
   156
	end = SDL_GetTicks() + timeout;
slouken@6218
   157
	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
slouken@6218
   158
		if ((SDL_GetTicks() - end) >= 0) {
slouken@6218
   159
			break;
slouken@6218
   160
		}
slouken@6218
   161
		SDL_Delay(0);
slouken@6218
   162
	}
slouken@6218
   163
#endif /* HAVE_SEM_TIMEDWAIT */
slouken@1361
   164
slouken@1361
   165
	return retval;
slouken@1361
   166
}
slouken@1361
   167
slouken@1361
   168
Uint32 SDL_SemValue(SDL_sem *sem)
slouken@1361
   169
{
slouken@1361
   170
	int ret = 0;
slouken@1361
   171
	if ( sem ) {
slouken@1630
   172
		sem_getvalue(&sem->sem, &ret);
slouken@1361
   173
		if ( ret < 0 ) {
slouken@1361
   174
			ret = 0;
slouken@1361
   175
		}
slouken@1361
   176
	}
slouken@1361
   177
	return (Uint32)ret;
slouken@1361
   178
}
slouken@1361
   179
slouken@1361
   180
int SDL_SemPost(SDL_sem *sem)
slouken@1361
   181
{
slouken@1361
   182
	int retval;
slouken@1361
   183
slouken@1361
   184
	if ( ! sem ) {
slouken@1361
   185
		SDL_SetError("Passed a NULL semaphore");
slouken@1361
   186
		return -1;
slouken@1361
   187
	}
slouken@1361
   188
slouken@1630
   189
	retval = sem_post(&sem->sem);
slouken@1361
   190
	if ( retval < 0 ) {
slouken@1361
   191
		SDL_SetError("sem_post() failed");
slouken@1361
   192
	}
slouken@1361
   193
	return retval;
slouken@1361
   194
}
slouken@1627
   195
slouken@1627
   196
#endif /* __MACOSX__ */