blob: ba7b37ad7e04b90b647c8623ceaec54360cfbc4e [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
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006#define STACKSIZE 1000 /* stacksize for a thread */
7#define NSTACKS 2 /* # stacks to be put in cache initially */
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00008
9struct lock {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000010 int lock_locked;
11 cv_t lock_condvar;
12 mon_t lock_monitor;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000013};
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000021 lwp_setstkcache(STACKSIZE, NSTACKS);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000022}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000031 thread_t tid;
32 int success;
33 dprintf(("PyThread_start_new_thread called\n"));
34 if (!initialized)
35 PyThread_init_thread();
36 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
37 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000042 thread_t tid;
43 if (!initialized)
44 PyThread_init_thread();
45 if (lwp_self(&tid) < 0)
46 return -1;
47 return tid.thread_id;
Guido van Rossume944da81994-05-23 12:43:41 +000048}
49
Amaury Forgeot d'Arcadfc80b2010-02-23 23:19:39 +000050void PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000051{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000052 dprintf(("PyThread_exit_thread called\n"));
53 if (!initialized)
54 exit(0);
55 lwp_destroy(SELF);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000056}
57
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000058/*
59 * Lock support.
60 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000062{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000063 struct lock *lock;
64 extern char *malloc(size_t);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000065
Antoine Pitrouc83ea132010-05-09 14:46:46 +000066 dprintf(("PyThread_allocate_lock called\n"));
67 if (!initialized)
68 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 lock = (struct lock *) malloc(sizeof(struct lock));
71 lock->lock_locked = 0;
72 (void) mon_create(&lock->lock_monitor);
73 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
74 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
75 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000076}
77
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000078void PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000079{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000080 dprintf(("PyThread_free_lock(%p) called\n", lock));
81 mon_destroy(((struct lock *) lock)->lock_monitor);
82 free((char *) lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083}
84
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000086{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 int success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000088
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
90 success = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 (void) mon_enter(((struct lock *) lock)->lock_monitor);
93 if (waitflag)
94 while (((struct lock *) lock)->lock_locked)
95 cv_wait(((struct lock *) lock)->lock_condvar);
96 if (!((struct lock *) lock)->lock_locked) {
97 success = 1;
98 ((struct lock *) lock)->lock_locked = 1;
99 }
100 cv_broadcast(((struct lock *) lock)->lock_condvar);
101 mon_exit(((struct lock *) lock)->lock_monitor);
102 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
103 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000104}
105
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106void PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000107{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 dprintf(("PyThread_release_lock(%p) called\n", lock));
109 (void) mon_enter(((struct lock *) lock)->lock_monitor);
110 ((struct lock *) lock)->lock_locked = 0;
111 cv_broadcast(((struct lock *) lock)->lock_condvar);
112 mon_exit(((struct lock *) lock)->lock_monitor);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113}