[Python-Dev] POSIX thread code

Gerald S. Williams gsw@agere.com
Wed, 27 Feb 2002 17:54:32 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_0023_01C1BFB7.CF9678F0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I recently came up with a fix for thread support in Python
under Cygwin. Jason Tishler and Norman Vine are looking it
over, but I'm pretty sure something similar should be used
for the Cygwin Python port.

This is easily done--simply add a few lines to thread.c
and create a new thread_cygwin.h (context diff and new file
both provided).

But there is a larger issue:

The thread interface code in thread_pthread.h uses mutexes
and condition variables to emulate semaphores, which are
then used to provide Python "lock" and "sema" services.

I know this is a common practice since those two thread
synchronization primitives are defined in "pthread.h". But
it comes with quite a bit of overhead. (And in the case of
Cygwin causes race conditions, but that's another matter.)

POSIX does define semaphores, though. (In fact, it's in
the standard just before Mutexes and Condition Variables.)
According to POSIX, they are found in <semaphore.h> and
_POSIX_SEMAPHORES should be defined if they work as POSIX
expects.

If they are available, it seems like providing direct
semaphore services would be preferable to emulating them
using condition variables and mutexes.

thread_posix.h.diff-c is a context diff that can be used
to convert thread_pthread.h into a more general POSIX
version that will use semaphores if available.

thread_cygwin.h would no longer be needed then, since all
it does is uses POSIX semaphores directly rather than
mutexes/condition vars. Changing the interface to POSIX
threads should bring a performance improvement to any
POSIX platform that supports semaphores directly.

Does this sound like a good idea? Should I create a
more thorough set of patch files and submit them?

(I haven't been accepted to the python-dev list yet, so
please CC me. Thanks.)

-Jerry

-O Gerald S. Williams, 22Y-103GA : mailto:gsw@agere.com O-
-O AGERE SYSTEMS, 555 UNION BLVD : office:610-712-8661  O-
-O ALLENTOWN, PA, USA 18109-3286 : mobile:908-672-7592  O-

------=_NextPart_000_0023_01C1BFB7.CF9678F0
Content-Type: application/octet-stream;
	name="thread.c.diff-c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="thread.c.diff-c"

*** thread.c	Tue Oct 16 17:13:49 2001
--- thread.c.new	Tue Feb 26 07:49:13 2002
***************
*** 113,118 ****
--- 113,123 ----
  #include "thread_pth.h"
  #endif
  
+ #ifdef __CYGWIN__
+ #include "thread_cygwin.h"
+ #undef _POSIX_THREADS
+ #endif
+ 
  #ifdef _POSIX_THREADS
  #include "thread_pthread.h"
  #endif

------=_NextPart_000_0023_01C1BFB7.CF9678F0
Content-Type: application/octet-stream;
	name="thread_cygwin.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="thread_cygwin.h"


/* Posix threads interface */

/*
 * Modified to avoid condition variables, which cause race conditions in Cygwin.
 * Gerald Williams, gsw@agere.com
 * $Id: thread_cygwin.h,v 1.6 2002/02/27 19:34:08 gsw Exp $
 */

#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>


/* try to determine what version of the Pthread Standard is installed.
 * this is important, since all sorts of parameter types changed from
 * draft to draft and there are several (incompatible) drafts in
 * common use.	these macros are a start, at least. 
 * 12 May 1997 -- david arnold <davida@pobox.com>
 */

#if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
#  define PY_PTHREAD_D4

#elif defined(__osf__) && defined (__alpha)
/* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
#  if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
#	 define PY_PTHREAD_D4
#  else
#	 define PY_PTHREAD_STD
#  endif

#elif defined(_AIX)
/* SCHED_BG_NP is defined if using AIX DCE pthreads
 * but it is unsupported by AIX 4 pthreads. Default
 * attributes for AIX 4 pthreads equal to NULL. For
 * AIX DCE pthreads they should be left unchanged.
 */
#  if !defined(SCHED_BG_NP)
#	 define PY_PTHREAD_STD
#  else
#	 define PY_PTHREAD_D7
#  endif

#elif defined(__DGUX)
#  define PY_PTHREAD_D6

#elif defined(__hpux) && defined(_DECTHREADS_)
#  define PY_PTHREAD_D4

#else /* Default case */
#  define PY_PTHREAD_STD

#endif

#ifdef USE_GUSI
/* The Macintosh GUSI I/O library sets the stackspace to
** 20KB, much too low. We up it to 64K.
*/
#define THREAD_STACK_SIZE 0x10000
#endif


/* set default attribute object for different versions */

#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
#  define pthread_attr_default pthread_attr_default
#  define pthread_mutexattr_default pthread_mutexattr_default
#elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
#  define pthread_attr_default ((pthread_attr_t *)NULL)
#  define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
#endif


/* On platforms that don't use standard POSIX threads pthread_sigmask()
 * isn't present.  DEC threads uses sigprocmask() instead as do most
 * other UNIX International compliant systems that don't have the full
 * pthread implementation.
 */
#ifdef HAVE_PTHREAD_SIGMASK
#  define SET_THREAD_SIGMASK pthread_sigmask
#else
#  define SET_THREAD_SIGMASK sigprocmask
#endif

#define CHECK_STATUS(name)	if (status != 0) { perror(name); error = 1; }

/*
 * Initialization.
 */

#ifdef _HAVE_BSDI
static
void _noop(void)
{
}

static void
PyThread__init_thread(void)
{
	/* DO AN INIT BY STARTING THE THREAD */
	static int dummy = 0;
	pthread_t thread1;
	pthread_create(&thread1, NULL, (void *) _noop, &dummy);
	pthread_join(thread1, NULL);
}

#else /* !_HAVE_BSDI */

static void
PyThread__init_thread(void)
{
#if defined(_AIX) && defined(__GNUC__)
	pthread_init();
#endif
}

#endif /* !_HAVE_BSDI */

/*
 * Thread support.
 */


long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
	pthread_t th;
	int success;
	sigset_t oldmask, newmask;
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
	pthread_attr_t attrs;
#endif
	dprintf(("PyThread_start_new_thread called\n"));
	if (!initialized)
		PyThread_init_thread();

#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
	pthread_attr_init(&attrs);
#endif
#ifdef THREAD_STACK_SIZE
	pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
#endif
#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
		pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
#endif

	/* Mask all signals in the current thread before creating the new
	 * thread.	This causes the new thread to start with all signals
	 * blocked.
	 */
	sigfillset(&newmask);
	SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);

	success = pthread_create(&th, 
#if defined(PY_PTHREAD_D4)
				 pthread_attr_default,
				 (pthread_startroutine_t)func, 
				 (pthread_addr_t)arg
#elif defined(PY_PTHREAD_D6)
				 pthread_attr_default,
				 (void* (*)(void *))func,
				 arg
#elif defined(PY_PTHREAD_D7)
				 pthread_attr_default,
				 func,
				 arg
#elif defined(PY_PTHREAD_STD)
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
				 &attrs,
#else
				 (pthread_attr_t*)NULL,
#endif
				 (void* (*)(void *))func,
				 (void *)arg
#endif
				 );

	/* Restore signal mask for original thread */
	SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);

