Patrice's fixes for GNU Pthread support
authorSam Lantinga
Mon, 01 Apr 2002 15:35:28 +0000
changeset 3291d74ddc90cb2
parent 328 dc21fa30faa9
child 330 5fed858d551c
Patrice's fixes for GNU Pthread support
configure.in
src/thread/SDL_thread.c
src/thread/pth/SDL_syscond.c
src/thread/pth/SDL_syscond_c.h
src/thread/pth/SDL_sysmutex.c
src/thread/pth/SDL_sysmutex_c.h
src/thread/pth/SDL_systhread.c
src/thread/pth/SDL_systhread_c.h
     1.1 --- a/configure.in	Sun Mar 31 04:07:05 2002 +0000
     1.2 +++ b/configure.in	Mon Apr 01 15:35:28 2002 +0000
     1.3 @@ -1265,6 +1265,7 @@
     1.4              PTH_LIBS=`$PTH_CONFIG --libs --all`
     1.5              SDL_CFLAGS="$SDL_CFLAGS $PTH_CFLAGS"
     1.6              SDL_LIBS="$SDL_LIBS $PTH_LIBS"
     1.7 +            CFLAGS="$CFLAGS -DENABLE_PTH"
     1.8              use_pth=yes
     1.9          fi
    1.10          AC_MSG_CHECKING(pth)
    1.11 @@ -2295,16 +2296,20 @@
    1.12              if test x$enable_pth = xyes; then
    1.13                  COPY_ARCH_SRC(src/thread, pth, SDL_systhread.c)
    1.14                  COPY_ARCH_SRC(src/thread, pth, SDL_systhread_c.h)
    1.15 +                COPY_ARCH_SRC(src/thread, pth, SDL_sysmutex.c)
    1.16 +                COPY_ARCH_SRC(src/thread, pth, SDL_sysmutex_c.h)
    1.17 +                COPY_ARCH_SRC(src/thread, pth, SDL_syscond.c)
    1.18 +                COPY_ARCH_SRC(src/thread, pth, SDL_syscond_c.h)
    1.19              else
    1.20                  COPY_ARCH_SRC(src/thread, generic, SDL_systhread.c)
    1.21                  COPY_ARCH_SRC(src/thread, generic, SDL_systhread_c.h)
    1.22 +                COPY_ARCH_SRC(src/thread, generic, SDL_sysmutex.c)
    1.23 +                COPY_ARCH_SRC(src/thread, generic, SDL_sysmutex_c.h)
    1.24 +                COPY_ARCH_SRC(src/thread, generic, SDL_syscond.c)
    1.25 +                COPY_ARCH_SRC(src/thread, generic, SDL_syscond_c.h)
    1.26              fi
    1.27 -            COPY_ARCH_SRC(src/thread, generic, SDL_sysmutex.c)
    1.28 -            COPY_ARCH_SRC(src/thread, generic, SDL_sysmutex_c.h)
    1.29 -            COPY_ARCH_SRC(src/thread, linux, SDL_syssem.c)
    1.30 +            COPY_ARCH_SRC(src/thread, generic, SDL_syssem.c)
    1.31              COPY_ARCH_SRC(src/thread, generic, SDL_syssem_c.h)
    1.32 -            COPY_ARCH_SRC(src/thread, generic, SDL_syscond.c)
    1.33 -            COPY_ARCH_SRC(src/thread, generic, SDL_syscond_c.h)
    1.34          fi
    1.35          # Set up files for the timer library
    1.36          if test x$enable_timers = xyes; then
     2.1 --- a/src/thread/SDL_thread.c	Sun Mar 31 04:07:05 2002 +0000
     2.2 +++ b/src/thread/SDL_thread.c	Mon Apr 01 15:35:28 2002 +0000
     2.3 @@ -52,6 +52,12 @@
     2.4  {
     2.5  	int retval;
     2.6  
     2.7 +#ifdef ENABLE_PTH
     2.8 +	if (!pth_init()) {
     2.9 +		return -1;
    2.10 +	}
    2.11 +#endif
    2.12 +
    2.13  	retval = 0;
    2.14  	/* Set the thread lock creation flag so that we can reuse an
    2.15  	   existing lock on the system - since this mutex never gets
    2.16 @@ -80,6 +86,10 @@
    2.17  	if ( mutex != NULL ) {
    2.18  		SDL_DestroyMutex(mutex);
    2.19  	}
    2.20 +
    2.21 +#ifdef ENABLE_PTH
    2.22 +	pth_kill();
    2.23 +#endif
    2.24  }
    2.25  
    2.26  /* Routines for manipulating the thread list */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/thread/pth/SDL_syscond.c	Mon Apr 01 15:35:28 2002 +0000
     3.3 @@ -0,0 +1,140 @@
     3.4 +/*
     3.5 + *	GNU pth conditions variables
     3.6 + *
     3.7 + *	Patrice Mandin
     3.8 + */
     3.9 +
    3.10 +#include <stdio.h>
    3.11 +#include <stdlib.h>
    3.12 +#include <pth.h>
    3.13 +
    3.14 +#include "SDL_error.h"
    3.15 +#include "SDL_thread.h"
    3.16 +#include "SDL_syscond_c.h"
    3.17 +#include "SDL_sysmutex_c.h"
    3.18 +
    3.19 +/* Create a condition variable */
    3.20 +SDL_cond * SDL_CreateCond(void)
    3.21 +{
    3.22 +	SDL_cond *cond;
    3.23 +
    3.24 +	cond = (SDL_cond *) malloc(sizeof(SDL_cond));
    3.25 +	if ( cond ) {
    3.26 +		if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
    3.27 +			SDL_SetError("pthread_cond_init() failed");
    3.28 +			free(cond);
    3.29 +			cond = NULL;
    3.30 +		}
    3.31 +	} else {
    3.32 +		SDL_OutOfMemory();
    3.33 +	}
    3.34 +	return(cond);
    3.35 +}
    3.36 +
    3.37 +/* Destroy a condition variable */
    3.38 +void SDL_DestroyCond(SDL_cond *cond)
    3.39 +{
    3.40 +	if ( cond ) {
    3.41 +		free(cond);
    3.42 +	}
    3.43 +}
    3.44 +
    3.45 +/* Restart one of the threads that are waiting on the condition variable */
    3.46 +int SDL_CondSignal(SDL_cond *cond)
    3.47 +{
    3.48 +	int retval;
    3.49 +
    3.50 +	if ( ! cond ) {
    3.51 +		SDL_SetError("Passed a NULL condition variable");
    3.52 +		return -1;
    3.53 +	}
    3.54 +
    3.55 +	retval = 0;
    3.56 +	if ( pth_cond_notify(&(cond->condpth_p), FALSE) != 0 ) {
    3.57 +		SDL_SetError("pth_cond_notify() failed");
    3.58 +		retval = -1;
    3.59 +	}
    3.60 +	return retval;
    3.61 +}
    3.62 +
    3.63 +/* Restart all threads that are waiting on the condition variable */
    3.64 +int SDL_CondBroadcast(SDL_cond *cond)
    3.65 +{
    3.66 +	int retval;
    3.67 +
    3.68 +	if ( ! cond ) {
    3.69 +		SDL_SetError("Passed a NULL condition variable");
    3.70 +		return -1;
    3.71 +	}
    3.72 +
    3.73 +	retval = 0;
    3.74 +	if ( pth_cond_notify(&(cond->condpth_p), TRUE) != 0 ) {
    3.75 +		SDL_SetError("pth_cond_notify() failed");
    3.76 +		retval = -1;
    3.77 +	}
    3.78 +	return retval;
    3.79 +}
    3.80 +
    3.81 +/* Wait on the condition variable for at most 'ms' milliseconds.
    3.82 +   The mutex must be locked before entering this function!
    3.83 +   The mutex is unlocked during the wait, and locked again after the wait.
    3.84 +
    3.85 +Typical use:
    3.86 +
    3.87 +Thread A:
    3.88 +	SDL_LockMutex(lock);
    3.89 +	while ( ! condition ) {
    3.90 +		SDL_CondWait(cond);
    3.91 +	}
    3.92 +	SDL_UnlockMutex(lock);
    3.93 +
    3.94 +Thread B:
    3.95 +	SDL_LockMutex(lock);
    3.96 +	...
    3.97 +	condition = true;
    3.98 +	...
    3.99 +	SDL_UnlockMutex(lock);
   3.100 + */
   3.101 +int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
   3.102 +{
   3.103 +	int retval;
   3.104 +	pth_event_t ev;
   3.105 +	int sec;
   3.106 +
   3.107 +	if ( ! cond ) {
   3.108 +		SDL_SetError("Passed a NULL condition variable");
   3.109 +		return -1;
   3.110 +	}
   3.111 +
   3.112 +	retval = 0;
   3.113 +
   3.114 +	sec = ms/1000;
   3.115 +	ev = pth_event(PTH_EVENT_TIME, pth_timeout(sec,(ms-sec*1000)*1000));
   3.116 +
   3.117 +	if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), ev) != 0 ) {
   3.118 +		SDL_SetError("pth_cond_await() failed");
   3.119 +		retval = -1;
   3.120 +	}
   3.121 +
   3.122 +    pth_event_free(ev, PTH_FREE_ALL);
   3.123 +
   3.124 +	return retval;
   3.125 +}
   3.126 +
   3.127 +/* Wait on the condition variable forever */
   3.128 +int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
   3.129 +{
   3.130 +	int retval;
   3.131 +
   3.132 +	if ( ! cond ) {
   3.133 +		SDL_SetError("Passed a NULL condition variable");
   3.134 +		return -1;
   3.135 +	}
   3.136 +
   3.137 +	retval = 0;
   3.138 +	if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), NULL) != 0 ) {
   3.139 +		SDL_SetError("pth_cond_await() failed");
   3.140 +		retval = -1;
   3.141 +	}
   3.142 +	return retval;
   3.143 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/thread/pth/SDL_syscond_c.h	Mon Apr 01 15:35:28 2002 +0000
     4.3 @@ -0,0 +1,9 @@
     4.4 +#ifndef _SDL_SYSCOND_C_H_
     4.5 +#define _SDL_SYSCOND_C_H_
     4.6 +
     4.7 +struct SDL_cond
     4.8 +{
     4.9 +	pth_cond_t	condpth_p;
    4.10 +};
    4.11 +
    4.12 +#endif /* _SDL_SYSCOND_C_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/thread/pth/SDL_sysmutex.c	Mon Apr 01 15:35:28 2002 +0000
     5.3 @@ -0,0 +1,67 @@
     5.4 +/*
     5.5 + *	GNU pth mutexes
     5.6 + *
     5.7 + *	Patrice Mandin
     5.8 + */
     5.9 +
    5.10 +#include <stdio.h>
    5.11 +#include <stdlib.h>
    5.12 +#include <pth.h>
    5.13 +
    5.14 +#include "SDL_error.h"
    5.15 +#include "SDL_mutex.h"
    5.16 +#include "SDL_sysmutex_c.h"
    5.17 +
    5.18 +/* Create a mutex */
    5.19 +SDL_mutex *SDL_CreateMutex(void)
    5.20 +{
    5.21 +	SDL_mutex *mutex;
    5.22 +
    5.23 +	/* Allocate mutex memory */
    5.24 +	mutex = (SDL_mutex *)malloc(sizeof(*mutex));
    5.25 +	if ( mutex ) {
    5.26 +		/* Create the mutex, with initial value signaled */
    5.27 +	    if (!pth_mutex_init(&(mutex->mutexpth_p))) {
    5.28 +			SDL_SetError("Couldn't create mutex");
    5.29 +			free(mutex);
    5.30 +			mutex = NULL;
    5.31 +		}
    5.32 +	} else {
    5.33 +		SDL_OutOfMemory();
    5.34 +	}
    5.35 +	return(mutex);
    5.36 +}
    5.37 +
    5.38 +/* Free the mutex */
    5.39 +void SDL_DestroyMutex(SDL_mutex *mutex)
    5.40 +{
    5.41 +	if ( mutex ) {
    5.42 +		free(mutex);
    5.43 +	}
    5.44 +}
    5.45 +
    5.46 +/* Lock the mutex */
    5.47 +int SDL_mutexP(SDL_mutex *mutex)
    5.48 +{
    5.49 +	if ( mutex == NULL ) {
    5.50 +		SDL_SetError("Passed a NULL mutex");
    5.51 +		return -1;
    5.52 +	}
    5.53 +
    5.54 +	pth_mutex_acquire(&(mutex->mutexpth_p), FALSE, NULL);
    5.55 +
    5.56 +	return(0);
    5.57 +}
    5.58 +
    5.59 +/* Unlock the mutex */
    5.60 +int SDL_mutexV(SDL_mutex *mutex)
    5.61 +{
    5.62 +	if ( mutex == NULL ) {
    5.63 +		SDL_SetError("Passed a NULL mutex");
    5.64 +		return -1;
    5.65 +	}
    5.66 +
    5.67 +    pth_mutex_release(&(mutex->mutexpth_p));
    5.68 +
    5.69 +	return(0);
    5.70 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/thread/pth/SDL_sysmutex_c.h	Mon Apr 01 15:35:28 2002 +0000
     6.3 @@ -0,0 +1,8 @@
     6.4 +#ifndef _SDL_SYSMUTEX_C_H_
     6.5 +#define _SDL_SYSMUTEX_C_H_
     6.6 +
     6.7 +struct SDL_mutex {
     6.8 +	pth_mutex_t mutexpth_p;
     6.9 +};
    6.10 +
    6.11 +#endif /* _SDL_SYSMUTEX_C_H_ */
     7.1 --- a/src/thread/pth/SDL_systhread.c	Sun Mar 31 04:07:05 2002 +0000
     7.2 +++ b/src/thread/pth/SDL_systhread.c	Mon Apr 01 15:35:28 2002 +0000
     7.3 @@ -25,7 +25,11 @@
     7.4   "@(#) $Id$";
     7.5  #endif
     7.6  
     7.7 -/* Pth thread management routines for SDL */
     7.8 +/*
     7.9 + *	GNU pth threads
    7.10 + *
    7.11 + *	Patrice Mandin
    7.12 + */
    7.13  
    7.14  #include "SDL_error.h"
    7.15  #include "SDL_thread.h"
    7.16 @@ -51,17 +55,16 @@
    7.17  {
    7.18  	pth_attr_t type;
    7.19  
    7.20 +	/* Create a new attribute */
    7.21  	type = pth_attr_new();
    7.22 -
    7.23 -	/* Set the thread attributes */
    7.24 -	if ( pth_attr_init(type) != 0 ) {
    7.25 +	if ( type == NULL ) {
    7.26  		SDL_SetError("Couldn't initialize pth attributes");
    7.27  		return(-1);
    7.28  	}
    7.29  	pth_attr_set(type, PTH_ATTR_JOINABLE, TRUE);
    7.30  
    7.31  	/* Create the thread and go! */
    7.32 -	if ( pth_spawn(type, RunThread, args) != 0 ) {
    7.33 +	if ( pth_spawn(type, RunThread, args) == NULL ) {
    7.34  		SDL_SetError("Not enough resources to create thread");
    7.35  		return(-1);
    7.36  	}
    7.37 @@ -72,6 +75,7 @@
    7.38  {
    7.39  	int i;
    7.40  	sigset_t mask;
    7.41 +	int oldstate;
    7.42  
    7.43  	/* Mask asynchronous signals for this thread */
    7.44  	sigemptyset(&mask);
    7.45 @@ -81,9 +85,7 @@
    7.46  	pth_sigmask(SIG_BLOCK, &mask, 0);
    7.47  
    7.48  	/* Allow ourselves to be asynchronously cancelled */
    7.49 -	{ int oldstate;
    7.50 -		pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    7.51 -	}
    7.52 +	pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    7.53  }
    7.54  
    7.55  /* WARNING:  This may not work for systems with 64-bit pid_t */
     8.1 --- a/src/thread/pth/SDL_systhread_c.h	Sun Mar 31 04:07:05 2002 +0000
     8.2 +++ b/src/thread/pth/SDL_systhread_c.h	Mon Apr 01 15:35:28 2002 +0000
     8.3 @@ -20,6 +20,11 @@
     8.4      slouken@libsdl.org
     8.5  */
     8.6  
     8.7 +#ifndef _SDL_SYSTHREAD_C_H_
     8.8 +#define _SDL_SYSTHREAD_C_H_
     8.9 +
    8.10  #include <pth.h>
    8.11  
    8.12  typedef pth_t SYS_ThreadHandle;
    8.13 +
    8.14 +#endif /* _SDL_SYSTHREAD_C_H_ */