1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
13 #include <lwp/stackdep.h>
15 #define STACKSIZE 1000 /* stacksize for a thread */
16 #define NSTACKS 2 /* # stacks to be put in cache initially */
28 static void PyThread__init_thread(void)
30 lwp_setstkcache(STACKSIZE
, NSTACKS
);
38 int PyThread_start_new_thread(void (*func
)(void *), void *arg
)
42 dprintf(("PyThread_start_new_thread called\n"));
44 PyThread_init_thread();
45 success
= lwp_create(&tid
, func
, MINPRIO
, 0, lwp_newstk(), 1, arg
);
46 return success
< 0 ? 0 : 1;
49 long PyThread_get_thread_ident(void)
53 PyThread_init_thread();
54 if (lwp_self(&tid
) < 0)
59 static void do_PyThread_exit_thread(int no_cleanup
)
61 dprintf(("PyThread_exit_thread called\n"));
70 void PyThread_exit_thread(void)
72 do_PyThread_exit_thread(0);
75 void PyThread__exit_thread(void)
77 do_PyThread_exit_thread(1);
81 static void do_PyThread_exit_prog(int status
, int no_cleanup
)
83 dprintf(("PyThread_exit_prog(%d) called\n", status
));
92 void PyThread_exit_prog(int status
)
94 do_PyThread_exit_prog(status
, 0);
97 void PyThread__exit_prog(int status
)
99 do_PyThread_exit_prog(status
, 1);
101 #endif /* NO_EXIT_PROG */
106 PyThread_type_lock
PyThread_allocate_lock(void)
109 extern char *malloc(size_t);
111 dprintf(("PyThread_allocate_lock called\n"));
113 PyThread_init_thread();
115 lock
= (struct lock
*) malloc(sizeof(struct lock
));
116 lock
->lock_locked
= 0;
117 (void) mon_create(&lock
->lock_monitor
);
118 (void) cv_create(&lock
->lock_condvar
, lock
->lock_monitor
);
119 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
120 return (PyThread_type_lock
) lock
;
123 void PyThread_free_lock(PyThread_type_lock lock
)
125 dprintf(("PyThread_free_lock(%p) called\n", lock
));
126 mon_destroy(((struct lock
*) lock
)->lock_monitor
);
130 int PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
134 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
137 (void) mon_enter(((struct lock
*) lock
)->lock_monitor
);
139 while (((struct lock
*) lock
)->lock_locked
)
140 cv_wait(((struct lock
*) lock
)->lock_condvar
);
141 if (!((struct lock
*) lock
)->lock_locked
) {
143 ((struct lock
*) lock
)->lock_locked
= 1;
145 cv_broadcast(((struct lock
*) lock
)->lock_condvar
);
146 mon_exit(((struct lock
*) lock
)->lock_monitor
);
147 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
151 void PyThread_release_lock(PyThread_type_lock lock
)
153 dprintf(("PyThread_release_lock(%p) called\n", lock
));
154 (void) mon_enter(((struct lock
*) lock
)->lock_monitor
);
155 ((struct lock
*) lock
)->lock_locked
= 0;
156 cv_broadcast(((struct lock
*) lock
)->lock_condvar
);
157 mon_exit(((struct lock
*) lock
)->lock_monitor
);
163 PyThread_type_sema
PyThread_allocate_sema(int value
)
165 PyThread_type_sema sema
= 0;
166 dprintf(("PyThread_allocate_sema called\n"));
168 PyThread_init_thread();
170 dprintf(("PyThread_allocate_sema() -> %p\n", sema
));
171 return (PyThread_type_sema
) sema
;
174 void PyThread_free_sema(PyThread_type_sema sema
)
176 dprintf(("PyThread_free_sema(%p) called\n", sema
));
179 int PyThread_down_sema(PyThread_type_sema sema
, int waitflag
)
181 dprintf(("PyThread_down_sema(%p, %d) called\n", sema
, waitflag
));
182 dprintf(("PyThread_down_sema(%p) return\n", sema
));
186 void PyThread_up_sema(PyThread_type_sema sema
)
188 dprintf(("PyThread_up_sema(%p)\n", sema
));