thread_cthread.h revision fd71b9e9d496caa510dec56a9b69966558d6ba5d
1/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5                        All Rights Reserved
6
7Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
11
12See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14
15******************************************************************/
16
17#include <mach/cthreads.h>
18
19
20/*
21 * Initialization.
22 */
23static void PyThread__init_thread _P0()
24{
25	cthread_init();
26}
27
28/*
29 * Thread support.
30 */
31int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
32{
33	int success = 0;	/* init not needed when SOLARIS_THREADS and */
34				/* C_THREADS implemented properly */
35
36	dprintf(("PyThread_start_new_thread called\n"));
37	if (!initialized)
38		PyThread_init_thread();
39	/* looks like solaris detaches the thread to never rejoin
40	 * so well do it here
41	 */
42	cthread_detach(cthread_fork((cthread_fn_t) func, arg));
43	return success < 0 ? 0 : 1;
44}
45
46long PyThread_get_thread_ident _P0()
47{
48	if (!initialized)
49		PyThread_init_thread();
50	return (long) cthread_self();
51}
52
53static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
54{
55	dprintf(("PyThread_exit_thread called\n"));
56	if (!initialized)
57		if (no_cleanup)
58			_exit(0);
59		else
60			exit(0);
61	cthread_exit(0);
62}
63
64void PyThread_exit_thread _P0()
65{
66	do_PyThread_exit_thread(0);
67}
68
69void PyThread__exit_thread _P0()
70{
71	do_PyThread_exit_thread(1);
72}
73
74#ifndef NO_EXIT_PROG
75static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
76{
77	dprintf(("PyThread_exit_prog(%d) called\n", status));
78	if (!initialized)
79		if (no_cleanup)
80			_exit(status);
81		else
82			exit(status);
83	if (no_cleanup)
84		_exit(status);
85	else
86		exit(status);
87}
88
89void PyThread_exit_prog _P1(status, int status)
90{
91	do_PyThread_exit_prog(status, 0);
92}
93
94void PyThread__exit_prog _P1(status, int status)
95{
96	do_PyThread_exit_prog(status, 1);
97}
98#endif /* NO_EXIT_PROG */
99
100/*
101 * Lock support.
102 */
103PyThread_type_lock PyThread_allocate_lock _P0()
104{
105	mutex_t lock;
106
107	dprintf(("PyThread_allocate_lock called\n"));
108	if (!initialized)
109		PyThread_init_thread();
110
111	lock = mutex_alloc();
112	if (mutex_init(lock)) {
113		perror("mutex_init");
114		free((void *) lock);
115		lock = 0;
116	}
117	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
118	return (PyThread_type_lock) lock;
119}
120
121void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
122{
123	dprintf(("PyThread_free_lock(%p) called\n", lock));
124	mutex_free(lock);
125}
126
127int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
128{
129	int success = FALSE;
130
131	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
132	if (waitflag) { 	/* blocking */
133		mutex_lock(lock);
134		success = TRUE;
135	} else {		/* non blocking */
136		success = mutex_try_lock(lock);
137	}
138	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
139	return success;
140}
141
142void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
143{
144	dprintf(("PyThread_release_lock(%p) called\n", lock));
145	mutex_unlock((mutex_t )lock);
146}
147
148/*
149 * Semaphore support.
150 *
151 * This implementation is ripped directly from the pthreads implementation.
152 * Which is to say that it is 100% non-functional at this time.
153 *
154 * Assuming the page is still up, documentation can be found at:
155 *
156 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
157 *
158 * Looking at the man page, it seems that one could easily implement a
159 * semaphore using a condition.
160 *
161 */
162PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
163{
164	char *sema = 0;
165	dprintf(("PyThread_allocate_sema called\n"));
166	if (!initialized)
167		PyThread_init_thread();
168
169	dprintf(("PyThread_allocate_sema() -> %p\n", sema));
170	return (PyThread_type_sema) sema;
171}
172
173void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
174{
175	dprintf(("PyThread_free_sema(%p) called\n", sema));
176}
177
178int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
179{
180	dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
181	dprintf(("PyThread_down_sema(%p) return\n", sema));
182	return -1;
183}
184
185void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
186{
187	dprintf(("PyThread_up_sema(%p)\n", sema));
188}
189