view Python/thread_os2.h @ 44349:4dc01402d78a

Merged revisions 59541-59561 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59544 | raymond.hettinger | 2007-12-18 01:13:45 +0100 (Tue, 18 Dec 2007) | 1 line Add more namedtuple() test cases. Neaten the code and comments. ........ r59545 | christian.heimes | 2007-12-18 04:38:03 +0100 (Tue, 18 Dec 2007) | 3 lines Fixed for #1601: IDLE not working correctly on Windows (Py30a2/IDLE30a1) Amaury's ideas works great. Should we build the Python core with WINVER=0x0500 and _WIN32_WINNT=0x0500, too? ........ r59546 | christian.heimes | 2007-12-18 10:00:13 +0100 (Tue, 18 Dec 2007) | 1 line Make it a bit easier to test Tcl/Tk and idle from a build dir. ........ r59547 | christian.heimes | 2007-12-18 10:12:10 +0100 (Tue, 18 Dec 2007) | 1 line Removed several unused files from the PCbuild9 directory. They are relics from the past. ........ r59548 | raymond.hettinger | 2007-12-18 19:26:18 +0100 (Tue, 18 Dec 2007) | 29 lines Speed-up dictionary constructor by about 10%. New opcode, STORE_MAP saves the compiler from awkward stack manipulations and specializes for dicts using PyDict_SetItem instead of PyObject_SetItem. Old disassembly: 0 BUILD_MAP 0 3 DUP_TOP 4 LOAD_CONST 1 (1) 7 ROT_TWO 8 LOAD_CONST 2 ('x') 11 STORE_SUBSCR 12 DUP_TOP 13 LOAD_CONST 3 (2) 16 ROT_TWO 17 LOAD_CONST 4 ('y') 20 STORE_SUBSCR New disassembly: 0 BUILD_MAP 0 3 LOAD_CONST 1 (1) 6 LOAD_CONST 2 ('x') 9 STORE_MAP 10 LOAD_CONST 3 (2) 13 LOAD_CONST 4 ('y') 16 STORE_MAP ........ r59549 | thomas.heller | 2007-12-18 20:00:34 +0100 (Tue, 18 Dec 2007) | 2 lines Issue #1642: Fix segfault in ctypes when trying to delete attributes. ........ r59551 | guido.van.rossum | 2007-12-18 21:10:42 +0100 (Tue, 18 Dec 2007) | 2 lines Issue #1645 by Alberto Bertogli. Fix a comment. ........ r59553 | raymond.hettinger | 2007-12-18 22:24:09 +0100 (Tue, 18 Dec 2007) | 12 lines Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary. Allows dictionaries to be pre-sized (upto 255 elements) saving time lost to re-sizes with their attendant mallocs and re-insertions. Has zero effect on small dictionaries (5 elements or fewer), a slight benefit for dicts upto 22 elements (because they had to resize once anyway), and more benefit for dicts upto 255 elements (saving multiple resizes during the build-up and reducing the number of collisions on the first insertions). Beyond 255 elements, there is no addional benefit. ........ r59554 | christian.heimes | 2007-12-18 22:56:09 +0100 (Tue, 18 Dec 2007) | 1 line Fixed #1649: IDLE error: dictionary changed size during iteration ........ r59557 | raymond.hettinger | 2007-12-18 23:21:27 +0100 (Tue, 18 Dec 2007) | 1 line Simplify and speedup _asdict() for named tuples. ........ r59558 | christian.heimes | 2007-12-19 00:22:54 +0100 (Wed, 19 Dec 2007) | 3 lines Applied patch #1635: Float patch for inf and nan on Windows (and other platforms). The patch unifies float("inf") and repr(float("inf")) on all platforms. ........ r59559 | raymond.hettinger | 2007-12-19 00:51:15 +0100 (Wed, 19 Dec 2007) | 1 line Users demand iterable input for named tuples. The author capitulates. ........ r59560 | raymond.hettinger | 2007-12-19 01:21:06 +0100 (Wed, 19 Dec 2007) | 1 line Beef-up tests for dict literals ........ r59561 | raymond.hettinger | 2007-12-19 01:27:21 +0100 (Wed, 19 Dec 2007) | 1 line Zap a duplicate line ........
author Christian Heimes <christian@cheimes.de>
date Wed, 19 Dec 2007 02:07:34 +0000
parents 654c380cf8b9
children f197407bdba2 798e0d0c7246
line wrap: on
line source

/* This code implemented by cvale@netcom.com */

#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#include "os2.h"
#include "limits.h"

#include "process.h"

#if defined(PYCC_GCC)
#include <sys/builtin.h>
#include <sys/fmutex.h>
#else
long PyThread_get_thread_ident(void);
#endif

/* default thread stack size of 64kB */
#if !defined(THREAD_STACK_SIZE)
#define	THREAD_STACK_SIZE	0x10000
#endif

#define OS2_STACKSIZE(x)	(x ? x : THREAD_STACK_SIZE)

/*
 * Initialization of the C package, should not be needed.
 */
static void
PyThread__init_thread(void)
{
}

