src/thread/SDL_thread.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/src/thread/SDL_thread.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/thread/SDL_thread.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -38,16 +38,17 @@
     1.4  static SDL_Thread **SDL_Threads = NULL;
     1.5  static SDL_mutex *thread_lock = NULL;
     1.6  
     1.7 -int SDL_ThreadsInit(void)
     1.8 +int
     1.9 +SDL_ThreadsInit (void)
    1.10  {
    1.11 -	int retval;
    1.12 +    int retval;
    1.13  
    1.14 -	retval = 0;
    1.15 -	thread_lock = SDL_CreateMutex();
    1.16 -	if ( thread_lock == NULL ) {
    1.17 -		retval = -1;
    1.18 -	}
    1.19 -	return(retval);
    1.20 +    retval = 0;
    1.21 +    thread_lock = SDL_CreateMutex ();
    1.22 +    if (thread_lock == NULL) {
    1.23 +        retval = -1;
    1.24 +    }
    1.25 +    return (retval);
    1.26  }
    1.27  
    1.28  /* This should never be called...
    1.29 @@ -55,244 +56,260 @@
    1.30     clean up threads here.  If any threads are still running after this call,
    1.31     they will no longer have access to any per-thread data.
    1.32   */
    1.33 -void SDL_ThreadsQuit(void)
    1.34 +void
    1.35 +SDL_ThreadsQuit (void)
    1.36  {
    1.37 -	SDL_mutex *mutex;
    1.38 +    SDL_mutex *mutex;
    1.39  
    1.40 -	mutex = thread_lock;
    1.41 -	thread_lock = NULL;
    1.42 -	if ( mutex != NULL ) {
    1.43 -		SDL_DestroyMutex(mutex);
    1.44 -	}
    1.45 +    mutex = thread_lock;
    1.46 +    thread_lock = NULL;
    1.47 +    if (mutex != NULL) {
    1.48 +        SDL_DestroyMutex (mutex);
    1.49 +    }
    1.50  }
    1.51  
    1.52  /* Routines for manipulating the thread list */
    1.53 -static void SDL_AddThread(SDL_Thread *thread)
    1.54 +static void
    1.55 +SDL_AddThread (SDL_Thread * thread)
    1.56  {
    1.57 -	/* WARNING:
    1.58 -	   If the very first threads are created simultaneously, then
    1.59 -	   there could be a race condition causing memory corruption.
    1.60 -	   In practice, this isn't a problem because by definition there
    1.61 -	   is only one thread running the first time this is called.
    1.62 -	*/
    1.63 -	if ( !thread_lock ) {
    1.64 -		if ( SDL_ThreadsInit() < 0 ) {
    1.65 -			return;
    1.66 -		}
    1.67 -	}
    1.68 -	SDL_mutexP(thread_lock);
    1.69 +    /* WARNING:
    1.70 +       If the very first threads are created simultaneously, then
    1.71 +       there could be a race condition causing memory corruption.
    1.72 +       In practice, this isn't a problem because by definition there
    1.73 +       is only one thread running the first time this is called.
    1.74 +     */
    1.75 +    if (!thread_lock) {
    1.76 +        if (SDL_ThreadsInit () < 0) {
    1.77 +            return;
    1.78 +        }
    1.79 +    }
    1.80 +    SDL_mutexP (thread_lock);
    1.81  
    1.82 -	/* Expand the list of threads, if necessary */
    1.83 +    /* Expand the list of threads, if necessary */
    1.84  #ifdef DEBUG_THREADS
    1.85 -	printf("Adding thread (%d already - %d max)\n",
    1.86 -			SDL_numthreads, SDL_maxthreads);
    1.87 +    printf ("Adding thread (%d already - %d max)\n",
    1.88 +            SDL_numthreads, SDL_maxthreads);
    1.89  #endif
    1.90 -	if ( SDL_numthreads == SDL_maxthreads ) {
    1.91 -		SDL_Thread **threads;
    1.92 -		threads = (SDL_Thread **)SDL_realloc(SDL_Threads,
    1.93 -			(SDL_maxthreads+ARRAY_CHUNKSIZE)*(sizeof *threads));
    1.94 -		if ( threads == NULL ) {
    1.95 -			SDL_OutOfMemory();
    1.96 -			goto done;
    1.97 -		}
    1.98 -		SDL_maxthreads += ARRAY_CHUNKSIZE;
    1.99 -		SDL_Threads = threads;
   1.100 -	}
   1.101 -	SDL_Threads[SDL_numthreads++] = thread;
   1.102 -done:
   1.103 -	SDL_mutexV(thread_lock);
   1.104 +    if (SDL_numthreads == SDL_maxthreads) {
   1.105 +        SDL_Thread **threads;
   1.106 +        threads = (SDL_Thread **) SDL_realloc (SDL_Threads,
   1.107 +                                               (SDL_maxthreads +
   1.108 +                                                ARRAY_CHUNKSIZE) *
   1.109 +                                               (sizeof *threads));
   1.110 +        if (threads == NULL) {
   1.111 +            SDL_OutOfMemory ();
   1.112 +            goto done;
   1.113 +        }
   1.114 +        SDL_maxthreads += ARRAY_CHUNKSIZE;
   1.115 +        SDL_Threads = threads;
   1.116 +    }
   1.117 +    SDL_Threads[SDL_numthreads++] = thread;
   1.118 +  done:
   1.119 +    SDL_mutexV (thread_lock);
   1.120  }
   1.121  
   1.122 -static void SDL_DelThread(SDL_Thread *thread)
   1.123 +static void
   1.124 +SDL_DelThread (SDL_Thread * thread)
   1.125  {
   1.126 -	int i;
   1.127 +    int i;
   1.128  
   1.129 -	if ( !thread_lock ) {
   1.130 -		return;
   1.131 -	}
   1.132 -	SDL_mutexP(thread_lock);
   1.133 -	for ( i=0; i<SDL_numthreads; ++i ) {
   1.134 -		if ( thread == SDL_Threads[i] ) {
   1.135 -			break;
   1.136 -		}
   1.137 -	}
   1.138 -	if ( i < SDL_numthreads ) {
   1.139 -		if ( --SDL_numthreads > 0 ) {
   1.140 -			while ( i < SDL_numthreads ) {
   1.141 -				SDL_Threads[i] = SDL_Threads[i+1];
   1.142 -				++i;
   1.143 -			}
   1.144 -		} else {
   1.145 -			SDL_maxthreads = 0;
   1.146 -			SDL_free(SDL_Threads);
   1.147 -			SDL_Threads = NULL;
   1.148 -		}
   1.149 +    if (!thread_lock) {
   1.150 +        return;
   1.151 +    }
   1.152 +    SDL_mutexP (thread_lock);
   1.153 +    for (i = 0; i < SDL_numthreads; ++i) {
   1.154 +        if (thread == SDL_Threads[i]) {
   1.155 +            break;
   1.156 +        }
   1.157 +    }
   1.158 +    if (i < SDL_numthreads) {
   1.159 +        if (--SDL_numthreads > 0) {
   1.160 +            while (i < SDL_numthreads) {
   1.161 +                SDL_Threads[i] = SDL_Threads[i + 1];
   1.162 +                ++i;
   1.163 +            }
   1.164 +        } else {
   1.165 +            SDL_maxthreads = 0;
   1.166 +            SDL_free (SDL_Threads);
   1.167 +            SDL_Threads = NULL;
   1.168 +        }
   1.169  #ifdef DEBUG_THREADS
   1.170 -		printf("Deleting thread (%d left - %d max)\n",
   1.171 -				SDL_numthreads, SDL_maxthreads);
   1.172 +        printf ("Deleting thread (%d left - %d max)\n",
   1.173 +                SDL_numthreads, SDL_maxthreads);
   1.174  #endif
   1.175 -	}
   1.176 -	SDL_mutexV(thread_lock);
   1.177 +    }
   1.178 +    SDL_mutexV (thread_lock);
   1.179  
   1.180 -	if ( SDL_Threads == NULL ) {
   1.181 -		SDL_ThreadsQuit();
   1.182 -	}
   1.183 +    if (SDL_Threads == NULL) {
   1.184 +        SDL_ThreadsQuit ();
   1.185 +    }
   1.186  }
   1.187  
   1.188  /* The default (non-thread-safe) global error variable */
   1.189  static SDL_error SDL_global_error;
   1.190  
   1.191  /* Routine to get the thread-specific error variable */
   1.192 -SDL_error *SDL_GetErrBuf(void)
   1.193 +SDL_error *
   1.194 +SDL_GetErrBuf (void)
   1.195  {
   1.196 -	SDL_error *errbuf;
   1.197 +    SDL_error *errbuf;
   1.198  
   1.199 -	errbuf = &SDL_global_error;
   1.200 -	if ( SDL_Threads ) {
   1.201 -		int i;
   1.202 -		Uint32 this_thread;
   1.203 +    errbuf = &SDL_global_error;
   1.204 +    if (SDL_Threads) {
   1.205 +        int i;
   1.206 +        Uint32 this_thread;
   1.207  
   1.208 -		this_thread = SDL_ThreadID();
   1.209 -		SDL_mutexP(thread_lock);
   1.210 -		for ( i=0; i<SDL_numthreads; ++i ) {
   1.211 -			if ( this_thread == SDL_Threads[i]->threadid ) {
   1.212 -				errbuf = &SDL_Threads[i]->errbuf;
   1.213 -				break;
   1.214 -			}
   1.215 -		}
   1.216 -		SDL_mutexV(thread_lock);
   1.217 -	}
   1.218 -	return(errbuf);
   1.219 +        this_thread = SDL_ThreadID ();
   1.220 +        SDL_mutexP (thread_lock);
   1.221 +        for (i = 0; i < SDL_numthreads; ++i) {
   1.222 +            if (this_thread == SDL_Threads[i]->threadid) {
   1.223 +                errbuf = &SDL_Threads[i]->errbuf;
   1.224 +                break;
   1.225 +            }
   1.226 +        }
   1.227 +        SDL_mutexV (thread_lock);
   1.228 +    }
   1.229 +    return (errbuf);
   1.230  }
   1.231  
   1.232  
   1.233  /* Arguments and callback to setup and run the user thread function */
   1.234 -typedef struct {
   1.235 -	int (SDLCALL *func)(void *);
   1.236 -	void *data;
   1.237 -	SDL_Thread *info;
   1.238 -	SDL_sem *wait;
   1.239 +typedef struct
   1.240 +{
   1.241 +    int (SDLCALL * func) (void *);
   1.242 +    void *data;
   1.243 +    SDL_Thread *info;
   1.244 +    SDL_sem *wait;
   1.245  } thread_args;
   1.246  
   1.247 -void SDL_RunThread(void *data)
   1.248 +void
   1.249 +SDL_RunThread (void *data)
   1.250  {
   1.251 -	thread_args *args;
   1.252 -	int (SDLCALL *userfunc)(void *);
   1.253 -	void *userdata;
   1.254 -	int *statusloc;
   1.255 +    thread_args *args;
   1.256 +    int (SDLCALL * userfunc) (void *);
   1.257 +    void *userdata;
   1.258 +    int *statusloc;
   1.259  
   1.260 -	/* Perform any system-dependent setup
   1.261 -	   - this function cannot fail, and cannot use SDL_SetError()
   1.262 -	 */
   1.263 -	SDL_SYS_SetupThread();
   1.264 +    /* Perform any system-dependent setup
   1.265 +       - this function cannot fail, and cannot use SDL_SetError()
   1.266 +     */
   1.267 +    SDL_SYS_SetupThread ();
   1.268  
   1.269 -	/* Get the thread id */
   1.270 -	args = (thread_args *)data;
   1.271 -	args->info->threadid = SDL_ThreadID();
   1.272 +    /* Get the thread id */
   1.273 +    args = (thread_args *) data;
   1.274 +    args->info->threadid = SDL_ThreadID ();
   1.275  
   1.276 -	/* Figure out what function to run */
   1.277 -	userfunc = args->func;
   1.278 -	userdata = args->data;
   1.279 -	statusloc = &args->info->status;
   1.280 +    /* Figure out what function to run */
   1.281 +    userfunc = args->func;
   1.282 +    userdata = args->data;
   1.283 +    statusloc = &args->info->status;
   1.284  
   1.285 -	/* Wake up the parent thread */
   1.286 -	SDL_SemPost(args->wait);
   1.287 +    /* Wake up the parent thread */
   1.288 +    SDL_SemPost (args->wait);
   1.289  
   1.290 -	/* Run the function */
   1.291 -	*statusloc = userfunc(userdata);
   1.292 +    /* Run the function */
   1.293 +    *statusloc = userfunc (userdata);
   1.294  }
   1.295  
   1.296  #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
   1.297  #undef SDL_CreateThread
   1.298 -DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
   1.299 +DECLSPEC SDL_Thread *SDLCALL
   1.300 +SDL_CreateThread (int (SDLCALL * fn) (void *), void *data,
   1.301 +                  pfnSDL_CurrentBeginThread pfnBeginThread,
   1.302 +                  pfnSDL_CurrentEndThread pfnEndThread)
   1.303  #else
   1.304 -DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
   1.305 +DECLSPEC SDL_Thread *SDLCALL
   1.306 +SDL_CreateThread (int (SDLCALL * fn) (void *), void *data)
   1.307  #endif
   1.308  {
   1.309 -	SDL_Thread *thread;
   1.310 -	thread_args *args;
   1.311 -	int ret;
   1.312 +    SDL_Thread *thread;
   1.313 +    thread_args *args;
   1.314 +    int ret;
   1.315  
   1.316 -	/* Allocate memory for the thread info structure */
   1.317 -	thread = (SDL_Thread *)SDL_malloc(sizeof(*thread));
   1.318 -	if ( thread == NULL ) {
   1.319 -		SDL_OutOfMemory();
   1.320 -		return(NULL);
   1.321 -	}
   1.322 -	SDL_memset(thread, 0, (sizeof *thread));
   1.323 -	thread->status = -1;
   1.324 +    /* Allocate memory for the thread info structure */
   1.325 +    thread = (SDL_Thread *) SDL_malloc (sizeof (*thread));
   1.326 +    if (thread == NULL) {
   1.327 +        SDL_OutOfMemory ();
   1.328 +        return (NULL);
   1.329 +    }
   1.330 +    SDL_memset (thread, 0, (sizeof *thread));
   1.331 +    thread->status = -1;
   1.332  
   1.333 -	/* Set up the arguments for the thread */
   1.334 -	args = (thread_args *)SDL_malloc(sizeof(*args));
   1.335 -	if ( args == NULL ) {
   1.336 -		SDL_OutOfMemory();
   1.337 -		SDL_free(thread);
   1.338 -		return(NULL);
   1.339 -	}
   1.340 -	args->func = fn;
   1.341 -	args->data = data;
   1.342 -	args->info = thread;
   1.343 -	args->wait = SDL_CreateSemaphore(0);
   1.344 -	if ( args->wait == NULL ) {
   1.345 -		SDL_free(thread);
   1.346 -		SDL_free(args);
   1.347 -		return(NULL);
   1.348 -	}
   1.349 +    /* Set up the arguments for the thread */
   1.350 +    args = (thread_args *) SDL_malloc (sizeof (*args));
   1.351 +    if (args == NULL) {
   1.352 +        SDL_OutOfMemory ();
   1.353 +        SDL_free (thread);
   1.354 +        return (NULL);
   1.355 +    }
   1.356 +    args->func = fn;
   1.357 +    args->data = data;
   1.358 +    args->info = thread;
   1.359 +    args->wait = SDL_CreateSemaphore (0);
   1.360 +    if (args->wait == NULL) {
   1.361 +        SDL_free (thread);
   1.362 +        SDL_free (args);
   1.363 +        return (NULL);
   1.364 +    }
   1.365  
   1.366 -	/* Add the thread to the list of available threads */
   1.367 -	SDL_AddThread(thread);
   1.368 +    /* Add the thread to the list of available threads */
   1.369 +    SDL_AddThread (thread);
   1.370  
   1.371 -	/* Create the thread and go! */
   1.372 +    /* Create the thread and go! */
   1.373  #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
   1.374 -	ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
   1.375 +    ret = SDL_SYS_CreateThread (thread, args, pfnBeginThread, pfnEndThread);
   1.376  #else
   1.377 -	ret = SDL_SYS_CreateThread(thread, args);
   1.378 +    ret = SDL_SYS_CreateThread (thread, args);
   1.379  #endif
   1.380 -	if ( ret >= 0 ) {
   1.381 -		/* Wait for the thread function to use arguments */
   1.382 -		SDL_SemWait(args->wait);
   1.383 -	} else {
   1.384 -		/* Oops, failed.  Gotta free everything */
   1.385 -		SDL_DelThread(thread);
   1.386 -		SDL_free(thread);
   1.387 -		thread = NULL;
   1.388 -	}
   1.389 -	SDL_DestroySemaphore(args->wait);
   1.390 -	SDL_free(args);
   1.391 +    if (ret >= 0) {
   1.392 +        /* Wait for the thread function to use arguments */
   1.393 +        SDL_SemWait (args->wait);
   1.394 +    } else {
   1.395 +        /* Oops, failed.  Gotta free everything */
   1.396 +        SDL_DelThread (thread);
   1.397 +        SDL_free (thread);
   1.398 +        thread = NULL;
   1.399 +    }
   1.400 +    SDL_DestroySemaphore (args->wait);
   1.401 +    SDL_free (args);
   1.402  
   1.403 -	/* Everything is running now */
   1.404 -	return(thread);
   1.405 +    /* Everything is running now */
   1.406 +    return (thread);
   1.407  }
   1.408  
   1.409 -void SDL_WaitThread(SDL_Thread *thread, int *status)
   1.410 +void
   1.411 +SDL_WaitThread (SDL_Thread * thread, int *status)
   1.412  {
   1.413 -	if ( thread ) {
   1.414 -		SDL_SYS_WaitThread(thread);
   1.415 -		if ( status ) {
   1.416 -			*status = thread->status;
   1.417 -		}
   1.418 -		SDL_DelThread(thread);
   1.419 -		SDL_free(thread);
   1.420 -	}
   1.421 +    if (thread) {
   1.422 +        SDL_SYS_WaitThread (thread);
   1.423 +        if (status) {
   1.424 +            *status = thread->status;
   1.425 +        }
   1.426 +        SDL_DelThread (thread);
   1.427 +        SDL_free (thread);
   1.428 +    }
   1.429  }
   1.430  
   1.431 -Uint32 SDL_GetThreadID(SDL_Thread *thread)
   1.432 +Uint32
   1.433 +SDL_GetThreadID (SDL_Thread * thread)
   1.434  {
   1.435 -	Uint32 id;
   1.436 +    Uint32 id;
   1.437  
   1.438 -	if ( thread ) {
   1.439 -		id = thread->threadid;
   1.440 -	} else {
   1.441 -		id = SDL_ThreadID();
   1.442 -	}
   1.443 -	return(id);
   1.444 +    if (thread) {
   1.445 +        id = thread->threadid;
   1.446 +    } else {
   1.447 +        id = SDL_ThreadID ();
   1.448 +    }
   1.449 +    return (id);
   1.450  }
   1.451  
   1.452 -void SDL_KillThread(SDL_Thread *thread)
   1.453 +void
   1.454 +SDL_KillThread (SDL_Thread * thread)
   1.455  {
   1.456 -	if ( thread ) {
   1.457 -		SDL_SYS_KillThread(thread);
   1.458 -		SDL_WaitThread(thread, NULL);
   1.459 -	}
   1.460 +    if (thread) {
   1.461 +        SDL_SYS_KillThread (thread);
   1.462 +        SDL_WaitThread (thread, NULL);
   1.463 +    }
   1.464  }
   1.465  
   1.466 +/* vi: set ts=4 sw=4 expandtab: */