#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
	pthread_attr_destroy(&attrs);
#endif
	if (success == 0) {
#if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
		pthread_detach(&th);
#elif defined(PY_PTHREAD_STD)
		pthread_detach(th);
#endif
	}
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
	return (long) th;
#else
	return (long) *(long *) &th;
#endif
}

/* XXX This implementation is considered (to quote Tim Peters) "inherently
   hosed" because:
	 - It does not guanrantee the promise that a non-zero integer is returned.
	 - The cast to long is inherently unsafe.
	 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
	   latter return statement (for Alpha OSF/1) are any longer necessary.
*/
long 
PyThread_get_thread_ident(void)
{
	volatile pthread_t threadid;
	if (!initialized)
		PyThread_init_thread();
	/* Jump through some hoops for Alpha OSF/1 */
	threadid = pthread_self();
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
	return (long) threadid;
#else
	return (long) *(long *) &threadid;
#endif
}

static void 
do_PyThread_exit_thread(int no_cleanup)
{
	dprintf(("PyThread_exit_thread called\n"));
	if (!initialized) {
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	}
}

void 
PyThread_exit_thread(void)
{
	do_PyThread_exit_thread(0);
}

void 
PyThread__exit_thread(void)
{
	do_PyThread_exit_thread(1);
}

#ifndef NO_EXIT_PROG
static void 
do_PyThread_exit_prog(int status, int no_cleanup)
{
	dprintf(("PyThread_exit_prog(%d) called\n", status));
	if (!initialized)
		if (no_cleanup)
			_exit(status);
		else
			exit(status);
}

void 
PyThread_exit_prog(int status)
{
	do_PyThread_exit_prog(status, 0);
}

void 
PyThread__exit_prog(int status)
{
	do_PyThread_exit_prog(status, 1);
}
#endif /* NO_EXIT_PROG */

/*
 * Lock support.
 */

