blob: e93d65aa30dc680a5c45e6c457394adf0ecc61e8 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001
2#include <stdlib.h>
3#include <lwp/lwp.h>
4#include <lwp/stackdep.h>
5
6#define STACKSIZE 1000 /* stacksize for a thread */
Thomas Wouters7e474022000-07-16 12:04:32 +00007#define NSTACKS 2 /* # stacks to be put in cache initially */
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00008
9struct lock {
10 int lock_locked;
11 cv_t lock_condvar;
12 mon_t lock_monitor;
13};
14
15
16/*
17 * Initialization.
18 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000019static void PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000020{
21 lwp_setstkcache(STACKSIZE, NSTACKS);
22}
23
24/*
25 * Thread support.
26 */
27
28
Guido van Rossum3c288632001-10-16 21:13:49 +000029long PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000030{
31 thread_t tid;
Guido van Rossume944da81994-05-23 12:43:41 +000032 int success;
Guido van Rossum65d5b571998-12-21 19:32:43 +000033 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000034 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000035 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000036 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
Guido van Rossum3c288632001-10-16 21:13:49 +000037 return success < 0 ? -1 : 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000038}
39
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040long PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +000041{
42 thread_t tid;
43 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000044 PyThread_init_thread();
Guido van Rossume944da81994-05-23 12:43:41 +000045 if (lwp_self(&tid) < 0)
46 return -1;
47 return tid.thread_id;
48}
49
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050static void do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000051{
Guido van Rossum65d5b571998-12-21 19:32:43 +000052 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000053 if (!initialized)
54 if (no_cleanup)
55 _exit(0);
56 else
57 exit(0);
58 lwp_destroy(SELF);
59}
60
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061void PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000062{
Guido van Rossum65d5b571998-12-21 19:32:43 +000063 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000064}
65
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066void PyThread__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(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069}
70
71#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072static void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000073{
Guido van Rossum65d5b571998-12-21 19:32:43 +000074 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000075 if (!initialized)
76 if (no_cleanup)
77 _exit(status);
78 else
79 exit(status);
80 pod_exit(status);
81}
82
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000083void PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000084{
Guido van Rossum65d5b571998-12-21 19:32:43 +000085 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000086}
87
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000088void PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089{
Guido van Rossum65d5b571998-12-21 19:32:43 +000090 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000091}
92#endif /* NO_EXIT_PROG */
93
94/*
95 * Lock support.
96 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000097PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000098{
99 struct lock *lock;
Thomas Woutersb4bd21c2000-07-22 23:38:01 +0000100 extern char *malloc(size_t);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000101
Guido van Rossum65d5b571998-12-21 19:32:43 +0000102 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000103 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000104 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105
106 lock = (struct lock *) malloc(sizeof(struct lock));
107 lock->lock_locked = 0;
108 (void) mon_create(&lock->lock_monitor);
109 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
Fred Drakea44d3532000-06-30 15:01:00 +0000110 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000111 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000112}
113
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000114void PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000115{
Fred Drakea44d3532000-06-30 15:01:00 +0000116 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000117 mon_destroy(((struct lock *) lock)->lock_monitor);
118 free((char *) lock);
119}
120
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000122{
123 int success;
124
Fred Drakea44d3532000-06-30 15:01:00 +0000125 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126 success = 0;
127
128 (void) mon_enter(((struct lock *) lock)->lock_monitor);
129 if (waitflag)
130 while (((struct lock *) lock)->lock_locked)
131 cv_wait(((struct lock *) lock)->lock_condvar);
132 if (!((struct lock *) lock)->lock_locked) {
133 success = 1;
134 ((struct lock *) lock)->lock_locked = 1;
135 }
136 cv_broadcast(((struct lock *) lock)->lock_condvar);
137 mon_exit(((struct lock *) lock)->lock_monitor);
Fred Drakea44d3532000-06-30 15:01:00 +0000138 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000139 return success;
140}
141
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142void PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000143{
Fred Drakea44d3532000-06-30 15:01:00 +0000144 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000145 (void) mon_enter(((struct lock *) lock)->lock_monitor);
146 ((struct lock *) lock)->lock_locked = 0;
147 cv_broadcast(((struct lock *) lock)->lock_condvar);
148 mon_exit(((struct lock *) lock)->lock_monitor);
149}