2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
14 * Support code for Solaris threads. Provides functionality we wish Sun
15 * had provided. Relies on some information we probably shouldn't rely on.
17 /* Boehm, September 14, 1994 4:44 pm PDT */
19 # if defined(GC_SOLARIS_THREADS) || defined(GC_SOLARIS_PTHREADS)
21 # include "private/gc_priv.h"
22 # include "private/solaris_threads.h"
27 # include <sys/types.h>
28 # include <sys/mman.h>
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 # include <sys/stat.h>
32 # include <sys/syscall.h>
33 # include <sys/procfs.h>
36 # define _CLASSIC_XOPEN_TYPES
41 * This is the default size of the LWP arrays. If there are more LWPs
42 * than this when a stop-the-world GC happens, set_max_lwps will be
44 * This must be higher than the number of LWPs at startup time.
45 * The threads library creates a thread early on, so the min. is 3
47 # define DEFAULT_MAX_LWPS 4
54 cond_t GC_prom_join_cv
; /* Broadcast when any thread terminates */
55 cond_t GC_create_cv
; /* Signalled when a new undetached */
61 #endif /* MMAP_STACKS */
63 /* We use the allocation lock to protect thread-related data structures. */
65 /* We stop the world using /proc primitives. This makes some */
66 /* minimal assumptions about the threads implementation. */
67 /* We don't play by the rules, since the rules make this */
68 /* impossible (as of Solaris 2.3). Also note that as of */
69 /* Solaris 2.3 the various thread and lwp suspension */
70 /* primitives failed to stop threads by the time the request */
74 static sigset_t old_mask
;
76 /* Sleep for n milliseconds, n < 1000 */
77 void GC_msec_sleep(int n
)
82 ts
.tv_nsec
= 1000000*n
;
83 if (syscall(SYS_nanosleep
, &ts
, 0) < 0) {
84 ABORT("nanosleep failed");
87 /* Turn off preemption; gross but effective. */
88 /* Caller has allocation lock. */
89 /* Actually this is not needed under Solaris 2.3 and */
90 /* 2.4, but hopefully that'll change. */
95 (void)sigfillset(&set
);
96 sigdelset(&set
, SIGABRT
);
97 syscall(SYS_sigprocmask
, SIG_SETMASK
, &set
, &old_mask
);
102 syscall(SYS_sigprocmask
, SIG_SETMASK
, &old_mask
, NULL
);
105 int GC_main_proc_fd
= -1;
108 struct lwp_cache_entry
{
110 int lc_descr
; /* /proc file descriptor. */
111 } GC_lwp_cache_default
[DEFAULT_MAX_LWPS
];
113 static int max_lwps
= DEFAULT_MAX_LWPS
;
114 static struct lwp_cache_entry
*GC_lwp_cache
= GC_lwp_cache_default
;
116 static prgregset_t GC_lwp_registers_default
[DEFAULT_MAX_LWPS
];
117 static prgregset_t
*GC_lwp_registers
= GC_lwp_registers_default
;
119 /* Return a file descriptor for the /proc entry corresponding */
120 /* to the given lwp. The file descriptor may be stale if the */
121 /* lwp exited and a new one was forked. */
122 static int open_lwp(lwpid_t id
)
125 static int next_victim
= 0;
128 for (i
= 0; i
< max_lwps
; i
++) {
129 if (GC_lwp_cache
[i
].lc_id
== id
) return(GC_lwp_cache
[i
].lc_descr
);
131 result
= syscall(SYS_ioctl
, GC_main_proc_fd
, PIOCOPENLWP
, &id
);
133 * If PIOCOPENLWP fails, try closing fds in the cache until it succeeds.
135 if (result
< 0 && errno
== EMFILE
) {
136 for (i
= 0; i
< max_lwps
; i
++) {
137 if (GC_lwp_cache
[i
].lc_id
!= 0) {
138 (void)syscall(SYS_close
, GC_lwp_cache
[i
].lc_descr
);
139 result
= syscall(SYS_ioctl
, GC_main_proc_fd
, PIOCOPENLWP
, &id
);
140 if (result
>= 0 || (result
< 0 && errno
!= EMFILE
))
146 if (errno
== EMFILE
) {
147 ABORT("Too many open files");
149 return(-1) /* exited? */;
151 if (GC_lwp_cache
[next_victim
].lc_id
!= 0)
152 (void)syscall(SYS_close
, GC_lwp_cache
[next_victim
].lc_descr
);
153 GC_lwp_cache
[next_victim
].lc_id
= id
;
154 GC_lwp_cache
[next_victim
].lc_descr
= result
;
155 if (++next_victim
>= max_lwps
)
160 static void uncache_lwp(lwpid_t id
)
164 for (i
= 0; i
< max_lwps
; i
++) {
165 if (GC_lwp_cache
[i
].lc_id
== id
) {
166 (void)syscall(SYS_close
, GC_lwp_cache
[id
].lc_descr
);
167 GC_lwp_cache
[i
].lc_id
= 0;
172 /* Sequence of current lwp ids */
173 static lwpid_t GC_current_ids_default
[DEFAULT_MAX_LWPS
+ 1];
174 static lwpid_t
*GC_current_ids
= GC_current_ids_default
;
176 /* Temporary used below (can be big if large number of LWPs) */
177 static lwpid_t last_ids_default
[DEFAULT_MAX_LWPS
+ 1];
178 static lwpid_t
*last_ids
= last_ids_default
;
181 #define ROUNDUP(n) WORDS_TO_BYTES(ROUNDED_UP_WORDS(n))
183 static void set_max_lwps(GC_word n
)
187 int required_bytes
= ROUNDUP(n
* sizeof(struct lwp_cache_entry
))
188 + ROUNDUP(n
* sizeof(prgregset_t
))
189 + ROUNDUP((n
+ 1) * sizeof(lwpid_t
))
190 + ROUNDUP((n
+ 1) * sizeof(lwpid_t
));
192 GC_expand_hp_inner(divHBLKSZ((word
)required_bytes
));
193 oldmem
= mem
= GC_scratch_alloc(required_bytes
);
194 if (0 == mem
) ABORT("No space for lwp data structures");
197 * We can either flush the old lwp cache or copy it over. Do the latter.
199 memcpy(mem
, GC_lwp_cache
, max_lwps
* sizeof(struct lwp_cache_entry
));
200 GC_lwp_cache
= (struct lwp_cache_entry
*)mem
;
201 mem
+= ROUNDUP(n
* sizeof(struct lwp_cache_entry
));
203 BZERO(GC_lwp_registers
, max_lwps
* sizeof(GC_lwp_registers
[0]));
204 GC_lwp_registers
= (prgregset_t
*)mem
;
205 mem
+= ROUNDUP(n
* sizeof(prgregset_t
));
208 GC_current_ids
= (lwpid_t
*)mem
;
209 mem
+= ROUNDUP((n
+ 1) * sizeof(lwpid_t
));
211 last_ids
= (lwpid_t
*)mem
;
212 mem
+= ROUNDUP((n
+ 1)* sizeof(lwpid_t
));
214 if (mem
> oldmem
+ required_bytes
)
215 ABORT("set_max_lwps buffer overflow");
221 /* Stop all lwps in process. Assumes preemption is off. */
222 /* Caller has allocation lock (and any other locks he may */
224 static void stop_all_lwps()
231 lwpid_t me
= _lwp_self();
233 if (GC_main_proc_fd
== -1) {
234 sprintf(buf
, "/proc/%d", getpid());
235 GC_main_proc_fd
= syscall(SYS_open
, buf
, O_RDONLY
);
236 if (GC_main_proc_fd
< 0) {
238 ABORT("/proc open failed: too many open files");
239 GC_printf1("/proc open failed: errno %d", errno
);
243 BZERO(GC_lwp_registers
, sizeof (prgregset_t
) * max_lwps
);
244 for (i
= 0; i
< max_lwps
; i
++)
247 if (syscall(SYS_ioctl
, GC_main_proc_fd
, PIOCSTATUS
, &status
) < 0)
248 ABORT("Main PIOCSTATUS failed");
249 if (status
.pr_nlwp
< 1)
250 ABORT("Invalid number of lwps returned by PIOCSTATUS");
251 if (status
.pr_nlwp
>= max_lwps
) {
252 set_max_lwps(status
.pr_nlwp
*2 + 10);
254 * The data in the old GC_current_ids and
255 * GC_lwp_registers has been trashed. Cleaning out last_ids
256 * will make sure every LWP gets re-examined.
258 for (i
= 0; i
< max_lwps
; i
++)
262 if (syscall(SYS_ioctl
, GC_main_proc_fd
, PIOCLWPIDS
, GC_current_ids
) < 0)
263 ABORT("PIOCLWPIDS failed");
265 for (i
= 0; GC_current_ids
[i
] != 0 && i
< max_lwps
; i
++) {
266 if (GC_current_ids
[i
] != last_ids
[i
]) {
268 if (GC_current_ids
[i
] != me
) {
269 /* PIOCSTOP doesn't work without a writable */
270 /* descriptor. And that makes the process */
272 if (_lwp_suspend(GC_current_ids
[i
]) < 0) {
273 /* Could happen if the lwp exited */
274 uncache_lwp(GC_current_ids
[i
]);
275 GC_current_ids
[i
] = me
; /* ignore */
281 * In the unlikely event something does a fork between the
282 * PIOCSTATUS and the PIOCLWPIDS.
286 /* All lwps in GC_current_ids != me have been suspended. Note */
287 /* that _lwp_suspend is idempotent. */
288 for (i
= 0; GC_current_ids
[i
] != 0; i
++) {
289 if (GC_current_ids
[i
] != last_ids
[i
]) {
290 if (GC_current_ids
[i
] != me
) {
291 lwp_fd
= open_lwp(GC_current_ids
[i
]);
294 GC_current_ids
[i
] = me
;
297 /* LWP should be stopped. Empirically it sometimes */
298 /* isn't, and more frequently the PR_STOPPED flag */
299 /* is not set. Wait for PR_STOPPED. */
300 if (syscall(SYS_ioctl
, lwp_fd
,
301 PIOCSTATUS
, &status
) < 0) {
302 /* Possible if the descriptor was stale, or */
303 /* we encountered the 2.3 _lwp_suspend bug. */
304 uncache_lwp(GC_current_ids
[i
]);
305 GC_current_ids
[i
] = me
; /* handle next time. */
307 while (!(status
.pr_flags
& PR_STOPPED
)) {
309 if (syscall(SYS_ioctl
, lwp_fd
,
310 PIOCSTATUS
, &status
) < 0) {
311 ABORT("Repeated PIOCSTATUS failed");
313 if (status
.pr_flags
& PR_STOPPED
) break;
316 if (syscall(SYS_ioctl
, lwp_fd
,
317 PIOCSTATUS
, &status
) < 0) {
318 ABORT("Repeated PIOCSTATUS failed");
321 if (status
.pr_who
!= GC_current_ids
[i
]) {
322 /* can happen if thread was on death row */
323 uncache_lwp(GC_current_ids
[i
]);
324 GC_current_ids
[i
] = me
; /* handle next time. */
327 /* Save registers where collector can */
329 BCOPY(status
.pr_reg
, GC_lwp_registers
[i
],
330 sizeof (prgregset_t
));
336 for (i
= 0; i
< max_lwps
; i
++) last_ids
[i
] = GC_current_ids
[i
];
340 /* Restart all lwps in process. Assumes preemption is off. */
341 static void restart_all_lwps()
346 lwpid_t me
= _lwp_self();
349 for (i
= 0; GC_current_ids
[i
] != 0; i
++) {
351 if (GC_current_ids
[i
] != me
) {
352 int lwp_fd
= open_lwp(GC_current_ids
[i
]);
355 if (lwp_fd
< 0) ABORT("open_lwp failed");
356 if (syscall(SYS_ioctl
, lwp_fd
,
357 PIOCSTATUS
, &status
) < 0) {
358 ABORT("PIOCSTATUS failed in restart_all_lwps");
360 if (memcmp(status
.pr_reg
, GC_lwp_registers
[i
],
361 sizeof (prgregset_t
)) != 0) {
364 for(j
= 0; j
< NGREG
; j
++)
366 GC_printf3("%i: %x -> %x\n", j
,
367 GC_lwp_registers
[i
][j
],
370 ABORT("Register contents changed");
372 if (!status
.pr_flags
& PR_STOPPED
) {
373 ABORT("lwp no longer stopped");
378 if (syscall(SYS_ioctl
, lwp_fd
,
379 PIOCGWIN
, &windows
) < 0) {
380 ABORT("PIOCSTATUS failed in restart_all_lwps");
382 if (windows
.wbcnt
> 0) ABORT("unsaved register windows");
386 # endif /* PARANOID */
387 if (GC_current_ids
[i
] == me
) continue;
388 if (_lwp_continue(GC_current_ids
[i
]) < 0) {
389 ABORT("Failed to restart lwp");
392 if (i
>= max_lwps
) ABORT("Too many lwps");
395 GC_bool GC_multithreaded
= 0;
400 if (GC_multithreaded
)
404 void GC_start_world()
406 if (GC_multithreaded
)
411 void GC_thr_init(void);
413 GC_bool GC_thr_initialized
= FALSE
;
415 size_t GC_min_stack_sz
;
419 * stack_head is stored at the top of free stacks
422 struct stack_head
*next
;
427 # define N_FREE_LISTS 25
428 struct stack_head
*GC_stack_free_lists
[N_FREE_LISTS
] = { 0 };
429 /* GC_stack_free_lists[i] is free list for stacks of */
430 /* size GC_min_stack_sz*2**i. */
431 /* Free lists are linked through stack_head stored */ /* at top of stack. */
433 /* Return a stack of size at least *stack_size. *stack_size is */
434 /* replaced by the actual stack size. */
435 /* Caller holds allocation lock. */
436 ptr_t
GC_stack_alloc(size_t * stack_size
)
438 register size_t requested_sz
= *stack_size
;
439 register size_t search_sz
= GC_min_stack_sz
;
440 register int index
= 0; /* = log2(search_sz/GC_min_stack_sz) */
442 register struct stack_head
*result
;
444 while (search_sz
< requested_sz
) {
448 if ((result
= GC_stack_free_lists
[index
]) == 0
449 && (result
= GC_stack_free_lists
[index
+1]) != 0) {
450 /* Try next size up. */
451 search_sz
*= 2; index
++;
454 base
= GC_stack_free_lists
[index
]->base
;
455 GC_stack_free_lists
[index
] = GC_stack_free_lists
[index
]->next
;
458 base
= (ptr_t
)mmap(0, search_sz
+ GC_page_size
,
459 PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_NORESERVE
,
461 if (base
== (ptr_t
)-1)
467 mprotect(base
, GC_page_size
, PROT_NONE
);
468 /* Should this use divHBLKSZ(search_sz + GC_page_size) ? -- cf */
469 GC_is_fresh((struct hblk
*)base
, divHBLKSZ(search_sz
));
470 base
+= GC_page_size
;
473 base
= (ptr_t
) GC_scratch_alloc(search_sz
+ 2*GC_page_size
);
480 base
= (ptr_t
)(((word
)base
+ GC_page_size
) & ~(GC_page_size
- 1));
481 /* Protect hottest page to detect overflow. */
482 # ifdef SOLARIS23_MPROTECT_BUG_FIXED
483 mprotect(base
, GC_page_size
, PROT_NONE
);
485 GC_is_fresh((struct hblk
*)base
, divHBLKSZ(search_sz
));
487 base
+= GC_page_size
;
490 *stack_size
= search_sz
;
494 /* Caller holds allocationlock. */
495 void GC_stack_free(ptr_t stack
, size_t size
)
497 register int index
= 0;
498 register size_t search_sz
= GC_min_stack_sz
;
499 register struct stack_head
*head
;
503 mmap(stack
, size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_NORESERVE
|MAP_FIXED
,
506 while (search_sz
< size
) {
510 if (search_sz
!= size
) ABORT("Bad stack size");
512 head
= (struct stack_head
*)(stack
+ search_sz
- sizeof(struct stack_head
));
513 head
->next
= GC_stack_free_lists
[index
];
515 GC_stack_free_lists
[index
] = head
;
518 void GC_my_stack_limits();
520 /* Notify virtual dirty bit implementation that known empty parts of */
521 /* stacks do not contain useful data. */
522 /* Caller holds allocation lock. */
523 void GC_old_stacks_are_fresh()
525 /* No point in doing this for MMAP stacks - and pointers are zero'd out */
526 /* by the mmap in GC_stack_free */
529 register struct stack_head
*s
;
532 register struct hblk
* h
;
535 for (i
= 0, sz
= GC_min_stack_sz
; i
< N_FREE_LISTS
;
537 for (s
= GC_stack_free_lists
[i
]; s
!= 0; s
= s
->next
) {
539 h
= (struct hblk
*)(((word
)p
+ HBLKSIZE
-1) & ~(HBLKSIZE
-1));
541 GC_is_fresh((struct hblk
*)p
, divHBLKSZ(sz
));
543 GC_is_fresh((struct hblk
*)p
, divHBLKSZ(sz
) - 1);
544 BZERO(p
, (ptr_t
)h
- p
);
548 #endif /* MMAP_STACKS */
549 GC_my_stack_limits();
552 /* The set of all known threads. We intercept thread creation and */
553 /* joins. We never actually create detached threads. We allocate all */
554 /* new thread stacks ourselves. These allow us to maintain this */
555 /* data structure. */
557 # define THREAD_TABLE_SZ 128 /* Must be power of 2 */
558 volatile GC_thread GC_threads
[THREAD_TABLE_SZ
];
560 void GC_push_thread_structures
GC_PROTO((void))
562 GC_push_all((ptr_t
)(GC_threads
), (ptr_t
)(GC_threads
)+sizeof(GC_threads
));
565 /* Add a thread to GC_threads. We assume it wasn't already there. */
566 /* Caller holds allocation lock. */
567 GC_thread
GC_new_thread(thread_t id
)
569 int hv
= ((word
)id
) % THREAD_TABLE_SZ
;
571 static struct GC_Thread_Rep first_thread
;
572 static GC_bool first_thread_used
= FALSE
;
574 if (!first_thread_used
) {
575 result
= &first_thread
;
576 first_thread_used
= TRUE
;
577 /* Dont acquire allocation lock, since we may already hold it. */
579 result
= (struct GC_Thread_Rep
*)
580 GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep
), NORMAL
);
582 if (result
== 0) return(0);
584 result
-> next
= GC_threads
[hv
];
585 GC_threads
[hv
] = result
;
586 /* result -> finished = 0; */
587 (void) cond_init(&(result
->join_cv
), USYNC_THREAD
, 0);
591 /* Delete a thread from GC_threads. We assume it is there. */
592 /* (The code intentionally traps if it wasn't.) */
593 /* Caller holds allocation lock. */
594 void GC_delete_thread(thread_t id
)
596 int hv
= ((word
)id
) % THREAD_TABLE_SZ
;
597 register GC_thread p
= GC_threads
[hv
];
598 register GC_thread prev
= 0;
600 while (p
-> id
!= id
) {
605 GC_threads
[hv
] = p
-> next
;
607 prev
-> next
= p
-> next
;
611 /* Return the GC_thread correpsonding to a given thread_t. */
612 /* Returns 0 if it's not there. */
613 /* Caller holds allocation lock. */
614 GC_thread
GC_lookup_thread(thread_t id
)
616 int hv
= ((word
)id
) % THREAD_TABLE_SZ
;
617 register GC_thread p
= GC_threads
[hv
];
619 while (p
!= 0 && p
-> id
!= id
) p
= p
-> next
;
623 /* Solaris 2/Intel uses an initial stack size limit slightly bigger than the
624 SPARC default of 8 MB. Account for this to warn only if the user has
625 raised the limit beyond the default.
627 This is identical to DFLSSIZ defined in <sys/vm_machparam.h>. This file
628 is installed in /usr/platform/`uname -m`/include, which is not in the
629 default include directory list, so copy the definition here. */
631 # define MAX_ORIG_STACK_SIZE (8 * 1024 * 1024 + ((USRSTACK) & 0x3FFFFF))
633 # define MAX_ORIG_STACK_SIZE (8 * 1024 * 1024)
636 word
GC_get_orig_stack_size() {
638 static int warned
= 0;
641 if (getrlimit(RLIMIT_STACK
, &rl
) != 0) ABORT("getrlimit failed");
642 result
= (word
)rl
.rlim_cur
& ~(HBLKSIZE
-1);
643 if (result
> MAX_ORIG_STACK_SIZE
) {
645 WARN("Large stack limit(%ld): only scanning 8 MB\n", result
);
648 result
= MAX_ORIG_STACK_SIZE
;
653 /* Notify dirty bit implementation of unused parts of my stack. */
654 /* Caller holds allocation lock. */
655 void GC_my_stack_limits()
658 register ptr_t hottest
= (ptr_t
)((word
)(&dummy
) & ~(HBLKSIZE
-1));
659 register GC_thread me
= GC_lookup_thread(thr_self());
660 register size_t stack_size
= me
-> stack_size
;
661 register ptr_t stack
;
663 if (stack_size
== 0) {
664 /* original thread */
665 /* Empirically, what should be the stack page with lowest */
666 /* address is actually inaccessible. */
667 stack_size
= GC_get_orig_stack_size() - GC_page_size
;
668 stack
= GC_stackbottom
- stack_size
+ GC_page_size
;
672 if (stack
> hottest
|| stack
+ stack_size
< hottest
) {
673 ABORT("sp out of bounds");
675 GC_is_fresh((struct hblk
*)stack
, divHBLKSZ(hottest
- stack
));
679 /* We hold allocation lock. Should do exactly the right thing if the */
680 /* world is stopped. Should not fail if it isn't. */
681 void GC_push_all_stacks()
684 register GC_thread p
;
685 register ptr_t sp
= GC_approx_sp();
686 register ptr_t bottom
, top
;
689 # define PUSH(bottom,top) \
690 if (GC_dirty_maintained) { \
691 GC_push_selected((bottom), (top), GC_page_was_ever_dirty, \
692 GC_push_all_stack); \
694 GC_push_all_stack((bottom), (top)); \
696 GC_push_all_stack((ptr_t
)GC_lwp_registers
,
697 (ptr_t
)GC_lwp_registers
698 + max_lwps
* sizeof(GC_lwp_registers
[0]));
699 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
700 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
701 if (p
-> stack_size
!= 0) {
703 top
= p
-> stack
+ p
-> stack_size
;
705 /* The original stack. */
706 bottom
= GC_stackbottom
- GC_get_orig_stack_size() + GC_page_size
;
707 top
= GC_stackbottom
;
709 if ((word
)sp
> (word
)bottom
&& (word
)sp
< (word
)top
) bottom
= sp
;
716 int GC_is_thread_stack(ptr_t addr
)
719 register GC_thread p
;
720 register ptr_t bottom
, top
;
722 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
723 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
724 if (p
-> stack_size
!= 0) {
725 if (p
-> stack
<= addr
&&
726 addr
< p
-> stack
+ p
-> stack_size
)
734 /* The only thread that ever really performs a thr_join. */
735 void * GC_thr_daemon(void * dummy
)
739 register GC_thread t
;
745 result
= thr_join((thread_t
)0, &departed
, &status
);
748 /* No more threads; wait for create. */
749 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
750 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> next
) {
751 if (!(t
-> flags
& (DETACHED
| FINISHED
))) {
753 goto start
; /* Thread started just before we */
754 /* acquired the lock. */
758 cond_wait(&GC_create_cv
, &GC_allocate_ml
);
761 t
= GC_lookup_thread(departed
);
763 if (!(t
-> flags
& CLIENT_OWNS_STACK
)) {
764 GC_stack_free(t
-> stack
, t
-> stack_size
);
766 if (t
-> flags
& DETACHED
) {
767 GC_delete_thread(departed
);
769 t
-> status
= status
;
770 t
-> flags
|= FINISHED
;
771 cond_signal(&(t
-> join_cv
));
772 cond_broadcast(&GC_prom_join_cv
);
779 /* We hold the allocation lock, or caller ensures that 2 instances */
780 /* cannot be invoked concurrently. */
781 void GC_thr_init(void)
786 if (GC_thr_initialized
)
788 GC_thr_initialized
= TRUE
;
789 GC_min_stack_sz
= ((thr_min_stack() + 32*1024 + HBLKSIZE
-1)
792 GC_zfd
= open("/dev/zero", O_RDONLY
);
794 ABORT("Can't open /dev/zero");
795 #endif /* MMAP_STACKS */
796 cond_init(&GC_prom_join_cv
, USYNC_THREAD
, 0);
797 cond_init(&GC_create_cv
, USYNC_THREAD
, 0);
798 /* Add the initial thread, so we can stop it. */
799 t
= GC_new_thread(thr_self());
801 t
-> flags
= DETACHED
| CLIENT_OWNS_STACK
;
802 if (thr_create(0 /* stack */, 0 /* stack_size */, GC_thr_daemon
,
803 0 /* arg */, THR_DETACHED
| THR_DAEMON
,
804 &tid
/* thread_id */) != 0) {
805 ABORT("Cant fork daemon");
807 thr_setprio(tid
, 126);
810 /* We acquire the allocation lock to prevent races with */
811 /* stopping/starting world. */
812 /* This is no more correct than the underlying Solaris 2.X */
813 /* implementation. Under 2.3 THIS IS BROKEN. */
814 int GC_thr_suspend(thread_t target_thread
)
820 result
= thr_suspend(target_thread
);
822 t
= GC_lookup_thread(target_thread
);
823 if (t
== 0) ABORT("thread unknown to GC");
824 t
-> flags
|= SUSPENDED
;
830 int GC_thr_continue(thread_t target_thread
)
836 result
= thr_continue(target_thread
);
838 t
= GC_lookup_thread(target_thread
);
839 if (t
== 0) ABORT("thread unknown to GC");
840 t
-> flags
&= ~SUSPENDED
;
846 int GC_thr_join(thread_t wait_for
, thread_t
*departed
, void **status
)
848 register GC_thread t
;
854 register GC_bool thread_exists
;
857 thread_exists
= FALSE
;
858 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
859 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> next
) {
860 if (!(t
-> flags
& DETACHED
)) {
861 if (t
-> flags
& FINISHED
) {
864 thread_exists
= TRUE
;
868 if (!thread_exists
) {
872 cond_wait(&GC_prom_join_cv
, &GC_allocate_ml
);
875 t
= GC_lookup_thread(wait_for
);
876 if (t
== 0 || t
-> flags
& DETACHED
) {
880 if (wait_for
== thr_self()) {
884 while (!(t
-> flags
& FINISHED
)) {
885 cond_wait(&(t
-> join_cv
), &GC_allocate_ml
);
890 if (status
) *status
= t
-> status
;
891 if (departed
) *departed
= t
-> id
;
892 cond_destroy(&(t
-> join_cv
));
893 GC_delete_thread(t
-> id
);
901 GC_thr_create(void *stack_base
, size_t stack_size
,
902 void *(*start_routine
)(void *), void *arg
, long flags
,
903 thread_t
*new_thread
)
907 thread_t my_new_thread
;
909 void * stack
= stack_base
;
912 if (!GC_is_initialized
) GC_init_inner();
915 if (stack_size
== 0) stack_size
= 1024*1024;
916 stack
= (void *)GC_stack_alloc(&stack_size
);
923 my_flags
|= CLIENT_OWNS_STACK
;
925 if (flags
& THR_DETACHED
) my_flags
|= DETACHED
;
926 if (flags
& THR_SUSPENDED
) my_flags
|= SUSPENDED
;
927 result
= thr_create(stack
, stack_size
, start_routine
,
928 arg
, flags
& ~THR_DETACHED
, &my_new_thread
);
930 t
= GC_new_thread(my_new_thread
);
931 t
-> flags
= my_flags
;
932 if (!(my_flags
& DETACHED
)) cond_init(&(t
-> join_cv
), USYNC_THREAD
, 0);
934 t
-> stack_size
= stack_size
;
935 if (new_thread
!= 0) *new_thread
= my_new_thread
;
936 cond_signal(&GC_create_cv
);
939 if (!(my_flags
& CLIENT_OWNS_STACK
)) {
940 GC_stack_free(stack
, stack_size
);
947 # else /* !GC_SOLARIS_THREADS */
950 int GC_no_sunOS_threads
;