PyThread_type_lock 
PyThread_allocate_lock(void)
{
	sem_t *lock;
	int status, error = 0;

	dprintf(("PyThread_allocate_lock called\n"));
	if (!initialized)
		PyThread_init_thread();

	lock = (sem_t *)malloc(sizeof(sem_t));

	if (lock) {
		status = sem_init(lock,0,1);
		CHECK_STATUS("sem_init");

		if (error) {
			free((void *)lock);
			lock = NULL;
		}
	}

	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
	return (PyThread_type_lock)lock;
}

void 
PyThread_free_lock(PyThread_type_lock lock)
{
	sem_t *thelock = (sem_t *)lock;
	int status, error = 0;

	dprintf(("PyThread_free_lock(%p) called\n", lock));

	if (!thelock)
		return;

	status = sem_destroy(thelock);
	CHECK_STATUS("sem_destroy");

	free((void *)thelock);
}

int 
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
	int success;
	sem_t *thelock = (sem_t *)lock;
	int status, error = 0;

	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));

	if (waitflag) {
		status = sem_wait(thelock);
		CHECK_STATUS("sem_wait");
	} else {
		status = sem_trywait(thelock);
	}

	success = (status == 0) ? 1 : 0;

	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
	return success;
}

void 
PyThread_release_lock(PyThread_type_lock lock)
{
	sem_t *thelock = (sem_t *)lock;
	int status, error = 0;

	dprintf(("PyThread_release_lock(%p) called\n", lock));

	status = sem_post(thelock);
	CHECK_STATUS("sem_post");
}

/*
 * Semaphore support.
 */

PyThread_type_sema 
PyThread_allocate_sema(int value)
{
	sem_t *sema;
	int status, error = 0;

	dprintf(("PyThread_allocate_sema called\n"));
	if (!initialized)
		PyThread_init_thread();

	sema = (sem_t *)malloc(sizeof(sem_t));

	if (sema) {
		status = sem_init(sema,0,value);
		CHECK_STATUS("sem_init");

		if (error) {
			free((void *)sema);
			sema = NULL;
		}
	}
	dprintf(("PyThread_allocate_sema() -> %p\n",  sema));
	return (PyThread_type_sema)sema;
}

void 
PyThread_free_sema(PyThread_type_sema sema)
{
	int status, error = 0;
	sem_t *thesema = (sem_t *)sema;

	dprintf(("PyThread_free_sema(%p) called\n",  sema));

	if (!thesema)
		return;

	status = sem_destroy(thesema);
	CHECK_STATUS("sem_destroy");

	free((void *) thesema);
}

int 
PyThread_down_sema(PyThread_type_sema sema, int waitflag)
{
	int status, error = 0, success;
	sem_t *thesema = (sem_t *)sema;

	dprintf(("PyThread_down_sema(%p, %d) called\n",  sema, waitflag));

	if (waitflag) {
		status = sem_wait(thesema);
		CHECK_STATUS("sem_wait");
	} else {
		status = sem_trywait(thesema);
	}

	success = (status == 0) ? 1 : 0;

	dprintf(("PyThread_down_sema(%p) return\n",  sema));
	return success;
}

void 
PyThread_up_sema(PyThread_type_sema sema)
{
	int status, error = 0;
	sem_t *thesema = (sem_t *)sema;

	dprintf(("PyThread_up_sema(%p)\n",	sema));

	status = sem_post(thesema);
	CHECK_STATUS("sem_post");
}

------=_NextPart_000_0023_01C1BFB7.CF9678F0
Content-Type: application/octet-stream;
	name="thread_posix.diff-c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="thread_posix.diff-c"

*** thread_pthread.h	Wed Feb 27 17:35:11 2002
--- thread_posix.h	Wed Feb 27 17:39:30 2002
***************
*** 5,10 ****
--- 5,13 ----
  #include <string.h>
  #include <pthread.h>
  #include <signal.h>
+ #ifdef _POSIX_SEMAPHORES
+ #include <semaphore.h>
+ #endif
  
  
  /* try to determine what version of the Pthread Standard is installed.
***************
*** 288,293 ****
--- 291,457 ----
  }
  #endif /* NO_EXIT_PROG */
  
