11 #define HDR_SIZE 2680 /* sizeof(ushdr_t) */
12 #define MAXPROC 100 /* max # of threads that can be started */
14 static usptr_t
*shared_arena
;
15 static ulock_t count_lock
; /* protection for some variables */
16 static ulock_t wait_lock
; /* lock used to wait for other threads */
17 static int waiting_for_threads
; /* protected by count_lock */
18 static int nthreads
; /* protected by count_lock */
19 static int exit_status
;
21 static int do_exit
; /* indicates that the program is to exit */
23 static int exiting
; /* we're already exiting (for maybe_exit) */
24 static pid_t my_pid
; /* PID of main thread */
25 static struct pidlist
{
28 } pidlist
[MAXPROC
]; /* PIDs of other threads; protected by count_lock */
29 static int maxpidindex
; /* # of PIDs in pidlist */
33 * This routine is called as a signal handler when another thread
34 * exits. When that happens, we must see whether we have to exit as
35 * well (because of an PyThread_exit_prog()) or whether we should continue on.
37 static void exit_sig(void)
39 d2printf(("exit_sig called\n"));
40 if (exiting
&& getpid() == my_pid
) {
41 d2printf(("already exiting\n"));
45 d2printf(("exiting in exit_sig\n"));
47 if ((thread_debug
& 8) == 0)
48 thread_debug
&= ~1; /* don't produce debug messages */
50 PyThread_exit_thread();
55 * This routine is called when a process calls exit(). If that wasn't
56 * done from the library, we do as if an PyThread_exit_prog() was intended.
58 static void maybe_exit(void)
60 dprintf(("maybe_exit called\n"));
62 dprintf(("already exiting\n"));
65 PyThread_exit_prog(0);
67 #endif /* NO_EXIT_PROG */
72 static void PyThread__init_thread(void)
76 #endif /* NO_EXIT_PROG */
83 if ((size
= usconfig(CONF_INITSIZE
, 64*1024)) < 0)
84 perror("usconfig - CONF_INITSIZE (check)");
85 if (usconfig(CONF_INITSIZE
, size
) < 0)
86 perror("usconfig - CONF_INITSIZE (reset)");
87 addr
= (long) dl_getrange(size
+ HDR_SIZE
);
88 dprintf(("trying to use addr %p-%p for shared arena\n", addr
, addr
+size
));
90 if ((addr
= usconfig(CONF_ATTACHADDR
, addr
)) < 0 && errno
!= 0)
91 perror("usconfig - CONF_ATTACHADDR (set)");
93 if (usconfig(CONF_INITUSERS
, 16) < 0)
94 perror("usconfig - CONF_INITUSERS");
95 my_pid
= getpid(); /* so that we know which is the main thread */
98 s
.sa_handler
= exit_sig
;
99 sigemptyset(&s
.sa_mask
);
100 /*sigaddset(&s.sa_mask, SIGUSR1);*/
102 sigaction(SIGUSR1
, &s
, 0);
103 if (prctl(PR_SETEXITSIG
, SIGUSR1
) < 0)
104 perror("prctl - PR_SETEXITSIG");
105 #endif /* NO_EXIT_PROG */
106 if (usconfig(CONF_ARENATYPE
, US_SHAREDONLY
) < 0)
107 perror("usconfig - CONF_ARENATYPE");
108 usconfig(CONF_LOCKTYPE
, US_DEBUG
); /* XXX */
110 if (thread_debug
& 4)
111 usconfig(CONF_LOCKTYPE
, US_DEBUGPLUS
);
112 else if (thread_debug
& 2)
113 usconfig(CONF_LOCKTYPE
, US_DEBUG
);
114 #endif /* Py_DEBUG */
115 if ((shared_arena
= usinit(tmpnam(0))) == 0)
118 if (usconfig(CONF_ATTACHADDR
, addr
) < 0) /* reset address */
119 perror("usconfig - CONF_ATTACHADDR (reset)");
121 if ((count_lock
= usnewlock(shared_arena
)) == NULL
)
122 perror("usnewlock (count_lock)");
123 (void) usinitlock(count_lock
);
124 if ((wait_lock
= usnewlock(shared_arena
)) == NULL
)
125 perror("usnewlock (wait_lock)");
126 dprintf(("arena start: %p, arena size: %ld\n", shared_arena
, (long) usconfig(CONF_GETSIZE
, shared_arena
)));
133 static void clean_threads(void)
138 /* clean up any exited threads */
141 while (i
< maxpidindex
) {
142 if (pidlist
[i
].parent
== mypid
&& (pid
= pidlist
[i
].child
) > 0) {
143 pid
= waitpid(pid
, 0, WNOHANG
);
145 /* a thread has exited */
146 pidlist
[i
] = pidlist
[--maxpidindex
];
147 /* remove references to children of dead proc */
148 for (j
= 0; j
< maxpidindex
; j
++)
149 if (pidlist
[j
].parent
== pid
)
150 pidlist
[j
].child
= -1;
151 continue; /* don't increment i */
156 /* clean up the list */
158 while (i
< maxpidindex
) {
159 if (pidlist
[i
].child
== -1) {
160 pidlist
[i
] = pidlist
[--maxpidindex
];
161 continue; /* don't increment i */
167 long PyThread_start_new_thread(void (*func
)(void *), void *arg
)
171 static int local_initialized
= 0;
173 int success
= 0; /* init not needed when SOLARIS_THREADS and */
174 /* C_THREADS implemented properly */
176 dprintf(("PyThread_start_new_thread called\n"));
178 PyThread_init_thread();
179 switch (ussetlock(count_lock
)) {
181 case -1: perror("ussetlock (count_lock)");
183 if (maxpidindex
>= MAXPROC
)
187 if (!local_initialized
) {
188 if ((size
= usconfig(CONF_INITSIZE
, 64*1024)) < 0)
189 perror("usconfig - CONF_INITSIZE (check)");
190 if (usconfig(CONF_INITSIZE
, size
) < 0)
191 perror("usconfig - CONF_INITSIZE (reset)");
192 addr
= (long) dl_getrange(size
+ HDR_SIZE
);
193 dprintf(("trying to use addr %p-%p for sproc\n",
196 if ((addr
= usconfig(CONF_ATTACHADDR
, addr
)) < 0 &&
198 perror("usconfig - CONF_ATTACHADDR (set)");
202 if ((success
= sproc(func
, PR_SALL
, arg
)) < 0)
205 if (!local_initialized
) {
206 if (usconfig(CONF_ATTACHADDR
, addr
) < 0)
208 perror("usconfig - CONF_ATTACHADDR (reset)");
209 local_initialized
= 1;
214 pidlist
[maxpidindex
].parent
= getpid();
215 pidlist
[maxpidindex
++].child
= success
;
216 dprintf(("pidlist[%d] = %d\n",
217 maxpidindex
-1, success
));
220 if (usunsetlock(count_lock
) < 0)
221 perror("usunsetlock (count_lock)");
225 long PyThread_get_thread_ident(void)
230 static void do_PyThread_exit_thread(int no_cleanup
)
232 dprintf(("PyThread_exit_thread called\n"));
238 if (ussetlock(count_lock
) < 0)
239 perror("ussetlock (count_lock)");
241 if (getpid() == my_pid
) {
242 /* main thread; wait for other threads to exit */
248 /* notify other threads */
251 dprintf(("kill other threads\n"));
252 for (i
= 0; i
< maxpidindex
; i
++)
253 if (pidlist
[i
].child
> 0)
254 (void) kill(pidlist
[i
].child
,
259 #endif /* NO_EXIT_PROG */
260 waiting_for_threads
= 1;
261 if (ussetlock(wait_lock
) < 0)
262 perror("ussetlock (wait_lock)");
265 dprintf(("really exit (%d)\n", exit_status
));
271 if (usunsetlock(count_lock
) < 0)
272 perror("usunsetlock (count_lock)");
273 dprintf(("waiting for other threads (%d)\n", nthreads
));
274 if (ussetlock(wait_lock
) < 0)
275 perror("ussetlock (wait_lock)");
276 if (ussetlock(count_lock
) < 0)
277 perror("ussetlock (count_lock)");
280 /* not the main thread */
281 if (waiting_for_threads
) {
282 dprintf(("main thread is waiting\n"));
283 if (usunsetlock(wait_lock
) < 0)
284 perror("usunsetlock (wait_lock)");
288 (void) kill(my_pid
, SIGUSR1
);
289 #endif /* NO_EXIT_PROG */
290 if (usunsetlock(count_lock
) < 0)
291 perror("usunsetlock (count_lock)");
295 void PyThread_exit_thread(void)
297 do_PyThread_exit_thread(0);
300 void PyThread__exit_thread(void)
302 do_PyThread_exit_thread(1);
306 static void do_PyThread_exit_prog(int status
, int no_cleanup
)
308 dprintf(("PyThread_exit_prog(%d) called\n", status
));
315 exit_status
= status
;
316 do_PyThread_exit_thread(no_cleanup
);
319 void PyThread_exit_prog(int status
)
321 do_PyThread_exit_prog(status
, 0);
324 void PyThread__exit_prog(int status
)
326 do_PyThread_exit_prog(status
, 1);
328 #endif /* NO_EXIT_PROG */
333 PyThread_type_lock
PyThread_allocate_lock(void)
337 dprintf(("PyThread_allocate_lock called\n"));
339 PyThread_init_thread();
341 if ((lock
= usnewlock(shared_arena
)) == NULL
)
343 (void) usinitlock(lock
);
344 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
345 return (PyThread_type_lock
) lock
;
348 void PyThread_free_lock(PyThread_type_lock lock
)
350 dprintf(("PyThread_free_lock(%p) called\n", lock
));
351 usfreelock((ulock_t
) lock
, shared_arena
);
354 int PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
358 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
359 errno
= 0; /* clear it just in case */
361 success
= ussetlock((ulock_t
) lock
);
363 success
= uscsetlock((ulock_t
) lock
, 1); /* Try it once */
365 perror(waitflag
? "ussetlock" : "uscsetlock");
366 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
370 void PyThread_release_lock(PyThread_type_lock lock
)
372 dprintf(("PyThread_release_lock(%p) called\n", lock
));
373 if (usunsetlock((ulock_t
) lock
) < 0)
374 perror("usunsetlock");