src/thread/pthread/SDL_syssem.c
changeset 1630 eba962f9d603
parent 1627 aee7ea396f59
child 1662 782fd950bd46
child 1851 536b0704b7d8
equal deleted inserted replaced
1629:ef4a796e7f24 1630:eba962f9d603
    33 /* 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 */
    34 #include "../generic/SDL_syssem.c"
    34 #include "../generic/SDL_syssem.c"
    35 #else
    35 #else
    36 
    36 
    37 struct SDL_semaphore {
    37 struct SDL_semaphore {
    38 	sem_t *sem;
    38 	sem_t sem;
    39 };
    39 };
    40 
    40 
    41 /* Create a semaphore, initialized with value */
    41 /* Create a semaphore, initialized with value */
    42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    43 {
    43 {
    44 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    44 	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    45 	if ( sem ) {
    45 	if ( sem ) {
    46 		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
    46 		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    47 			SDL_SetError("sem_init() failed");
    47 			SDL_SetError("sem_init() failed");
    48 			SDL_free(sem);
    48 			SDL_free(sem);
    49 			sem = NULL;
    49 			sem = NULL;
    50 		} else {
       
    51 			sem->sem = &sem->sem_data;
       
    52 		}
    50 		}
    53 	} else {
    51 	} else {
    54 		SDL_OutOfMemory();
    52 		SDL_OutOfMemory();
    55 	}
    53 	}
    56 	return sem;
    54 	return sem;
    57 }
    55 }
    58 
    56 
    59 void SDL_DestroySemaphore(SDL_sem *sem)
    57 void SDL_DestroySemaphore(SDL_sem *sem)
    60 {
    58 {
    61 	if ( sem ) {
    59 	if ( sem ) {
    62 		sem_destroy(sem->sem);
    60 		sem_destroy(&sem->sem);
    63 		SDL_free(sem);
    61 		SDL_free(sem);
    64 	}
    62 	}
    65 }
    63 }
    66 
    64 
    67 int SDL_SemTryWait(SDL_sem *sem)
    65 int SDL_SemTryWait(SDL_sem *sem)
    71 	if ( ! sem ) {
    69 	if ( ! sem ) {
    72 		SDL_SetError("Passed a NULL semaphore");
    70 		SDL_SetError("Passed a NULL semaphore");
    73 		return -1;
    71 		return -1;
    74 	}
    72 	}
    75 	retval = SDL_MUTEX_TIMEDOUT;
    73 	retval = SDL_MUTEX_TIMEDOUT;
    76 	if ( sem_trywait(sem->sem) == 0 ) {
    74 	if ( sem_trywait(&sem->sem) == 0 ) {
    77 		retval = 0;
    75 		retval = 0;
    78 	}
    76 	}
    79 	return retval;
    77 	return retval;
    80 }
    78 }
    81 
    79 
    86 	if ( ! sem ) {
    84 	if ( ! sem ) {
    87 		SDL_SetError("Passed a NULL semaphore");
    85 		SDL_SetError("Passed a NULL semaphore");
    88 		return -1;
    86 		return -1;
    89 	}
    87 	}
    90 
    88 
    91 	retval = sem_wait(sem->sem);
    89 	retval = sem_wait(&sem->sem);
    92 	if ( retval < 0 ) {
    90 	if ( retval < 0 ) {
    93 		SDL_SetError("sem_wait() failed");
    91 		SDL_SetError("sem_wait() failed");
    94 	}
    92 	}
    95 	return retval;
    93 	return retval;
    96 }
    94 }
   128 
   126 
   129 Uint32 SDL_SemValue(SDL_sem *sem)
   127 Uint32 SDL_SemValue(SDL_sem *sem)
   130 {
   128 {
   131 	int ret = 0;
   129 	int ret = 0;
   132 	if ( sem ) {
   130 	if ( sem ) {
   133 		sem_getvalue(sem->sem, &ret);
   131 		sem_getvalue(&sem->sem, &ret);
   134 		if ( ret < 0 ) {
   132 		if ( ret < 0 ) {
   135 			ret = 0;
   133 			ret = 0;
   136 		}
   134 		}
   137 	}
   135 	}
   138 	return (Uint32)ret;
   136 	return (Uint32)ret;
   145 	if ( ! sem ) {
   143 	if ( ! sem ) {
   146 		SDL_SetError("Passed a NULL semaphore");
   144 		SDL_SetError("Passed a NULL semaphore");
   147 		return -1;
   145 		return -1;
   148 	}
   146 	}
   149 
   147 
   150 	retval = sem_post(sem->sem);
   148 	retval = sem_post(&sem->sem);
   151 	if ( retval < 0 ) {
   149 	if ( retval < 0 ) {
   152 		SDL_SetError("sem_post() failed");
   150 		SDL_SetError("sem_post() failed");
   153 	}
   151 	}
   154 	return retval;
   152 	return retval;
   155 }
   153 }