/*
 * Thread support.
 */
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
	int thread_id;

	thread_id = _beginthread(func,
				NULL,
				OS2_STACKSIZE(_pythread_stacksize),
				arg);

	if (thread_id == -1) {
		dprintf(("_beginthread failed. return %ld\n", errno));
	}

	return thread_id;
}

long
PyThread_get_thread_ident(void)
{
#if !defined(PYCC_GCC)
	PPIB pib;
	PTIB tib;
#endif

	if (!initialized)
		PyThread_init_thread();

#if defined(PYCC_GCC)
	return _gettid();
#else
	DosGetInfoBlocks(&tib, &pib);
	return tib->tib_ptib2->tib2_ultid;
#endif
}

static void
do_PyThread_exit_thread(int no_cleanup)
{
	dprintf(("%ld: PyThread_exit_thread called\n",
		 PyThread_get_thread_ident()));
	if (!initialized)
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	_endthread();
}

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.  This is implemented with an event semaphore and critical
 * sections to make it behave more like a posix mutex than its OS/2 
 * counterparts.
 */

typedef struct os2_lock_t {
	int is_set;
	HEV changed;
} *type_os2_lock;

PyThread_type_lock 
PyThread_allocate_lock(void)
{
#if defined(PYCC_GCC)
	_fmutex *sem = malloc(sizeof(_fmutex));
	if (!initialized)
		PyThread_init_thread();
	dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
		 PyThread_get_thread_ident(),
		 (long)sem));
	if (_fmutex_create(sem, 0)) {
		free(sem);
		sem = NULL;
	}
	return (PyThread_type_lock)sem;
#else
	APIRET rc;
	type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));

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

	lock->is_set = 0;

	DosCreateEventSem(NULL, &lock->changed, 0, 0);

	dprintf(("%ld: PyThread_allocate_lock() -> %p\n", 
		 PyThread_get_thread_ident(), 
        	 lock->changed));

	return (PyThread_type_lock)lock;
#endif
}

void 
PyThread_free_lock(PyThread_type_lock aLock)
{
#if !defined(PYCC_GCC)
	type_os2_lock lock = (type_os2_lock)aLock;
#endif

	dprintf(("%ld: PyThread_free_lock(%p) called\n",
		 PyThread_get_thread_ident(),aLock));

#if defined(PYCC_GCC)
	if (aLock) {
		_fmutex_close((_fmutex *)aLock);
		free((_fmutex *)aLock);
	}
#else
	DosCloseEventSem(lock->changed);
	free(aLock);
#endif
}

/*
 * Return 1 on success if the lock was acquired
 *
 * and 0 if the lock was not acquired.
 */
int 
PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
{
#if !defined(PYCC_GCC)
	int   done = 0;
	ULONG count;
	PID   pid = 0;
	TID   tid = 0;
	type_os2_lock lock = (type_os2_lock)aLock;
#endif

	dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
		 PyThread_get_thread_ident(),
		 aLock,
		 waitflag));

#if defined(PYCC_GCC)
	/* always successful if the lock doesn't exist */
	if (aLock &&
	    _fmutex_request((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT))
		return 0;
#else
	while (!done) {
		/* if the lock is currently set, we have to wait for
		 * the state to change
		 */
		if (lock->is_set) {
			if (!waitflag)
				return 0;
			DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT);
		}

		/* enter a critical section and try to get the semaphore.  If
		 * it is still locked, we will try again.
		 */
		if (DosEnterCritSec())
			return 0;

		if (!lock->is_set) {
			lock->is_set = 1;
			DosResetEventSem(lock->changed, &count);
			done = 1;
		}

		DosExitCritSec();
	}
#endif

	return 1;
}

void
PyThread_release_lock(PyThread_type_lock aLock)
{
#if !defined(PYCC_GCC)
	type_os2_lock lock = (type_os2_lock)aLock;
#endif

	dprintf(("%ld: PyThread_release_lock(%p) called\n",
		 PyThread_get_thread_ident(),
		 aLock));

#if defined(PYCC_GCC)
	if (aLock)
		_fmutex_release((_fmutex *)aLock);
#else
	if (!lock->is_set) {
		dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
			 PyThread_get_thread_ident(),
			 aLock,
			 GetLastError()));
		return;
	}

	if (DosEnterCritSec()) {
		dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
			 PyThread_get_thread_ident(),
			 aLock,
			 GetLastError()));
		return;
	}

	lock->is_set = 0;
	DosPostEventSem(lock->changed);

	DosExitCritSec();
#endif
}

/* minimum/maximum thread stack sizes supported */
#define THREAD_MIN_STACKSIZE	0x8000		/* 32kB */
#define THREAD_MAX_STACKSIZE	0x2000000	/* 32MB */

/* set the thread stack size.
 * Return 0 if size is valid, -1 otherwise.
 */
static int
_pythread_os2_set_stacksize(size_t size)
{
	/* set to default */
	if (size == 0) {
		_pythread_stacksize = 0;
		return 0;
	}

	/* valid range? */
	if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
		_pythread_stacksize = size;
		return 0;
	}

	return -1;
}

#define THREAD_SET_STACKSIZE(x)	_pythread_os2_set_stacksize(x)