src/thread/pthread/SDL_syssem.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1630 eba962f9d603
child 1668 4da1ee79c9af
     1.1 --- a/src/thread/pthread/SDL_syssem.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/thread/pthread/SDL_syssem.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -34,122 +34,132 @@
     1.4  #include "../generic/SDL_syssem.c"
     1.5  #else
     1.6  
     1.7 -struct SDL_semaphore {
     1.8 -	sem_t sem;
     1.9 +struct SDL_semaphore
    1.10 +{
    1.11 +    sem_t sem;
    1.12  };
    1.13  
    1.14  /* Create a semaphore, initialized with value */
    1.15 -SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.16 +SDL_sem *
    1.17 +SDL_CreateSemaphore (Uint32 initial_value)
    1.18  {
    1.19 -	SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
    1.20 -	if ( sem ) {
    1.21 -		if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
    1.22 -			SDL_SetError("sem_init() failed");
    1.23 -			SDL_free(sem);
    1.24 -			sem = NULL;
    1.25 -		}
    1.26 -	} else {
    1.27 -		SDL_OutOfMemory();
    1.28 -	}
    1.29 -	return sem;
    1.30 +    SDL_sem *sem = (SDL_sem *) SDL_malloc (sizeof (SDL_sem));
    1.31 +    if (sem) {
    1.32 +        if (sem_init (&sem->sem, 0, initial_value) < 0) {
    1.33 +            SDL_SetError ("sem_init() failed");
    1.34 +            SDL_free (sem);
    1.35 +            sem = NULL;
    1.36 +        }
    1.37 +    } else {
    1.38 +        SDL_OutOfMemory ();
    1.39 +    }
    1.40 +    return sem;
    1.41  }
    1.42  
    1.43 -void SDL_DestroySemaphore(SDL_sem *sem)
    1.44 +void
    1.45 +SDL_DestroySemaphore (SDL_sem * sem)
    1.46  {
    1.47 -	if ( sem ) {
    1.48 -		sem_destroy(&sem->sem);
    1.49 -		SDL_free(sem);
    1.50 -	}
    1.51 +    if (sem) {
    1.52 +        sem_destroy (&sem->sem);
    1.53 +        SDL_free (sem);
    1.54 +    }
    1.55  }
    1.56  
    1.57 -int SDL_SemTryWait(SDL_sem *sem)
    1.58 +int
    1.59 +SDL_SemTryWait (SDL_sem * sem)
    1.60  {
    1.61 -	int retval;
    1.62 +    int retval;
    1.63  
    1.64 -	if ( ! sem ) {
    1.65 -		SDL_SetError("Passed a NULL semaphore");
    1.66 -		return -1;
    1.67 -	}
    1.68 -	retval = SDL_MUTEX_TIMEDOUT;
    1.69 -	if ( sem_trywait(&sem->sem) == 0 ) {
    1.70 -		retval = 0;
    1.71 -	}
    1.72 -	return retval;
    1.73 +    if (!sem) {
    1.74 +        SDL_SetError ("Passed a NULL semaphore");
    1.75 +        return -1;
    1.76 +    }
    1.77 +    retval = SDL_MUTEX_TIMEDOUT;
    1.78 +    if (sem_trywait (&sem->sem) == 0) {
    1.79 +        retval = 0;
    1.80 +    }
    1.81 +    return retval;
    1.82  }
    1.83  
    1.84 -int SDL_SemWait(SDL_sem *sem)
    1.85 +int
    1.86 +SDL_SemWait (SDL_sem * sem)
    1.87  {
    1.88 -	int retval;
    1.89 +    int retval;
    1.90  
    1.91 -	if ( ! sem ) {
    1.92 -		SDL_SetError("Passed a NULL semaphore");
    1.93 -		return -1;
    1.94 -	}
    1.95 +    if (!sem) {
    1.96 +        SDL_SetError ("Passed a NULL semaphore");
    1.97 +        return -1;
    1.98 +    }
    1.99  
   1.100 -	retval = sem_wait(&sem->sem);
   1.101 -	if ( retval < 0 ) {
   1.102 -		SDL_SetError("sem_wait() failed");
   1.103 -	}
   1.104 -	return retval;
   1.105 +    retval = sem_wait (&sem->sem);
   1.106 +    if (retval < 0) {
   1.107 +        SDL_SetError ("sem_wait() failed");
   1.108 +    }
   1.109 +    return retval;
   1.110  }
   1.111  
   1.112 -int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   1.113 +int
   1.114 +SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout)
   1.115  {
   1.116 -	int retval;
   1.117 +    int retval;
   1.118  
   1.119 -	if ( ! sem ) {
   1.120 -		SDL_SetError("Passed a NULL semaphore");
   1.121 -		return -1;
   1.122 -	}
   1.123 +    if (!sem) {
   1.124 +        SDL_SetError ("Passed a NULL semaphore");
   1.125 +        return -1;
   1.126 +    }
   1.127  
   1.128 -	/* Try the easy cases first */
   1.129 -	if ( timeout == 0 ) {
   1.130 -		return SDL_SemTryWait(sem);
   1.131 -	}
   1.132 -	if ( timeout == SDL_MUTEX_MAXWAIT ) {
   1.133 -		return SDL_SemWait(sem);
   1.134 -	}
   1.135 +    /* Try the easy cases first */
   1.136 +    if (timeout == 0) {
   1.137 +        return SDL_SemTryWait (sem);
   1.138 +    }
   1.139 +    if (timeout == SDL_MUTEX_MAXWAIT) {
   1.140 +        return SDL_SemWait (sem);
   1.141 +    }
   1.142  
   1.143 -	/* Ack!  We have to busy wait... */
   1.144 -	/* FIXME: Use sem_timedwait()? */
   1.145 -	timeout += SDL_GetTicks();
   1.146 -	do {
   1.147 -		retval = SDL_SemTryWait(sem);
   1.148 -		if ( retval == 0 ) {
   1.149 -			break;
   1.150 -		}
   1.151 -		SDL_Delay(1);
   1.152 -	} while ( SDL_GetTicks() < timeout );
   1.153 +    /* Ack!  We have to busy wait... */
   1.154 +    /* FIXME: Use sem_timedwait()? */
   1.155 +    timeout += SDL_GetTicks ();
   1.156 +    do {
   1.157 +        retval = SDL_SemTryWait (sem);
   1.158 +        if (retval == 0) {
   1.159 +            break;
   1.160 +        }
   1.161 +        SDL_Delay (1);
   1.162 +    }
   1.163 +    while (SDL_GetTicks () < timeout);
   1.164  
   1.165 -	return retval;
   1.166 +    return retval;
   1.167  }
   1.168  
   1.169 -Uint32 SDL_SemValue(SDL_sem *sem)
   1.170 +Uint32
   1.171 +SDL_SemValue (SDL_sem * sem)
   1.172  {
   1.173 -	int ret = 0;
   1.174 -	if ( sem ) {
   1.175 -		sem_getvalue(&sem->sem, &ret);
   1.176 -		if ( ret < 0 ) {
   1.177 -			ret = 0;
   1.178 -		}
   1.179 -	}
   1.180 -	return (Uint32)ret;
   1.181 +    int ret = 0;
   1.182 +    if (sem) {
   1.183 +        sem_getvalue (&sem->sem, &ret);
   1.184 +        if (ret < 0) {
   1.185 +            ret = 0;
   1.186 +        }
   1.187 +    }
   1.188 +    return (Uint32) ret;
   1.189  }
   1.190  
   1.191 -int SDL_SemPost(SDL_sem *sem)
   1.192 +int
   1.193 +SDL_SemPost (SDL_sem * sem)
   1.194  {
   1.195 -	int retval;
   1.196 +    int retval;
   1.197  
   1.198 -	if ( ! sem ) {
   1.199 -		SDL_SetError("Passed a NULL semaphore");
   1.200 -		return -1;
   1.201 -	}
   1.202 +    if (!sem) {
   1.203 +        SDL_SetError ("Passed a NULL semaphore");
   1.204 +        return -1;
   1.205 +    }
   1.206  
   1.207 -	retval = sem_post(&sem->sem);
   1.208 -	if ( retval < 0 ) {
   1.209 -		SDL_SetError("sem_post() failed");
   1.210 -	}
   1.211 -	return retval;
   1.212 +    retval = sem_post (&sem->sem);
   1.213 +    if (retval < 0) {
   1.214 +        SDL_SetError ("sem_post() failed");
   1.215 +    }
   1.216 +    return retval;
   1.217  }
   1.218  
   1.219  #endif /* __MACOSX__ */
   1.220 +/* vi: set ts=4 sw=4 expandtab: */