src/thread/generic/SDL_syssem.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1627 aee7ea396f59
child 1668 4da1ee79c9af
     1.1 --- a/src/thread/generic/SDL_syssem.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/thread/generic/SDL_syssem.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -30,180 +30,195 @@
     1.4  
     1.5  #if SDL_THREADS_DISABLED
     1.6  
     1.7 -SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
     1.8 +SDL_sem *
     1.9 +SDL_CreateSemaphore (Uint32 initial_value)
    1.10  {
    1.11 -	SDL_SetError("SDL not configured with thread support");
    1.12 -	return (SDL_sem *)0;
    1.13 +    SDL_SetError ("SDL not configured with thread support");
    1.14 +    return (SDL_sem *) 0;
    1.15  }
    1.16  
    1.17 -void SDL_DestroySemaphore(SDL_sem *sem)
    1.18 +void
    1.19 +SDL_DestroySemaphore (SDL_sem * sem)
    1.20  {
    1.21 -	return;
    1.22 +    return;
    1.23  }
    1.24  
    1.25 -int SDL_SemTryWait(SDL_sem *sem)
    1.26 +int
    1.27 +SDL_SemTryWait (SDL_sem * sem)
    1.28  {
    1.29 -	SDL_SetError("SDL not configured with thread support");
    1.30 -	return -1;
    1.31 +    SDL_SetError ("SDL not configured with thread support");
    1.32 +    return -1;
    1.33  }
    1.34  
    1.35 -int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
    1.36 +int
    1.37 +SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout)
    1.38  {
    1.39 -	SDL_SetError("SDL not configured with thread support");
    1.40 -	return -1;
    1.41 +    SDL_SetError ("SDL not configured with thread support");
    1.42 +    return -1;
    1.43  }
    1.44  
    1.45 -int SDL_SemWait(SDL_sem *sem)
    1.46 +int
    1.47 +SDL_SemWait (SDL_sem * sem)
    1.48  {
    1.49 -	SDL_SetError("SDL not configured with thread support");
    1.50 -	return -1;
    1.51 +    SDL_SetError ("SDL not configured with thread support");
    1.52 +    return -1;
    1.53  }
    1.54  
    1.55 -Uint32 SDL_SemValue(SDL_sem *sem)
    1.56 +Uint32
    1.57 +SDL_SemValue (SDL_sem * sem)
    1.58  {
    1.59 -	return 0;
    1.60 +    return 0;
    1.61  }
    1.62  
    1.63 -int SDL_SemPost(SDL_sem *sem)
    1.64 +int
    1.65 +SDL_SemPost (SDL_sem * sem)
    1.66  {
    1.67 -	SDL_SetError("SDL not configured with thread support");
    1.68 -	return -1;
    1.69 +    SDL_SetError ("SDL not configured with thread support");
    1.70 +    return -1;
    1.71  }
    1.72  
    1.73  #else
    1.74  
    1.75  struct SDL_semaphore
    1.76  {
    1.77 -	Uint32 count;
    1.78 -	Uint32 waiters_count;
    1.79 -	SDL_mutex *count_lock;
    1.80 -	SDL_cond *count_nonzero;
    1.81 +    Uint32 count;
    1.82 +    Uint32 waiters_count;
    1.83 +    SDL_mutex *count_lock;
    1.84 +    SDL_cond *count_nonzero;
    1.85  };
    1.86  
    1.87 -SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.88 +SDL_sem *
    1.89 +SDL_CreateSemaphore (Uint32 initial_value)
    1.90  {
    1.91 -	SDL_sem *sem;
    1.92 +    SDL_sem *sem;
    1.93  
    1.94 -	sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
    1.95 -	if ( ! sem ) {
    1.96 -		SDL_OutOfMemory();
    1.97 -		return NULL;
    1.98 -	}
    1.99 -	sem->count = initial_value;
   1.100 -	sem->waiters_count = 0;
   1.101 +    sem = (SDL_sem *) SDL_malloc (sizeof (*sem));
   1.102 +    if (!sem) {
   1.103 +        SDL_OutOfMemory ();
   1.104 +        return NULL;
   1.105 +    }
   1.106 +    sem->count = initial_value;
   1.107 +    sem->waiters_count = 0;
   1.108  
   1.109 -	sem->count_lock = SDL_CreateMutex();
   1.110 -	sem->count_nonzero = SDL_CreateCond();
   1.111 -	if ( ! sem->count_lock || ! sem->count_nonzero ) {
   1.112 -		SDL_DestroySemaphore(sem);
   1.113 -		return NULL;
   1.114 -	}
   1.115 +    sem->count_lock = SDL_CreateMutex ();
   1.116 +    sem->count_nonzero = SDL_CreateCond ();
   1.117 +    if (!sem->count_lock || !sem->count_nonzero) {
   1.118 +        SDL_DestroySemaphore (sem);
   1.119 +        return NULL;
   1.120 +    }
   1.121  
   1.122 -	return sem;
   1.123 +    return sem;
   1.124  }
   1.125  
   1.126  /* WARNING:
   1.127     You cannot call this function when another thread is using the semaphore.
   1.128  */
   1.129 -void SDL_DestroySemaphore(SDL_sem *sem)
   1.130 +void
   1.131 +SDL_DestroySemaphore (SDL_sem * sem)
   1.132  {
   1.133 -	if ( sem ) {
   1.134 -		sem->count = 0xFFFFFFFF;
   1.135 -		while ( sem->waiters_count > 0) {
   1.136 -			SDL_CondSignal(sem->count_nonzero);
   1.137 -			SDL_Delay(10);
   1.138 -		}
   1.139 -		SDL_DestroyCond(sem->count_nonzero);
   1.140 -		if ( sem->count_lock ) {
   1.141 -			SDL_mutexP(sem->count_lock);
   1.142 -			SDL_mutexV(sem->count_lock);
   1.143 -			SDL_DestroyMutex(sem->count_lock);
   1.144 -		}
   1.145 -		SDL_free(sem);
   1.146 -	}
   1.147 +    if (sem) {
   1.148 +        sem->count = 0xFFFFFFFF;
   1.149 +        while (sem->waiters_count > 0) {
   1.150 +            SDL_CondSignal (sem->count_nonzero);
   1.151 +            SDL_Delay (10);
   1.152 +        }
   1.153 +        SDL_DestroyCond (sem->count_nonzero);
   1.154 +        if (sem->count_lock) {
   1.155 +            SDL_mutexP (sem->count_lock);
   1.156 +            SDL_mutexV (sem->count_lock);
   1.157 +            SDL_DestroyMutex (sem->count_lock);
   1.158 +        }
   1.159 +        SDL_free (sem);
   1.160 +    }
   1.161  }
   1.162  
   1.163 -int SDL_SemTryWait(SDL_sem *sem)
   1.164 +int
   1.165 +SDL_SemTryWait (SDL_sem * sem)
   1.166  {
   1.167 -	int retval;
   1.168 +    int retval;
   1.169  
   1.170 -	if ( ! sem ) {
   1.171 -		SDL_SetError("Passed a NULL semaphore");
   1.172 -		return -1;
   1.173 -	}
   1.174 +    if (!sem) {
   1.175 +        SDL_SetError ("Passed a NULL semaphore");
   1.176 +        return -1;
   1.177 +    }
   1.178  
   1.179 -	retval = SDL_MUTEX_TIMEDOUT;
   1.180 -	SDL_LockMutex(sem->count_lock);
   1.181 -	if ( sem->count > 0 ) {
   1.182 -		--sem->count;
   1.183 -		retval = 0;
   1.184 -	}
   1.185 -	SDL_UnlockMutex(sem->count_lock);
   1.186 +    retval = SDL_MUTEX_TIMEDOUT;
   1.187 +    SDL_LockMutex (sem->count_lock);
   1.188 +    if (sem->count > 0) {
   1.189 +        --sem->count;
   1.190 +        retval = 0;
   1.191 +    }
   1.192 +    SDL_UnlockMutex (sem->count_lock);
   1.193  
   1.194 -	return retval;
   1.195 +    return retval;
   1.196  }
   1.197  
   1.198 -int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   1.199 +int
   1.200 +SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout)
   1.201  {
   1.202 -	int retval;
   1.203 +    int retval;
   1.204  
   1.205 -	if ( ! sem ) {
   1.206 -		SDL_SetError("Passed a NULL semaphore");
   1.207 -		return -1;
   1.208 -	}
   1.209 +    if (!sem) {
   1.210 +        SDL_SetError ("Passed a NULL semaphore");
   1.211 +        return -1;
   1.212 +    }
   1.213  
   1.214 -	/* A timeout of 0 is an easy case */
   1.215 -	if ( timeout == 0 ) {
   1.216 -		return SDL_SemTryWait(sem);
   1.217 -	}
   1.218 +    /* A timeout of 0 is an easy case */
   1.219 +    if (timeout == 0) {
   1.220 +        return SDL_SemTryWait (sem);
   1.221 +    }
   1.222  
   1.223 -	SDL_LockMutex(sem->count_lock);
   1.224 -	++sem->waiters_count;
   1.225 -	retval = 0;
   1.226 -	while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
   1.227 -		retval = SDL_CondWaitTimeout(sem->count_nonzero,
   1.228 -		                             sem->count_lock, timeout);
   1.229 -	}
   1.230 -	--sem->waiters_count;
   1.231 -	--sem->count;
   1.232 -	SDL_UnlockMutex(sem->count_lock);
   1.233 +    SDL_LockMutex (sem->count_lock);
   1.234 +    ++sem->waiters_count;
   1.235 +    retval = 0;
   1.236 +    while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
   1.237 +        retval = SDL_CondWaitTimeout (sem->count_nonzero,
   1.238 +                                      sem->count_lock, timeout);
   1.239 +    }
   1.240 +    --sem->waiters_count;
   1.241 +    --sem->count;
   1.242 +    SDL_UnlockMutex (sem->count_lock);
   1.243  
   1.244 -	return retval;
   1.245 +    return retval;
   1.246  }
   1.247  
   1.248 -int SDL_SemWait(SDL_sem *sem)
   1.249 +int
   1.250 +SDL_SemWait (SDL_sem * sem)
   1.251  {
   1.252 -	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   1.253 +    return SDL_SemWaitTimeout (sem, SDL_MUTEX_MAXWAIT);
   1.254  }
   1.255  
   1.256 -Uint32 SDL_SemValue(SDL_sem *sem)
   1.257 +Uint32
   1.258 +SDL_SemValue (SDL_sem * sem)
   1.259  {
   1.260 -	Uint32 value;
   1.261 -	
   1.262 -	value = 0;
   1.263 -	if ( sem ) {
   1.264 -		SDL_LockMutex(sem->count_lock);
   1.265 -		value = sem->count;
   1.266 -		SDL_UnlockMutex(sem->count_lock);
   1.267 -	}
   1.268 -	return value;
   1.269 +    Uint32 value;
   1.270 +
   1.271 +    value = 0;
   1.272 +    if (sem) {
   1.273 +        SDL_LockMutex (sem->count_lock);
   1.274 +        value = sem->count;
   1.275 +        SDL_UnlockMutex (sem->count_lock);
   1.276 +    }
   1.277 +    return value;
   1.278  }
   1.279  
   1.280 -int SDL_SemPost(SDL_sem *sem)
   1.281 +int
   1.282 +SDL_SemPost (SDL_sem * sem)
   1.283  {
   1.284 -	if ( ! sem ) {
   1.285 -		SDL_SetError("Passed a NULL semaphore");
   1.286 -		return -1;
   1.287 -	}
   1.288 +    if (!sem) {
   1.289 +        SDL_SetError ("Passed a NULL semaphore");
   1.290 +        return -1;
   1.291 +    }
   1.292  
   1.293 -	SDL_LockMutex(sem->count_lock);
   1.294 -	if ( sem->waiters_count > 0 ) {
   1.295 -		SDL_CondSignal(sem->count_nonzero);
   1.296 -	}
   1.297 -	++sem->count;
   1.298 -	SDL_UnlockMutex(sem->count_lock);
   1.299 +    SDL_LockMutex (sem->count_lock);
   1.300 +    if (sem->waiters_count > 0) {
   1.301 +        SDL_CondSignal (sem->count_nonzero);
   1.302 +    }
   1.303 +    ++sem->count;
   1.304 +    SDL_UnlockMutex (sem->count_lock);
   1.305  
   1.306 -	return 0;
   1.307 +    return 0;
   1.308  }
   1.309  
   1.310  #endif /* SDL_THREADS_DISABLED */
   1.311 +/* vi: set ts=4 sw=4 expandtab: */