1 /* Threading for AtheOS.
2 Based on thread_beos.h. */
4 #include <atheos/threads.h>
5 #include <atheos/semaphore.h>
6 #include <atheos/atomic.h>
10 /* Missing decl from threads.h */
11 extern int exit_thread(int);
14 /* Undefine FASTLOCK to play with simple semaphores. */
20 /* Use an atomic counter and a semaphore for maximum speed. */
21 typedef struct fastmutex {
27 static int fastmutex_create(const char *name, fastmutex_t * mutex);
28 static int fastmutex_destroy(fastmutex_t * mutex);
29 static int fastmutex_lock(fastmutex_t * mutex);
30 static int fastmutex_timedlock(fastmutex_t * mutex, bigtime_t timeout);
31 static int fastmutex_unlock(fastmutex_t * mutex);
34 static int fastmutex_create(const char *name, fastmutex_t * mutex)
37 mutex->sem = create_semaphore(name, 0, 0);
38 return (mutex->sem < 0) ? -1 : 0;
42 static int fastmutex_destroy(fastmutex_t * mutex)
44 if (fastmutex_timedlock(mutex, 0) == 0 || errno == EWOULDBLOCK) {
45 return delete_semaphore(mutex->sem);
51 static int fastmutex_lock(fastmutex_t * mutex)
53 atomic_t prev = atomic_add(&mutex->count, 1);
55 return lock_semaphore(mutex->sem);
60 static int fastmutex_timedlock(fastmutex_t * mutex, bigtime_t timeout)
62 atomic_t prev = atomic_add(&mutex->count, 1);
64 return lock_semaphore_x(mutex->sem, 1, 0, timeout);
69 static int fastmutex_unlock(fastmutex_t * mutex)
71 atomic_t prev = atomic_add(&mutex->count, -1);
73 return unlock_semaphore(mutex->sem);
85 static void PyThread__init_thread(void)
97 static atomic_t thread_count = 0;
99 long PyThread_start_new_thread(void (*func) (void *), void *arg)
101 status_t success = -1;
103 char name[OS_NAME_LENGTH];
104 atomic_t this_thread;
106 dprintf(("PyThread_start_new_thread called\n"));
108 this_thread = atomic_add(&thread_count, 1);
109 PyOS_snprintf(name, sizeof(name), "python thread (%d)", this_thread);
111 tid = spawn_thread(name, func, NORMAL_PRIORITY, 0, arg);
113 dprintf(("PyThread_start_new_thread spawn_thread failed: %s\n", strerror(errno)));
115 success = resume_thread(tid);
117 dprintf(("PyThread_start_new_thread resume_thread failed: %s\n", strerror(errno)));
121 return (success < 0 ? -1 : tid);
125 long PyThread_get_thread_ident(void)
127 return get_thread_id(NULL);
131 void PyThread_exit_thread(void)
133 dprintf(("PyThread_exit_thread called\n"));
135 /* Thread-safe way to read a variable without a mutex: */
136 if (atomic_add(&thread_count, 0) == 0) {
137 /* No threads around, so exit main(). */
151 static atomic_t lock_count = 0;
153 PyThread_type_lock PyThread_allocate_lock(void)
160 char name[OS_NAME_LENGTH];
163 dprintf(("PyThread_allocate_lock called\n"));
166 lock = (fastmutex_t *) malloc(sizeof(fastmutex_t));
168 dprintf(("PyThread_allocate_lock failed: out of memory\n"));
169 return (PyThread_type_lock) NULL;
172 this_lock = atomic_add(&lock_count, 1);
173 PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
176 if (fastmutex_create(name, lock) < 0) {
177 dprintf(("PyThread_allocate_lock failed: %s\n",
182 dprintf(("PyThread_allocate_lock()-> %p\n", lock));
183 return (PyThread_type_lock) lock;
185 sema = create_semaphore(name, 1, 0);
187 dprintf(("PyThread_allocate_lock failed: %s\n",
191 dprintf(("PyThread_allocate_lock()-> %p\n", sema));
192 return (PyThread_type_lock) sema;
197 void PyThread_free_lock(PyThread_type_lock lock)
199 dprintf(("PyThread_free_lock(%p) called\n", lock));
202 if (fastmutex_destroy((fastmutex_t *) lock) < 0) {
203 dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
208 if (delete_semaphore((sem_id) lock) < 0) {
209 dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
216 int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
220 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock,
225 retval = fastmutex_lock((fastmutex_t *) lock);
227 retval = fastmutex_timedlock((fastmutex_t *) lock, 0);
230 retval = lock_semaphore((sem_id) lock);
232 retval = lock_semaphore_x((sem_id) lock, 1, 0, 0);
235 dprintf(("PyThread_acquire_lock(%p, %d) failed: %s\n",
236 lock, waitflag, strerror(errno)));
238 dprintf(("PyThread_acquire_lock(%p, %d)-> %d\n", lock, waitflag,
240 return retval < 0 ? 0 : 1;
244 void PyThread_release_lock(PyThread_type_lock lock)
246 dprintf(("PyThread_release_lock(%p) called\n", lock));
249 if (fastmutex_unlock((fastmutex_t *) lock) < 0) {
250 dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
254 if (unlock_semaphore((sem_id) lock) < 0) {
255 dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,