Issue #7701: Fix crash in binascii.b2a_uu() in debug mode when given a
[python.git] / Python / thread_cthread.h
blobca776c63638b0d7105390e1b93c609728ede1ed3
2 #ifdef MACH_C_THREADS
3 #include <mach/cthreads.h>
4 #endif
6 #ifdef HURD_C_THREADS
7 #include <cthreads.h>
8 #endif
11 * Initialization.
13 static void
14 PyThread__init_thread(void)
16 #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 */
23 #endif
27 * Thread support.
29 long
30 PyThread_start_new_thread(void (*func)(void *), void *arg)
32 int success = 0; /* init not needed when SOLARIS_THREADS and */
33 /* C_THREADS implemented properly */
35 dprintf(("PyThread_start_new_thread called\n"));
36 if (!initialized)
37 PyThread_init_thread();
38 /* looks like solaris detaches the thread to never rejoin
39 * so well do it here
41 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
42 return success < 0 ? -1 : 0;
45 long
46 PyThread_get_thread_ident(void)
48 if (!initialized)
49 PyThread_init_thread();
50 return (long) cthread_self();
53 static void
54 do_PyThread_exit_thread(int no_cleanup)
56 dprintf(("PyThread_exit_thread called\n"));
57 if (!initialized)
58 if (no_cleanup)
59 _exit(0);
60 else
61 exit(0);
62 cthread_exit(0);
65 void
66 PyThread_exit_thread(void)
68 do_PyThread_exit_thread(0);
71 void
72 PyThread__exit_thread(void)
74 do_PyThread_exit_thread(1);
77 #ifndef NO_EXIT_PROG
78 static
79 void do_PyThread_exit_prog(int status, int no_cleanup)
81 dprintf(("PyThread_exit_prog(%d) called\n", status));
82 if (!initialized)
83 if (no_cleanup)
84 _exit(status);
85 else
86 exit(status);
87 if (no_cleanup)
88 _exit(status);
89 else
90 exit(status);
93 void
94 PyThread_exit_prog(int status)
96 do_PyThread_exit_prog(status, 0);
99 void
100 PyThread__exit_prog(int status)
102 do_PyThread_exit_prog(status, 1);
104 #endif /* NO_EXIT_PROG */
107 * Lock support.
109 PyThread_type_lock
110 PyThread_allocate_lock(void)
112 mutex_t lock;
114 dprintf(("PyThread_allocate_lock called\n"));
115 if (!initialized)
116 PyThread_init_thread();
118 lock = mutex_alloc();
119 if (mutex_init(lock)) {
120 perror("mutex_init");
121 free((void *) lock);
122 lock = 0;
124 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
125 return (PyThread_type_lock) lock;
128 void
129 PyThread_free_lock(PyThread_type_lock lock)
131 dprintf(("PyThread_free_lock(%p) called\n", lock));
132 mutex_free(lock);
136 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
138 int success = FALSE;
140 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
141 if (waitflag) { /* blocking */
142 mutex_lock((mutex_t)lock);
143 success = TRUE;
144 } else { /* non blocking */
145 success = mutex_try_lock((mutex_t)lock);
147 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
148 return success;
151 void
152 PyThread_release_lock(PyThread_type_lock lock)
154 dprintf(("PyThread_release_lock(%p) called\n", lock));
155 mutex_unlock((mutex_t )lock);