blob: ca776c63638b0d7105390e1b93c609728ede1ed3 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001
Martin v. Löwisa6e97582002-01-01 18:41:33 +00002#ifdef MACH_C_THREADS
Guido van Rossum3ecebf11996-07-30 16:48:31 +00003#include <mach/cthreads.h>
Martin v. Löwisa6e97582002-01-01 18:41:33 +00004#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00005
Martin v. Löwisa6e97582002-01-01 18:41:33 +00006#ifdef HURD_C_THREADS
7#include <cthreads.h>
8#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00009
10/*
11 * Initialization.
12 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000013static void
14PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000015{
Martin v. Löwisa6e97582002-01-01 18:41:33 +000016#ifndef HURD_C_THREADS
17 /* Roland McGrath said this should not be used since this is
18 done while linking to threads */
19 cthread_init();
20#else
21/* do nothing */
22 ;
23#endif
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000024}
25
26/*
27 * Thread support.
28 */
Guido van Rossum3c288632001-10-16 21:13:49 +000029long
Guido van Rossum128bcf42000-11-13 19:45:45 +000030PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000031{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000032 int success = 0; /* init not needed when SOLARIS_THREADS and */
33 /* C_THREADS implemented properly */
34
Guido van Rossum65d5b571998-12-21 19:32:43 +000035 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000036 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000037 PyThread_init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000038 /* looks like solaris detaches the thread to never rejoin
39 * so well do it here
40 */
41 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
Guido van Rossum3c288632001-10-16 21:13:49 +000042 return success < 0 ? -1 : 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000043}
44
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000045long
46PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +000047{
48 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000049 PyThread_init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000050 return (long) cthread_self();
Guido van Rossume944da81994-05-23 12:43:41 +000051}
52
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053static void
54do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000055{
Guido van Rossum65d5b571998-12-21 19:32:43 +000056 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000057 if (!initialized)
58 if (no_cleanup)
59 _exit(0);
60 else
61 exit(0);
62 cthread_exit(0);
63}
64
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000065void
66PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000067{
Guido van Rossum65d5b571998-12-21 19:32:43 +000068 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069}
70
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071void
72PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000073{
Guido van Rossum65d5b571998-12-21 19:32:43 +000074 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000075}
76
77#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000078static
79void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000080{
Guido van Rossum65d5b571998-12-21 19:32:43 +000081 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000082 if (!initialized)
83 if (no_cleanup)
84 _exit(status);
85 else
86 exit(status);
Guido van Rossum3ecebf11996-07-30 16:48:31 +000087 if (no_cleanup)
88 _exit(status);
89 else
90 exit(status);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000091}
92
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093void
94PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000095{
Guido van Rossum65d5b571998-12-21 19:32:43 +000096 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000097}
98
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099void
100PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000101{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000102 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000103}
104#endif /* NO_EXIT_PROG */
105
106/*
107 * Lock support.
108 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109PyThread_type_lock
110PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000112 mutex_t lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113
Guido van Rossum65d5b571998-12-21 19:32:43 +0000114 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000115 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000116 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000117
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000118 lock = mutex_alloc();
119 if (mutex_init(lock)) {
120 perror("mutex_init");
121 free((void *) lock);
122 lock = 0;
123 }
Fred Drakea44d3532000-06-30 15:01:00 +0000124 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000125 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126}
127
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128void
129PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000130{
Fred Drakea44d3532000-06-30 15:01:00 +0000131 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000132 mutex_free(lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133}
134
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135int
136PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000137{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000138 int success = FALSE;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000139
Fred Drakea44d3532000-06-30 15:01:00 +0000140 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000141 if (waitflag) { /* blocking */
Martin v. Löwisa6e97582002-01-01 18:41:33 +0000142 mutex_lock((mutex_t)lock);
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000143 success = TRUE;
144 } else { /* non blocking */
Martin v. Löwisa6e97582002-01-01 18:41:33 +0000145 success = mutex_try_lock((mutex_t)lock);
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000146 }
Fred Drakea44d3532000-06-30 15:01:00 +0000147 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000148 return success;
149}
150
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151void
152PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000153{
Fred Drakea44d3532000-06-30 15:01:00 +0000154 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000155 mutex_unlock((mutex_t )lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000156}