thread_os2.h 5.57 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6 7 8 9
/* This code implemented by cvale@netcom.com */

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

#include "process.h"

10 11 12 13
#if defined(PYCC_GCC)
#include <sys/builtin.h>
#include <sys/fmutex.h>
#else
14
long PyThread_get_thread_ident(void);
15
#endif
Guido van Rossum's avatar
Guido van Rossum committed
16

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

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

Guido van Rossum's avatar
Guido van Rossum committed
24 25 26
/*
 * Initialization of the C package, should not be needed.
 */
27 28
static void
PyThread__init_thread(void)
Guido van Rossum's avatar
Guido van Rossum committed
29 30 31 32 33 34
{
}

/*
 * Thread support.
 */
35
long
36
PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum's avatar
Guido van Rossum committed
37
{
38
	int thread_id;
Guido van Rossum's avatar
Guido van Rossum committed
39

40
	thread_id = _beginthread(func,
41 42 43
				NULL,
				OS2_STACKSIZE(_pythread_stacksize),
				arg);
Guido van Rossum's avatar
Guido van Rossum committed
44

45
	if (thread_id == -1) {
46 47
		dprintf(("_beginthread failed. return %ld\n", errno));
	}
Guido van Rossum's avatar
Guido van Rossum committed
48

49
	return thread_id;
Guido van Rossum's avatar
Guido van Rossum committed
50 51
}

52 53
long
PyThread_get_thread_ident(void)
Guido van Rossum's avatar
Guido van Rossum committed
54
{
55
#if !defined(PYCC_GCC)
56 57
	PPIB pib;
	PTIB tib;
58
#endif
Guido van Rossum's avatar
Guido van Rossum committed
59

60 61 62
	if (!initialized)
		PyThread_init_thread();

63
#if defined(PYCC_GCC)
64
	return _gettid();
65
#else
66 67
	DosGetInfoBlocks(&tib, &pib);
	return tib->tib_ptib2->tib2_ultid;
68
#endif
Guido van Rossum's avatar
Guido van Rossum committed
69 70
}

71 72
static void
do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum's avatar
Guido van Rossum committed
73
{
74 75 76 77 78 79 80 81
	dprintf(("%ld: PyThread_exit_thread called\n",
		 PyThread_get_thread_ident()));
	if (!initialized)
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	_endthread();
Guido van Rossum's avatar
Guido van Rossum committed
82 83
}

84 85
void 
PyThread_exit_thread(void)
Guido van Rossum's avatar
Guido van Rossum committed
86
{
87
	do_PyThread_exit_thread(0);
Guido van Rossum's avatar
Guido van Rossum committed
88 89
}

90 91
void 
PyThread__exit_thread(void)
Guido van Rossum's avatar
Guido van Rossum committed
92
{
93
	do_PyThread_exit_thread(1);
Guido van Rossum's avatar
Guido van Rossum committed
94 95 96
}

#ifndef NO_EXIT_PROG
97 98
static void 
do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum's avatar
Guido van Rossum committed
99
{
100 101 102 103 104 105
	dprintf(("PyThread_exit_prog(%d) called\n", status));
	if (!initialized)
		if (no_cleanup)
			_exit(status);
		else
			exit(status);
Guido van Rossum's avatar
Guido van Rossum committed
106 107
}

108 109
void 
PyThread_exit_prog(int status)
Guido van Rossum's avatar
Guido van Rossum committed
110
{
111
	do_PyThread_exit_prog(status, 0);
Guido van Rossum's avatar
Guido van Rossum committed
112 113
}

114 115
void 
PyThread__exit_prog(int status)
Guido van Rossum's avatar
Guido van Rossum committed
116
{
117
	do_PyThread_exit_prog(status, 1);
Guido van Rossum's avatar
Guido van Rossum committed
118 119 120 121
}
#endif /* NO_EXIT_PROG */

/*
122 123
 * 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 
124
 * counterparts.
Guido van Rossum's avatar
Guido van Rossum committed
125
 */
126 127

typedef struct os2_lock_t {
128 129
	int is_set;
	HEV changed;
130 131
} *type_os2_lock;

132 133
PyThread_type_lock 
PyThread_allocate_lock(void)
Guido van Rossum's avatar
Guido van Rossum committed
134
{
135
#if defined(PYCC_GCC)
136 137 138 139 140 141 142 143 144 145 146
	_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;
147
#else
148 149
	APIRET rc;
	type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
Guido van Rossum's avatar
Guido van Rossum committed
150

151 152 153
	dprintf(("PyThread_allocate_lock called\n"));
	if (!initialized)
		PyThread_init_thread();
Guido van Rossum's avatar
Guido van Rossum committed
154

155
	lock->is_set = 0;
Guido van Rossum's avatar
Guido van Rossum committed
156

157
	DosCreateEventSem(NULL, &lock->changed, 0, 0);
Guido van Rossum's avatar
Guido van Rossum committed
158

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

163
	return (PyThread_type_lock)lock;
164
#endif
Guido van Rossum's avatar
Guido van Rossum committed
165 166
}

167 168
void 
PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossum's avatar
Guido van Rossum committed
169
{
170
#if !defined(PYCC_GCC)
171
	type_os2_lock lock = (type_os2_lock)aLock;
172 173
#endif

174 175
	dprintf(("%ld: PyThread_free_lock(%p) called\n",
		 PyThread_get_thread_ident(),aLock));
Guido van Rossum's avatar
Guido van Rossum committed
176

177
#if defined(PYCC_GCC)
178 179 180 181
	if (aLock) {
		_fmutex_close((_fmutex *)aLock);
		free((_fmutex *)aLock);
	}
182
#else
183 184
	DosCloseEventSem(lock->changed);
	free(aLock);
185
#endif
Guido van Rossum's avatar
Guido van Rossum committed
186 187 188 189 190
}

/*
 * Return 1 on success if the lock was acquired
 *
191
 * and 0 if the lock was not acquired.
Guido van Rossum's avatar
Guido van Rossum committed
192
 */
193 194
int 
PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossum's avatar
Guido van Rossum committed
195
{
196
#if !defined(PYCC_GCC)
197 198 199 200 201
	int   done = 0;
	ULONG count;
	PID   pid = 0;
	TID   tid = 0;
	type_os2_lock lock = (type_os2_lock)aLock;
202
#endif
Guido van Rossum's avatar
Guido van Rossum committed
203

204 205 206 207
	dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
		 PyThread_get_thread_ident(),
		 aLock,
		 waitflag));
Guido van Rossum's avatar
Guido van Rossum committed
208

209
#if defined(PYCC_GCC)
210 211 212 213
	/* always successful if the lock doesn't exist */
	if (aLock &&
	    _fmutex_request((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT))
		return 0;
214
#else
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	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();
	}
239
#endif
Guido van Rossum's avatar
Guido van Rossum committed
240

241
	return 1;
Guido van Rossum's avatar
Guido van Rossum committed
242 243
}

244 245
void
PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossum's avatar
Guido van Rossum committed
246
{
247
#if !defined(PYCC_GCC)
248
	type_os2_lock lock = (type_os2_lock)aLock;
249 250
#endif

251 252 253
	dprintf(("%ld: PyThread_release_lock(%p) called\n",
		 PyThread_get_thread_ident(),
		 aLock));
Guido van Rossum's avatar
Guido van Rossum committed
254

255
#if defined(PYCC_GCC)
256 257
	if (aLock)
		_fmutex_release((_fmutex *)aLock);
258
#else
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	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();
279
#endif
Guido van Rossum's avatar
Guido van Rossum committed
280
}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

/* 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)