src/thread/pthread/SDL_syssem.c
changeset 1895 c121d94672cb
parent 1851 536b0704b7d8
child 2343 375f9132da7f
child 2669 e27bdcc80744
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
    21 */
    21 */
    22 #include "SDL_config.h"
    22 #include "SDL_config.h"
    23 
    23 
    24 #include <pthread.h>
    24 #include <pthread.h>
    25 #include <semaphore.h>
    25 #include <semaphore.h>
    26 #include <errno.h>
       
    27 
    26 
    28 #include "SDL_thread.h"
    27 #include "SDL_thread.h"
    29 #include "SDL_timer.h"
    28 #include "SDL_timer.h"
    30 
    29 
    31 /* Wrapper around POSIX 1003.1b semaphores */
    30 /* Wrapper around POSIX 1003.1b semaphores */
    33 #ifdef __MACOSX__
    32 #ifdef __MACOSX__
    34 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    33 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
    35 #include "../generic/SDL_syssem.c"
    34 #include "../generic/SDL_syssem.c"
    36 #else
    35 #else
    37 
    36 
    38 struct SDL_semaphore {
    37 struct SDL_semaphore
    39 	sem_t sem;
    38 {
       
    39     sem_t sem;
    40 };
    40 };
    41 
    41 
    42 /* Create a semaphore, initialized with value */
    42 /* Create a semaphore, initialized with value */
    43 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    43 SDL_sem *
       
    44 SDL_CreateSemaphore(Uint32 initial_value)
    44 {
    45 {
    45 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    46     SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    46 	if ( sem ) {
    47     if (sem) {
    47 		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    48         if (sem_init(&sem->sem, 0, initial_value) < 0) {
    48 			SDL_SetError("sem_init() failed");
    49             SDL_SetError("sem_init() failed");
    49 			SDL_free(sem);
    50             SDL_free(sem);
    50 			sem = NULL;
    51             sem = NULL;
    51 		}
    52         }
    52 	} else {
    53     } else {
    53 		SDL_OutOfMemory();
    54         SDL_OutOfMemory();
    54 	}
    55     }
    55 	return sem;
    56     return sem;
    56 }
    57 }
    57 
    58 
    58 void SDL_DestroySemaphore(SDL_sem *sem)
    59 void
       
    60 SDL_DestroySemaphore(SDL_sem * sem)
    59 {
    61 {
    60 	if ( sem ) {
    62     if (sem) {
    61 		sem_destroy(&sem->sem);
    63         sem_destroy(&sem->sem);
    62 		SDL_free(sem);
    64         SDL_free(sem);
    63 	}
    65     }
    64 }
    66 }
    65 
    67 
    66 int SDL_SemTryWait(SDL_sem *sem)
    68 int
       
    69 SDL_SemTryWait(SDL_sem * sem)
    67 {
    70 {
    68 	int retval;
    71     int retval;
    69 
    72 
    70 	if ( ! sem ) {
    73     if (!sem) {
    71 		SDL_SetError("Passed a NULL semaphore");
    74         SDL_SetError("Passed a NULL semaphore");
    72 		return -1;
    75         return -1;
    73 	}
    76     }
    74 	retval = SDL_MUTEX_TIMEDOUT;
    77     retval = SDL_MUTEX_TIMEDOUT;
    75 	if ( sem_trywait(&sem->sem) == 0 ) {
    78     if (sem_trywait(&sem->sem) == 0) {
    76 		retval = 0;
    79         retval = 0;
    77 	}
    80     }
    78 	return retval;
    81     return retval;
    79 }
    82 }
    80 
    83 
    81 int SDL_SemWait(SDL_sem *sem)
    84 int
       
    85 SDL_SemWait(SDL_sem * sem)
    82 {
    86 {
    83 	int retval;
    87     int retval;
    84 
    88 
    85 	if ( ! sem ) {
    89     if (!sem) {
    86 		SDL_SetError("Passed a NULL semaphore");
    90         SDL_SetError("Passed a NULL semaphore");
    87 		return -1;
    91         return -1;
    88 	}
    92     }
    89 
    93 
    90 	while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
    94     retval = sem_wait(&sem->sem);
    91 	if ( retval < 0 ) {
    95     if (retval < 0) {
    92 		SDL_SetError("sem_wait() failed");
    96         SDL_SetError("sem_wait() failed");
    93 	}
    97     }
    94 	return retval;
    98     return retval;
    95 }
    99 }
    96 
   100 
    97 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   101 int
       
   102 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
    98 {
   103 {
    99 	int retval;
   104     int retval;
   100 
   105 
   101 	if ( ! sem ) {
   106     if (!sem) {
   102 		SDL_SetError("Passed a NULL semaphore");
   107         SDL_SetError("Passed a NULL semaphore");
   103 		return -1;
   108         return -1;
   104 	}
   109     }
   105 
   110 
   106 	/* Try the easy cases first */
   111     /* Try the easy cases first */
   107 	if ( timeout == 0 ) {
   112     if (timeout == 0) {
   108 		return SDL_SemTryWait(sem);
   113         return SDL_SemTryWait(sem);
   109 	}
   114     }
   110 	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   115     if (timeout == SDL_MUTEX_MAXWAIT) {
   111 		return SDL_SemWait(sem);
   116         return SDL_SemWait(sem);
   112 	}
   117     }
   113 
   118 
   114 	/* Ack!  We have to busy wait... */
   119     /* Ack!  We have to busy wait... */
   115 	/* FIXME: Use sem_timedwait()? */
   120     /* FIXME: Use sem_timedwait()? */
   116 	timeout += SDL_GetTicks();
   121     timeout += SDL_GetTicks();
   117 	do {
   122     do {
   118 		retval = SDL_SemTryWait(sem);
   123         retval = SDL_SemTryWait(sem);
   119 		if ( retval == 0 ) {
   124         if (retval == 0) {
   120 			break;
   125             break;
   121 		}
   126         }
   122 		SDL_Delay(1);
   127         SDL_Delay(1);
   123 	} while ( SDL_GetTicks() < timeout );
   128     }
       
   129     while (SDL_GetTicks() < timeout);
   124 
   130 
   125 	return retval;
   131     return retval;
   126 }
   132 }
   127 
   133 
   128 Uint32 SDL_SemValue(SDL_sem *sem)
   134 Uint32
       
   135 SDL_SemValue(SDL_sem * sem)
   129 {
   136 {
   130 	int ret = 0;
   137     int ret = 0;
   131 	if ( sem ) {
   138     if (sem) {
   132 		sem_getvalue(&sem->sem, &ret);
   139         sem_getvalue(&sem->sem, &ret);
   133 		if ( ret < 0 ) {
   140         if (ret < 0) {
   134 			ret = 0;
   141             ret = 0;
   135 		}
   142         }
   136 	}
   143     }
   137 	return (Uint32)ret;
   144     return (Uint32) ret;
   138 }
   145 }
   139 
   146 
   140 int SDL_SemPost(SDL_sem *sem)
   147 int
       
   148 SDL_SemPost(SDL_sem * sem)
   141 {
   149 {
   142 	int retval;
   150     int retval;
   143 
   151 
   144 	if ( ! sem ) {
   152     if (!sem) {
   145 		SDL_SetError("Passed a NULL semaphore");
   153         SDL_SetError("Passed a NULL semaphore");
   146 		return -1;
   154         return -1;
   147 	}
   155     }
   148 
   156 
   149 	retval = sem_post(&sem->sem);
   157     retval = sem_post(&sem->sem);
   150 	if ( retval < 0 ) {
   158     if (retval < 0) {
   151 		SDL_SetError("sem_post() failed");
   159         SDL_SetError("sem_post() failed");
   152 	}
   160     }
   153 	return retval;
   161     return retval;
   154 }
   162 }
   155 
   163 
   156 #endif /* __MACOSX__ */
   164 #endif /* __MACOSX__ */
       
   165 /* vi: set ts=4 sw=4 expandtab: */