thread_cthread.h 2.72 KB
Newer Older
1

2
#ifdef MACH_C_THREADS
3
#include <mach/cthreads.h>
4
#endif
5

6 7 8
#ifdef HURD_C_THREADS
#include <cthreads.h>
#endif
9 10 11 12

/*
 * Initialization.
 */
13 14
static void
PyThread__init_thread(void)
15
{
16 17 18 19 20 21 22 23
#ifndef HURD_C_THREADS
	/* Roland McGrath said this should not be used since this is
	done while linking to threads */
	cthread_init(); 
#else
/* do nothing */
	;
#endif
24 25 26 27 28
}

/*
 * Thread support.
 */
29
long
30
PyThread_start_new_thread(void (*func)(void *), void *arg)
31 32 33 34
{
	int success = 0;	/* init not needed when SOLARIS_THREADS and */
				/* C_THREADS implemented properly */

35
	dprintf(("PyThread_start_new_thread called\n"));
36
	if (!initialized)
37
		PyThread_init_thread();
38 39 40 41
	/* looks like solaris detaches the thread to never rejoin
	 * so well do it here
	 */
	cthread_detach(cthread_fork((cthread_fn_t) func, arg));
42
	return success < 0 ? -1 : 0;
43 44
}

45 46
long
PyThread_get_thread_ident(void)
47 48
{
	if (!initialized)
49
		PyThread_init_thread();
50
	return (long) cthread_self();
51 52
}

53 54
static void
do_PyThread_exit_thread(int no_cleanup)
55
{
56
	dprintf(("PyThread_exit_thread called\n"));
57 58 59 60 61 62 63 64
	if (!initialized)
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	cthread_exit(0);
}

65 66
void
PyThread_exit_thread(void)
67
{
68
	do_PyThread_exit_thread(0);
69 70
}

71 72
void
PyThread__exit_thread(void)
73
{
74
	do_PyThread_exit_thread(1);
75 76 77
}

#ifndef NO_EXIT_PROG
78 79
static
void do_PyThread_exit_prog(int status, int no_cleanup)
80
{
81
	dprintf(("PyThread_exit_prog(%d) called\n", status));
82 83 84 85 86
	if (!initialized)
		if (no_cleanup)
			_exit(status);
		else
			exit(status);
87 88 89 90
	if (no_cleanup)
		_exit(status);
	else
		exit(status);
91 92
}

93 94
void
PyThread_exit_prog(int status)
95
{
96
	do_PyThread_exit_prog(status, 0);
97 98
}

99 100
void
PyThread__exit_prog(int status)
101
{
102
	do_PyThread_exit_prog(status, 1);
103 104 105 106 107 108
}
#endif /* NO_EXIT_PROG */

/*
 * Lock support.
 */
109 110
PyThread_type_lock
PyThread_allocate_lock(void)
111
{
112
	mutex_t lock;
113

114
	dprintf(("PyThread_allocate_lock called\n"));
115
	if (!initialized)
116
		PyThread_init_thread();
117

118 119 120 121 122 123
	lock = mutex_alloc();
	if (mutex_init(lock)) {
		perror("mutex_init");
		free((void *) lock);
		lock = 0;
	}
124
	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
125
	return (PyThread_type_lock) lock;
126 127
}

128 129
void
PyThread_free_lock(PyThread_type_lock lock)
130
{
131
	dprintf(("PyThread_free_lock(%p) called\n", lock));
132
	mutex_free(lock);
133 134
}

135 136
int
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
137
{
138
	int success = FALSE;
139

140
	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
141
	if (waitflag) { 	/* blocking */
142
		mutex_lock((mutex_t)lock);
143 144
		success = TRUE;
	} else {		/* non blocking */
145
		success = mutex_try_lock((mutex_t)lock);
146
	}
147
	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
148 149 150
	return success;
}

151 152
void
PyThread_release_lock(PyThread_type_lock lock)
153
{
154
	dprintf(("PyThread_release_lock(%p) called\n", lock));
155
	mutex_unlock((mutex_t )lock);
156
}