src/thread/linux/SDL_syssem.c
changeset 1361 19418e4422cb
parent 1360 70a9cfb4cf1b
child 1362 88ddeb76c9c1
equal deleted inserted replaced
1360:70a9cfb4cf1b 1361:19418e4422cb
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997-2006 Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Lesser General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2.1 of the License, or (at your option) any later version.
       
     9 
       
    10     This library is distributed in the hope that it will be useful,
       
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13     Lesser General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Lesser General Public
       
    16     License along with this library; if not, write to the Free Software
       
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@libsdl.org
       
    21 */
       
    22 
       
    23 #include "SDL_thread.h"
       
    24 #include "SDL_timer.h"
       
    25 
       
    26 #ifdef linux
       
    27 /* Look to see if glibc is available, and if so, what version */
       
    28 #include <features.h>
       
    29 
       
    30 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
       
    31 #warning Working around a bug in glibc 2.0 pthreads
       
    32 #undef SDL_USE_PTHREADS
       
    33 /* The bug is actually a problem where threads are suspended, but don't
       
    34    wake up when the thread manager sends them a signal.  This is a problem
       
    35    with thread creation too, but it happens less often. :-/
       
    36    We avoid this by using System V IPC for semaphores.
       
    37  */
       
    38 #endif /* glibc 2.0 */
       
    39 #endif /* linux */
       
    40 
       
    41 #ifdef SDL_USE_PTHREADS
       
    42 
       
    43 #ifdef SDL_NO_PTHREAD_SEMAPHORES
       
    44 #include "generic/SDL_syssem.c"
       
    45 #else
       
    46 
       
    47 #include <pthread.h>
       
    48 #include <semaphore.h>
       
    49 
       
    50 /* Wrapper around POSIX 1003.1b semaphores */
       
    51 
       
    52 #ifdef MACOSX
       
    53 #define USE_NAMED_SEMAPHORES
       
    54 #endif /* MACOSX */
       
    55 
       
    56 struct SDL_semaphore {
       
    57 	sem_t *sem;
       
    58 #ifndef USE_NAMED_SEMAPHORES
       
    59 	sem_t sem_data;
       
    60 #endif
       
    61 };
       
    62 
       
    63 /* Create a semaphore, initialized with value */
       
    64 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
       
    65 {
       
    66 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
       
    67 	if ( sem ) {
       
    68 #ifdef USE_NAMED_SEMAPHORES
       
    69 		static int semnum = 0;
       
    70 		char name[32];
       
    71 
       
    72 		SDL_snprintf(name, SDL_arraysize(name), "/SDL_sem-%d-%4.4d", getpid(), semnum++);
       
    73 		sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
       
    74 		if ( sem->sem == (sem_t *)SEM_FAILED ) {
       
    75 			SDL_SetError("sem_open(%s) failed", name);
       
    76 			SDL_free(sem);
       
    77 			sem = NULL;
       
    78 		} else {
       
    79 			sem_unlink(name);
       
    80 		}
       
    81 #else
       
    82 		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
       
    83 			SDL_SetError("sem_init() failed");
       
    84 			SDL_free(sem);
       
    85 			sem = NULL;
       
    86 		} else {
       
    87 			sem->sem = &sem->sem_data;
       
    88 		}
       
    89 #endif /* USE_NAMED_SEMAPHORES */
       
    90 	} else {
       
    91 		SDL_OutOfMemory();
       
    92 	}
       
    93 	return sem;
       
    94 }
       
    95 
       
    96 void SDL_DestroySemaphore(SDL_sem *sem)
       
    97 {
       
    98 	if ( sem ) {
       
    99 #ifdef USE_NAMED_SEMAPHORES
       
   100 		sem_close(sem->sem);
       
   101 #else
       
   102 		sem_destroy(sem->sem);
       
   103 #endif
       
   104 		SDL_free(sem);
       
   105 	}
       
   106 }
       
   107 
       
   108 int SDL_SemTryWait(SDL_sem *sem)
       
   109 {
       
   110 	int retval;
       
   111 
       
   112 	if ( ! sem ) {
       
   113 		SDL_SetError("Passed a NULL semaphore");
       
   114 		return -1;
       
   115 	}
       
   116 	retval = SDL_MUTEX_TIMEDOUT;
       
   117 	if ( sem_trywait(sem->sem) == 0 ) {
       
   118 		retval = 0;
       
   119 	}
       
   120 	return retval;
       
   121 }
       
   122 
       
   123 int SDL_SemWait(SDL_sem *sem)
       
   124 {
       
   125 	int retval;
       
   126 
       
   127 	if ( ! sem ) {
       
   128 		SDL_SetError("Passed a NULL semaphore");
       
   129 		return -1;
       
   130 	}
       
   131 
       
   132 	retval = sem_wait(sem->sem);
       
   133 	if ( retval < 0 ) {
       
   134 		SDL_SetError("sem_wait() failed");
       
   135 	}
       
   136 	return retval;
       
   137 }
       
   138 
       
   139 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
       
   140 {
       
   141 	int retval;
       
   142 
       
   143 	if ( ! sem ) {
       
   144 		SDL_SetError("Passed a NULL semaphore");
       
   145 		return -1;
       
   146 	}
       
   147 
       
   148 	/* Try the easy cases first */
       
   149 	if ( timeout == 0 ) {
       
   150 		return SDL_SemTryWait(sem);
       
   151 	}
       
   152 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
       
   153 		return SDL_SemWait(sem);
       
   154 	}
       
   155 
       
   156 	/* Ack!  We have to busy wait... */
       
   157 	timeout += SDL_GetTicks();
       
   158 	do {
       
   159 		retval = SDL_SemTryWait(sem);
       
   160 		if ( retval == 0 ) {
       
   161 			break;
       
   162 		}
       
   163 		SDL_Delay(1);
       
   164 	} while ( SDL_GetTicks() < timeout );
       
   165 
       
   166 	return retval;
       
   167 }
       
   168 
       
   169 Uint32 SDL_SemValue(SDL_sem *sem)
       
   170 {
       
   171 	int ret = 0;
       
   172 	if ( sem ) {
       
   173 		sem_getvalue(sem->sem, &ret);
       
   174 		if ( ret < 0 ) {
       
   175 			ret = 0;
       
   176 		}
       
   177 	}
       
   178 	return (Uint32)ret;
       
   179 }
       
   180 
       
   181 int SDL_SemPost(SDL_sem *sem)
       
   182 {
       
   183 	int retval;
       
   184 
       
   185 	if ( ! sem ) {
       
   186 		SDL_SetError("Passed a NULL semaphore");
       
   187 		return -1;
       
   188 	}
       
   189 
       
   190 	retval = sem_post(sem->sem);
       
   191 	if ( retval < 0 ) {
       
   192 		SDL_SetError("sem_post() failed");
       
   193 	}
       
   194 	return retval;
       
   195 }
       
   196 
       
   197 #endif /* NO_PTHREAD_SEMAPHORES */
       
   198 
       
   199 #else /* System V IPC implementation */
       
   200 
       
   201 #include <stdio.h>
       
   202 #include <stdlib.h>
       
   203 #include <sys/types.h>
       
   204 #include <sys/ipc.h>
       
   205 #include <sys/sem.h>
       
   206 #include <errno.h>
       
   207 
       
   208 #include "SDL_error.h"
       
   209 #include "SDL_thread.h"
       
   210 
       
   211 
       
   212 struct SDL_semaphore {
       
   213 	int id;
       
   214 };
       
   215 
       
   216 /* Not defined by many operating systems, use configure to detect */
       
   217 #if !defined(HAVE_SEMUN)
       
   218 union semun {
       
   219 	int val;
       
   220 	struct semid_ds *buf;
       
   221 	ushort *array;
       
   222 };
       
   223 #endif
       
   224 
       
   225 static struct sembuf op_trywait[2] = {
       
   226 	{ 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
       
   227 };
       
   228 static struct sembuf op_wait[2] = {
       
   229 	{ 0, -1, SEM_UNDO }		/* Decrement semaphore */
       
   230 };
       
   231 static struct sembuf op_post[1] = {
       
   232 	{ 0, 1, (IPC_NOWAIT|SEM_UNDO) }	/* Increment semaphore */
       
   233 };
       
   234 
       
   235 /* Create a blockable semaphore */
       
   236 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
       
   237 {
       
   238 	extern int _creating_thread_lock;	/* SDL_threads.c */
       
   239 	SDL_sem *sem;
       
   240 	union semun init;
       
   241 	key_t key;
       
   242 
       
   243 	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
       
   244 	if ( sem == NULL ) {
       
   245 		SDL_OutOfMemory();
       
   246 		return(NULL);
       
   247 	}
       
   248 	/* This flag is true if we are creating the thread manager sem,
       
   249 	   which is never freed.  This allows us to reuse the same sem.
       
   250 	*/
       
   251 	if ( _creating_thread_lock ) {
       
   252 		key = 'S'+'D'+'L';
       
   253 	} else {
       
   254 		key = IPC_PRIVATE;
       
   255 	}
       
   256 	/* Keep trying to create sem while we don't own the requested key */
       
   257 	do {
       
   258 		if ( key != IPC_PRIVATE ) {
       
   259 			++key;
       
   260 		}
       
   261 		sem->id = semget(key, 1, (0600|IPC_CREAT));
       
   262 	} while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES));
       
   263 
       
   264 	/* Report the error if we eventually failed */
       
   265 	if ( sem->id < 0 ) {
       
   266 		SDL_SetError("Couldn't create semaphore");
       
   267 		SDL_free(sem);
       
   268 		return(NULL);
       
   269 	}
       
   270 	init.val = initial_value;	/* Initialize semaphore */
       
   271 	semctl(sem->id, 0, SETVAL, init);
       
   272 	return(sem);
       
   273 }
       
   274 
       
   275 void SDL_DestroySemaphore(SDL_sem *sem)
       
   276 {
       
   277 	if ( sem ) {
       
   278 #ifdef _SGI_SOURCE
       
   279 		semctl(sem->id, 0, IPC_RMID);
       
   280 #else
       
   281 		union semun dummy;
       
   282 		dummy.val = 0;
       
   283 		semctl(sem->id, 0, IPC_RMID, dummy);
       
   284 #endif
       
   285 		SDL_free(sem);
       
   286 	}
       
   287 }
       
   288 
       
   289 int SDL_SemTryWait(SDL_sem *sem)
       
   290 {
       
   291 	int retval;
       
   292 
       
   293 	if ( ! sem ) {
       
   294 		SDL_SetError("Passed a NULL semaphore");
       
   295 		return -1;
       
   296 	}
       
   297 
       
   298 	retval = 0;
       
   299   tryagain:
       
   300 	if ( semop(sem->id, op_trywait, 1) < 0 ) {
       
   301 		if ( errno == EINTR ) {
       
   302 			goto tryagain;
       
   303 		}
       
   304 		retval = SDL_MUTEX_TIMEDOUT;
       
   305 	}
       
   306 	return retval;
       
   307 }
       
   308 
       
   309 int SDL_SemWait(SDL_sem *sem)
       
   310 {
       
   311 	int retval;
       
   312 
       
   313 	if ( ! sem ) {
       
   314 		SDL_SetError("Passed a NULL semaphore");
       
   315 		return -1;
       
   316 	}
       
   317 
       
   318 	retval = 0;
       
   319   tryagain:
       
   320 	if ( semop(sem->id, op_wait, 1) < 0 ) {
       
   321 		if ( errno == EINTR ) {
       
   322 			goto tryagain;
       
   323 		}
       
   324 		SDL_SetError("Semaphore operation error");
       
   325 		retval = -1;
       
   326 	}
       
   327 	return retval;
       
   328 }
       
   329 
       
   330 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
       
   331 {
       
   332 	int retval;
       
   333 
       
   334 	if ( ! sem ) {
       
   335 		SDL_SetError("Passed a NULL semaphore");
       
   336 		return -1;
       
   337 	}
       
   338 
       
   339 	/* Try the easy cases first */
       
   340 	if ( timeout == 0 ) {
       
   341 		return SDL_SemTryWait(sem);
       
   342 	}
       
   343 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
       
   344 		return SDL_SemWait(sem);
       
   345 	}
       
   346 
       
   347 	/* Ack!  We have to busy wait... */
       
   348 	timeout += SDL_GetTicks();
       
   349 	do {
       
   350 		retval = SDL_SemTryWait(sem);
       
   351 		if ( retval == 0 ) {
       
   352 			break;
       
   353 		}
       
   354 		SDL_Delay(1);
       
   355 	} while ( SDL_GetTicks() < timeout );
       
   356 
       
   357 	return retval;
       
   358 }
       
   359 
       
   360 Uint32 SDL_SemValue(SDL_sem *sem)
       
   361 {
       
   362 	int semval;
       
   363 	Uint32 value;
       
   364 	
       
   365 	value = 0;
       
   366 	if ( sem ) {
       
   367 	  tryagain:
       
   368 #ifdef _SGI_SOURCE
       
   369 		semval = semctl(sem->id, 0, GETVAL);
       
   370 #else
       
   371 		{
       
   372 		union semun arg;
       
   373 		arg.val = 0;
       
   374 		semval = semctl(sem->id, 0, GETVAL, arg);
       
   375 		}
       
   376 #endif
       
   377 		if ( semval < 0 ) {
       
   378 			if ( errno == EINTR ) {
       
   379 				goto tryagain;
       
   380 			}
       
   381 		} else {
       
   382 			value = (Uint32)semval;
       
   383 		}
       
   384 	}
       
   385 	return value;
       
   386 }
       
   387 
       
   388 int SDL_SemPost(SDL_sem *sem)
       
   389 {
       
   390 	int retval;
       
   391 
       
   392 	if ( ! sem ) {
       
   393 		SDL_SetError("Passed a NULL semaphore");
       
   394 		return -1;
       
   395 	}
       
   396 
       
   397 	retval = 0;
       
   398   tryagain:
       
   399 	if ( semop(sem->id, op_post, 1) < 0 ) {
       
   400 		if ( errno == EINTR ) {
       
   401 			goto tryagain;
       
   402 		}
       
   403 		SDL_SetError("Semaphore operation error");
       
   404 		retval = -1;
       
   405 	}
       
   406 	return retval;
       
   407 }
       
   408 
       
   409 #endif /* SDL_USE_PTHREADS */