thread_os2.h 4.96 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 18 19 20
#if !defined(THREAD_STACK_SIZE)
#define	THREAD_STACK_SIZE	0x10000
#endif

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

/*
 * Thread support.
 */
32
long
33
PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum's avatar
Guido van Rossum committed
34
{
35 36
	int aThread;
	int success = 0;
Guido van Rossum's avatar
Guido van Rossum committed
37

38
	aThread = _beginthread(func, NULL, THREAD_STACK_SIZE, arg);
Guido van Rossum's avatar
Guido van Rossum committed
39

40 41 42 43 44
	if (aThread == -1) {
		success = -1;
		fprintf(stderr, "aThread failed == %d", aThread);
		dprintf(("_beginthread failed. return %ld\n", errno));
	}
Guido van Rossum's avatar
Guido van Rossum committed
45

46
	return success;
Guido van Rossum's avatar
Guido van Rossum committed
47 48
}

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

57 58 59
	if (!initialized)
		PyThread_init_thread();

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

68 69
static void
do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum's avatar
Guido van Rossum committed
70
{
71 72 73 74 75 76 77 78
	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
79 80
}

81 82
void 
PyThread_exit_thread(void)
Guido van Rossum's avatar
Guido van Rossum committed
83
{
84
	do_PyThread_exit_thread(0);
Guido van Rossum's avatar
Guido van Rossum committed
85 86
}

87 88
void 
PyThread__exit_thread(void)
Guido van Rossum's avatar
Guido van Rossum committed
89
{
90
	do_PyThread_exit_thread(1);
Guido van Rossum's avatar
Guido van Rossum committed
91 92 93
}

#ifndef NO_EXIT_PROG
94 95
static void 
do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum's avatar
Guido van Rossum committed
96
{
97 98 99 100 101 102
	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
103 104
}

105 106
void 
PyThread_exit_prog(int status)
Guido van Rossum's avatar
Guido van Rossum committed
107
{
108
	do_PyThread_exit_prog(status, 0);
Guido van Rossum's avatar
Guido van Rossum committed
109 110
}

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

/*
119 120
 * 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 
121
 * counterparts.
Guido van Rossum's avatar
Guido van Rossum committed
122
 */
123 124

typedef struct os2_lock_t {
125 126
	int is_set;
	HEV changed;
127 128
} *type_os2_lock;

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

148 149 150
	dprintf(("PyThread_allocate_lock called\n"));
	if (!initialized)
		PyThread_init_thread();
Guido van Rossum's avatar
Guido van Rossum committed
151

152
	lock->is_set = 0;
Guido van Rossum's avatar
Guido van Rossum committed
153

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

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

160
	return (PyThread_type_lock)lock;
161
#endif
Guido van Rossum's avatar
Guido van Rossum committed
162 163
}

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

171 172
	dprintf(("%ld: PyThread_free_lock(%p) called\n",
		 PyThread_get_thread_ident(),aLock));
Guido van Rossum's avatar
Guido van Rossum committed
173

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

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

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

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

238
	return 1;
Guido van Rossum's avatar
Guido van Rossum committed
239 240
}

241
void PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossum's avatar
Guido van Rossum committed
242
{
243
#if !defined(PYCC_GCC)
244
	type_os2_lock lock = (type_os2_lock)aLock;
245 246
#endif

247 248 249
	dprintf(("%ld: PyThread_release_lock(%p) called\n",
		 PyThread_get_thread_ident(),
		 aLock));
Guido van Rossum's avatar
Guido van Rossum committed
250

251
#if defined(PYCC_GCC)
252 253
	if (aLock)
		_fmutex_release((_fmutex *)aLock);
254
#else
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	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();
275
#endif
Guido van Rossum's avatar
Guido van Rossum committed
276
}