+ #ifdef _POSIX_SEMAPHORES
+ /*
+  * Lock support.
+  */
+ 
+ PyThread_type_lock 
+ PyThread_allocate_lock(void)
+ {
+ 	sem_t *lock;
+ 	int status, error = 0;
+ 
+ 	dprintf(("PyThread_allocate_lock called\n"));
+ 	if (!initialized)
+ 		PyThread_init_thread();
+ 
+ 	lock = (sem_t *)malloc(sizeof(sem_t));
+ 
+ 	if (lock) {
+ 		status = sem_init(lock,0,1);
+ 		CHECK_STATUS("sem_init");
+ 
+ 		if (error) {
+ 			free((void *)lock);
+ 			lock = NULL;
+ 		}
+ 	}
+ 
+ 	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
+ 	return (PyThread_type_lock)lock;
+ }
+ 
+ void 
+ PyThread_free_lock(PyThread_type_lock lock)
+ {
+ 	sem_t *thelock = (sem_t *)lock;
+ 	int status, error = 0;
+ 
+ 	dprintf(("PyThread_free_lock(%p) called\n", lock));
+ 
+ 	if (!thelock)
+ 		return;
+ 
+ 	status = sem_destroy(thelock);
+ 	CHECK_STATUS("sem_destroy");
+ 
+ 	free((void *)thelock);
+ }
+ 
+ int 
+ PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
+ {
+ 	int success;
+ 	sem_t *thelock = (sem_t *)lock;
+ 	int status, error = 0;
+ 
+ 	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
+ 
+ 	if (waitflag) {
+ 		status = sem_wait(thelock);
+ 		CHECK_STATUS("sem_wait");
+ 	} else {
+ 		status = sem_trywait(thelock);
+ 	}
+ 
+ 	success = (status == 0) ? 1 : 0;
+ 
+ 	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
+ 	return success;
+ }
+ 
+ void 
+ PyThread_release_lock(PyThread_type_lock lock)
+ {
+ 	sem_t *thelock = (sem_t *)lock;
+ 	int status, error = 0;
+ 
+ 	dprintf(("PyThread_release_lock(%p) called\n", lock));
+ 
+ 	status = sem_post(thelock);
+ 	CHECK_STATUS("sem_post");
+ }
+ 
+ /*
+  * Semaphore support.
+  */
+ 
+ PyThread_type_sema 
+ PyThread_allocate_sema(int value)
+ {
+ 	sem_t *sema;
+ 	int status, error = 0;
+ 
+ 	dprintf(("PyThread_allocate_sema called\n"));
+ 	if (!initialized)
+ 		PyThread_init_thread();
+ 
+ 	sema = (sem_t *)malloc(sizeof(sem_t));
+ 
+ 	if (sema) {
+ 		status = sem_init(sema,0,value);
+ 		CHECK_STATUS("sem_init");
+ 
+ 		if (error) {
+ 			free((void *)sema);
+ 			sema = NULL;
+ 		}
+ 	}
+ 	dprintf(("PyThread_allocate_sema() -> %p\n",  sema));
+ 	return (PyThread_type_sema)sema;
+ }
+ 
+ void 
+ PyThread_free_sema(PyThread_type_sema sema)
+ {
+ 	int status, error = 0;
+ 	sem_t *thesema = (sem_t *)sema;
+ 
+ 	dprintf(("PyThread_free_sema(%p) called\n",  sema));
+ 
+ 	if (!thesema)
+ 		return;
+ 
+ 	status = sem_destroy(thesema);
+ 	CHECK_STATUS("sem_destroy");
+ 
+ 	free((void *) thesema);
+ }
+ 
+ int 
+ PyThread_down_sema(PyThread_type_sema sema, int waitflag)
+ {
+ 	int status, error = 0, success;
+ 	sem_t *thesema = (sem_t *)sema;
+ 
+ 	dprintf(("PyThread_down_sema(%p, %d) called\n",  sema, waitflag));
+ 
+ 	if (waitflag) {
+ 		status = sem_wait(thesema);
+ 		CHECK_STATUS("sem_wait");
+ 	} else {
+ 		status = sem_trywait(thesema);
+ 	}
+ 
+ 	success = (status == 0) ? 1 : 0;
+ 
+ 	dprintf(("PyThread_down_sema(%p) return\n",  sema));
+ 	return success;
+ }
+ 
+ void 
+ PyThread_up_sema(PyThread_type_sema sema)
+ {
+ 	int status, error = 0;
+ 	sem_t *thesema = (sem_t *)sema;
+ 
+ 	dprintf(("PyThread_up_sema(%p)\n",	sema));
+ 
+ 	status = sem_post(thesema);
+ 	CHECK_STATUS("sem_post");
+ }
+ #else /* _POSIX_SEMAPHORES */
  /*
   * Lock support.
   */
***************
*** 497,499 ****
--- 661,664 ----
  	status = pthread_mutex_unlock(&thesema->mutex);
  	CHECK_STATUS("pthread_mutex_unlock");
  }
+ #endif /* _POSIX_SEMAPHORES */

------=_NextPart_000_0023_01C1BFB7.CF9678F0--