home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / SOL_THDS.C < prev    next >
C/C++ Source or Header  |  1994-09-14  |  22KB  |  739 lines

  1. /* 
  2.  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
  3.  *
  4.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  6.  *
  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.
  12.  */
  13. /*
  14.  * Support code for Solaris threads.  Provides functionality we wish Sun
  15.  * had provided.  Relies on some information we probably shouldn't rely on.
  16.  */
  17. /* Boehm, September 14, 1994 4:44 pm PDT */
  18.  
  19. # if defined(SOLARIS_THREADS)
  20.  
  21. # include "gc_priv.h"
  22. # include <thread.h>
  23. # include <synch.h>
  24. # include <signal.h>
  25. # include <fcntl.h>
  26. # include <sys/types.h>
  27. # include <sys/mman.h>
  28. # include <sys/time.h>
  29. # include <sys/resource.h>
  30. # include <sys/stat.h>
  31. # include <sys/syscall.h>
  32. # include <sys/procfs.h>
  33. # include <sys/lwp.h>
  34. # include <sys/reg.h>
  35. # define _CLASSIC_XOPEN_TYPES
  36. # include <unistd.h>
  37.  
  38. # define MAX_LWPS    32
  39.  
  40. #undef thr_join
  41. #undef thr_create
  42. #undef thr_suspend
  43. #undef thr_continue
  44.  
  45. cond_t GC_prom_join_cv;        /* Broadcast when any thread terminates    */
  46. cond_t GC_create_cv;        /* Signalled when a new undetached    */
  47.                 /* thread starts.            */
  48.                 
  49.  
  50. /* We use the allocation lock to protect thread-related data structures. */
  51.  
  52. /* We stop the world using /proc primitives.  This makes some    */
  53. /* minimal assumptions about the threads implementation.    */
  54. /* We don't play by the rules, since the rules make this    */
  55. /* impossible (as of Solaris 2.3).  Also note that as of    */
  56. /* Solaris 2.3 the various thread and lwp suspension        */
  57. /* primitives failed to stop threads by the time the request    */
  58. /* is completed.                        */
  59.  
  60.  
  61. static sigset_t old_mask;
  62. # define MAX_LWPS    32
  63.  
  64. /* Sleep for n milliseconds, n < 1000    */
  65. void GC_msec_sleep(int n)
  66. {
  67.     struct timespec ts;
  68.                             
  69.     ts.tv_sec = 0;
  70.     ts.tv_nsec = 1000000*n;
  71.     if (syscall(SYS_nanosleep, &ts, 0) < 0) {
  72.     ABORT("nanosleep failed");
  73.     }
  74. }
  75. /* Turn off preemption;  gross but effective.          */
  76. /* Caller has allocation lock.                */
  77. /* Actually this is not needed under Solaris 2.3 and    */
  78. /* 2.4, but hopefully that'll change.            */
  79. void preempt_off()
  80. {
  81.     sigset_t set;
  82.  
  83.     (void)sigfillset(&set);
  84.     syscall(SYS_sigprocmask, SIG_SETMASK, &set, &old_mask);
  85. }
  86.  
  87. void preempt_on()
  88. {
  89.     syscall(SYS_sigprocmask, SIG_SETMASK, &old_mask, NULL);
  90. }
  91.  
  92. int GC_main_proc_fd = -1;
  93.  
  94. struct lwp_cache_entry {
  95.     lwpid_t lc_id;
  96.     int lc_descr;    /* /proc file descriptor.    */
  97. } GC_lwp_cache[MAX_LWPS];
  98.  
  99. prgregset_t GC_lwp_registers[MAX_LWPS];
  100.  
  101. /* Return a file descriptor for the /proc entry corresponding    */
  102. /* to the given lwp.  The file descriptor may be stale if the    */
  103. /* lwp exited and a new one was forked.                */
  104. static int open_lwp(lwpid_t id)
  105. {
  106.     int result;
  107.     static int next_victim = 0;
  108.     register int i;
  109.     
  110.     for (i = 0; i < MAX_LWPS; i++) {
  111.         if (GC_lwp_cache[i].lc_id == id) return(GC_lwp_cache[i].lc_descr);
  112.     }
  113.     if ((result = syscall(SYS_ioctl, GC_main_proc_fd, PIOCOPENLWP, &id)) < 0) {
  114.         return(-1) /* exited? */;
  115.     }
  116.     if (GC_lwp_cache[next_victim].lc_id != 0)
  117.         (void)syscall(SYS_close, GC_lwp_cache[next_victim].lc_descr);
  118.     GC_lwp_cache[next_victim].lc_id = id;
  119.     GC_lwp_cache[next_victim].lc_descr = result;
  120.     next_victim++;
  121.     return(result);
  122. }
  123.  
  124. static void uncache_lwp(lwpid_t id)
  125. {
  126.     register int i;
  127.     
  128.     for (i = 0; i < MAX_LWPS; i++) {
  129.         if (GC_lwp_cache[i].lc_id == id) {
  130.             (void)syscall(SYS_close, GC_lwp_cache[id].lc_descr);
  131.             GC_lwp_cache[i].lc_id = 0;
  132.             break;
  133.         }
  134.     }
  135. }
  136.  
  137. lwpid_t GC_current_ids[MAX_LWPS + 1];    /* Sequence of current lwp ids    */
  138.  
  139. /* Stop all lwps in process.  Assumes preemption is off.    */
  140. /* Caller has allocation lock (and any other locks he may    */
  141. /* need).                            */
  142. static void stop_all_lwps()
  143. {
  144.     int lwp_fd;
  145.     char buf[30];
  146.     prstatus_t status;
  147.     lwpid_t last_ids[MAX_LWPS + 1];
  148.     register int i;
  149.     bool changed;
  150.     lwpid_t me = _lwp_self();
  151.  
  152.     if (GC_main_proc_fd == -1) {
  153.         sprintf(buf, "/proc/%d", getpid());
  154.         GC_main_proc_fd = syscall(SYS_open, buf, O_RDONLY);
  155.         if (GC_main_proc_fd < 0) {
  156.             ABORT("/proc open failed");
  157.         }
  158.     }
  159.     if (syscall(SYS_ioctl, GC_main_proc_fd, PIOCSTATUS, &status) < 0)
  160.         ABORT("Main PIOCSTATUS failed");
  161.     if (status.pr_nlwp < 1 || status.pr_nlwp > MAX_LWPS) {
  162.         ABORT("Too many lwps");
  163.         /* Only a heuristic.  There seems to be no way to do this right, */
  164.         /* since there can be intervening forks.             */
  165.     }
  166.     BZERO(GC_lwp_registers, sizeof GC_lwp_registers);
  167.     for (i = 0; i <= MAX_LWPS; i++) last_ids[i] = 0;
  168.     for (;;) {
  169.         if (syscall(SYS_ioctl, GC_main_proc_fd, PIOCLWPIDS, GC_current_ids) < 0) {
  170.             ABORT("PIOCLWPIDS failed");
  171.         }
  172.         changed = FALSE;
  173.         for (i = 0; GC_current_ids[i] != 0; i++) {
  174.             if (GC_current_ids[i] != last_ids[i]) {
  175.                 changed = TRUE;
  176.                 if (GC_current_ids[i] != me) {
  177.             /* PIOCSTOP doesn't work without a writable        */
  178.             /* descriptor.  And that makes the process        */
  179.             /* undebuggable.                    */
  180.                     if (_lwp_suspend(GC_current_ids[i]) < 0) {
  181.                         /* Could happen if the lwp exited */
  182.                         uncache_lwp(GC_current_ids[i]);
  183.                         GC_current_ids[i] = me; /* ignore */
  184.                     }
  185.                 }
  186.             }
  187.             if (i >= MAX_LWPS) ABORT("Too many lwps");
  188.         }
  189.         /* All lwps in GC_current_ids != me have been suspended.  Note    */
  190.         /* that _lwp_suspend is idempotent.                */
  191.         for (i = 0; GC_current_ids[i] != 0; i++) {
  192.             if (GC_current_ids[i] != last_ids[i]) {
  193.                 if (GC_current_ids[i] != me) {
  194.                     lwp_fd = open_lwp(GC_current_ids[i]);
  195.             /* LWP should be stopped.  Empirically it sometimes    */
  196.             /* isn't, and more frequently the PR_STOPPED flag    */
  197.             /* is not set.  Wait for PR_STOPPED.        */
  198.                     if (syscall(SYS_ioctl, lwp_fd,
  199.                                 PIOCSTATUS, &status) < 0) {
  200.             /* Possible if the descriptor was stale, or */
  201.             /* we encountered the 2.3 _lwp_suspend bug. */
  202.             uncache_lwp(GC_current_ids[i]);
  203.                         GC_current_ids[i] = me; /* handle next time. */
  204.                     } else {
  205.                         while (!(status.pr_flags & PR_STOPPED)) {
  206.                             GC_msec_sleep(1);
  207.                 if (syscall(SYS_ioctl, lwp_fd,
  208.                         PIOCSTATUS, &status) < 0) {
  209.                                 ABORT("Repeated PIOCSTATUS failed");
  210.                 }
  211.                 if (status.pr_flags & PR_STOPPED) break;
  212.                 
  213.                 GC_msec_sleep(20);
  214.                 if (syscall(SYS_ioctl, lwp_fd,
  215.                         PIOCSTATUS, &status) < 0) {
  216.                                 ABORT("Repeated PIOCSTATUS failed");
  217.                 }
  218.                         }
  219.                         if (status.pr_who !=  GC_current_ids[i]) {
  220.                             ABORT("Wrong lwp");
  221.                         }
  222.                         /* Save registers where collector can */
  223.             /* find them.              */
  224.                 BCOPY(status.pr_reg, GC_lwp_registers[i],
  225.                   sizeof (prgregset_t));
  226.                     }
  227.                 }
  228.             }
  229.         }
  230.         if (!changed) break;
  231.         for (i = 0; i <= MAX_LWPS; i++) last_ids[i] = GC_current_ids[i];
  232.     }
  233. }
  234.  
  235. /* Restart all lwps in process.  Assumes preemption is off.    */
  236. static void restart_all_lwps()
  237. {
  238.     int lwp_fd;
  239.     register int i;
  240.     bool changed;
  241.     lwpid_t me = _lwp_self();
  242. #   define PARANOID
  243.  
  244.     for (i = 0; GC_current_ids[i] != 0; i++) {
  245. #    ifdef PARANOID
  246.       if (GC_current_ids[i] != me) {
  247.         int lwp_fd = open_lwp(GC_current_ids[i]);
  248.         prstatus_t status;
  249.         gwindows_t windows;
  250.         
  251.         if (lwp_fd < 0) ABORT("open_lwp failed");
  252.         if (syscall(SYS_ioctl, lwp_fd,
  253.             PIOCSTATUS, &status) < 0) {
  254.                 ABORT("PIOCSTATUS failed in restart_all_lwps");
  255.         }
  256.         if (memcmp(status.pr_reg, GC_lwp_registers[i],
  257.                sizeof (prgregset_t)) != 0) {
  258.         ABORT("Register contents changed");
  259.         }
  260.         if (!status.pr_flags & PR_STOPPED) {
  261.             ABORT("lwp no longer stopped");
  262.         }
  263.         if (syscall(SYS_ioctl, lwp_fd,
  264.             PIOCGWIN, &windows) < 0) {
  265.                 ABORT("PIOCSTATUS failed in restart_all_lwps");
  266.         }
  267.         if (windows.wbcnt > 0) ABORT("unsaved register windows");
  268.       }
  269. #    endif /* PARANOID */
  270.     if (GC_current_ids[i] == me) continue;
  271.         if (_lwp_continue(GC_current_ids[i]) < 0) {
  272.             ABORT("Failed to restart lwp");
  273.         }
  274.     }
  275.     if (i >= MAX_LWPS) ABORT("Too many lwps");
  276. }
  277.  
  278.  
  279. void GC_stop_world()
  280. {
  281.     preempt_off();
  282.     stop_all_lwps();
  283. }
  284.  
  285. void GC_start_world()
  286. {
  287.     restart_all_lwps();
  288.     preempt_on();
  289. }
  290.  
  291. bool GC_thr_initialized = FALSE;
  292.  
  293. size_t GC_min_stack_sz;
  294.  
  295. size_t GC_page_sz;
  296.  
  297.  
  298. # define N_FREE_LISTS 25
  299. ptr_t GC_stack_free_lists[N_FREE_LISTS] = { 0 };
  300.         /* GC_stack_free_lists[i] is free list for stacks of     */
  301.         /* size GC_min_stack_sz*2**i.                */
  302.         /* Free lists are linked through first word.        */
  303.  
  304. /* Return a stack of size at least *stack_size.  *stack_size is    */
  305. /* replaced by the actual stack size.                */
  306. /* Caller holds allocation lock.                */
  307. ptr_t GC_stack_alloc(size_t * stack_size)
  308. {
  309.     register size_t requested_sz = *stack_size;
  310.     register size_t search_sz = GC_min_stack_sz;
  311.     register int index = 0;    /* = log2(search_sz/GC_min_stack_sz) */
  312.     register ptr_t result;
  313.     
  314.     while (search_sz < requested_sz) {
  315.         search_sz *= 2;
  316.         index++;
  317.     }
  318.     if ((result = GC_stack_free_lists[index]) == 0
  319.         && (result = GC_stack_free_lists[index+1]) != 0) {
  320.         /* Try next size up. */
  321.         search_sz *= 2; index++;
  322.     }
  323.     if (result != 0) {
  324.         GC_stack_free_lists[index] = *(ptr_t *)result;
  325.     } else {
  326.         result = (ptr_t) GC_scratch_alloc(search_sz + 2*GC_page_sz);
  327.         result = (ptr_t)(((word)result + GC_page_sz) & ~(GC_page_sz - 1));
  328.         /* Protect hottest page to detect overflow. */
  329. #    ifdef SOLARIS23_MPROTECT_BUG_FIXED
  330.             mprotect(result, GC_page_sz, PROT_NONE);
  331. #    endif
  332.         GC_is_fresh((struct hblk *)result, divHBLKSZ(search_sz));
  333.         result += GC_page_sz;
  334.     }
  335.     *stack_size = search_sz;
  336.     return(result);
  337. }
  338.  
  339. /* Caller holds  allocationlock.                    */
  340. void GC_stack_free(ptr_t stack, size_t size)
  341. {
  342.     register int index = 0;
  343.     register size_t search_sz = GC_min_stack_sz;
  344.     
  345.     while (search_sz < size) {
  346.         search_sz *= 2;
  347.         index++;
  348.     }
  349.     if (search_sz != size) ABORT("Bad stack size");
  350.     *(ptr_t *)stack = GC_stack_free_lists[index];
  351.     GC_stack_free_lists[index] = stack;
  352. }
  353.  
  354. void GC_my_stack_limits();
  355.  
  356. /* Notify virtual dirty bit implementation that known empty parts of    */
  357. /* stacks do not contain useful data.                    */ 
  358. /* Caller holds allocation lock.                    */
  359. void GC_old_stacks_are_fresh()
  360. {
  361.     register int i;
  362.     register ptr_t p;
  363.     register size_t sz;
  364.     register struct hblk * h;
  365.     int dummy;
  366.     
  367.     if (!GC_thr_initialized) GC_thr_init();
  368.     for (i = 0, sz= GC_min_stack_sz; i < N_FREE_LISTS;
  369.          i++, sz *= 2) {
  370.          for (p = GC_stack_free_lists[i]; p != 0; p = *(ptr_t *)p) {
  371.              h = (struct hblk *)(((word)p + HBLKSIZE-1) & ~(HBLKSIZE-1));
  372.              if ((ptr_t)h == p) {
  373.                  GC_is_fresh((struct hblk *)p, divHBLKSZ(sz));
  374.              } else {
  375.                  GC_is_fresh((struct hblk *)p, divHBLKSZ(sz) - 1);
  376.                  BZERO(p, (ptr_t)h - p);
  377.              }
  378.          }
  379.     }
  380.     GC_my_stack_limits();
  381. }
  382.  
  383. /* The set of all known threads.  We intercept thread creation and     */
  384. /* joins.  We never actually create detached threads.  We allocate all     */
  385. /* new thread stacks ourselves.  These allow us to maintain this    */
  386. /* data structure.                            */
  387. /* Protected by GC_thr_lock.                        */
  388. /* Some of this should be declared vaolatile, but that's incosnsistent    */
  389. /* with some library routine declarations.  In particular, the         */
  390. /* definition of cond_t doesn't mention volatile!            */
  391. typedef struct GC_Thread_Rep {
  392.     struct GC_Thread_Rep * next;
  393.     thread_t id;
  394.     word flags;
  395. #    define FINISHED 1       /* Thread has exited.    */
  396. #    define DETACHED 2    /* Thread is intended to be detached.    */
  397. #    define CLIENT_OWNS_STACK    4
  398.                 /* Stack was supplied by client.    */
  399. #    define SUSPENDED 8    /* Currently suspended.    */    
  400.     ptr_t stack;
  401.     size_t stack_size;
  402.     cond_t join_cv;
  403.     void * status;
  404. } * GC_thread;
  405.  
  406. # define THREAD_TABLE_SZ 128    /* Must be power of 2    */
  407. volatile GC_thread GC_threads[THREAD_TABLE_SZ];
  408.  
  409. /* Add a thread to GC_threads.  We assume it wasn't already there.    */
  410. /* Caller holds allocation lock.                    */
  411. GC_thread GC_new_thread(thread_t id)
  412. {
  413.     int hv = ((word)id) % THREAD_TABLE_SZ;
  414.     GC_thread result;
  415.     static struct GC_Thread_Rep first_thread;
  416.     static bool first_thread_used = FALSE;
  417.     
  418.     if (!first_thread_used) {
  419.         result = &first_thread;
  420.         first_thread_used = TRUE;
  421.         /* Dont acquire allocation lock, since we may already hold it. */
  422.     } else {
  423.         result = (struct GC_Thread_Rep *)
  424.              GC_generic_malloc_inner(sizeof(struct GC_Thread_Rep), NORMAL);
  425.     }
  426.     if (result == 0) return(0);
  427.     result -> id = id;
  428.     result -> next = GC_threads[hv];
  429.     GC_threads[hv] = result;
  430.     /* result -> finished = 0; */
  431.     (void) cond_init(&(result->join_cv), USYNC_THREAD, 0);
  432.     return(result);
  433. }
  434.  
  435. /* Delete a thread from GC_threads.  We assume it is there.    */
  436. /* (The code intentionally traps if it wasn't.)            */
  437. /* Caller holds allocation lock.                */
  438. void GC_delete_thread(thread_t id)
  439. {
  440.     int hv = ((word)id) % THREAD_TABLE_SZ;
  441.     register GC_thread p = GC_threads[hv];
  442.     register GC_thread prev = 0;
  443.     
  444.     while (p -> id != id) {
  445.         prev = p;
  446.         p = p -> next;
  447.     }
  448.     if (prev == 0) {
  449.         GC_threads[hv] = p -> next;
  450.     } else {
  451.         prev -> next = p -> next;
  452.     }
  453. }
  454.  
  455. /* Return the GC_thread correpsonding to a given thread_t.    */
  456. /* Returns 0 if it's not there.                    */
  457. /* Caller holds  allocation lock.                */
  458. GC_thread GC_lookup_thread(thread_t id)
  459. {
  460.     int hv = ((word)id) % THREAD_TABLE_SZ;
  461.     register GC_thread p = GC_threads[hv];
  462.     
  463.     while (p != 0 && p -> id != id) p = p -> next;
  464.     return(p);
  465. }
  466.  
  467. /* Notify dirty bit implementation of unused parts of my stack. */
  468. /* Caller holds allocation lock.                */
  469. void GC_my_stack_limits()
  470. {
  471.     int dummy;
  472.     register ptr_t hottest = (ptr_t)((word)(&dummy) & ~(HBLKSIZE-1));
  473.     register GC_thread me = GC_lookup_thread(thr_self());
  474.     register size_t stack_size = me -> stack_size;
  475.     register ptr_t stack;
  476.     
  477.     if (stack_size == 0) {
  478.       /* original thread */
  479.         struct rlimit rl;
  480.          
  481.         if (getrlimit(RLIMIT_STACK, &rl) != 0) ABORT("getrlimit failed");
  482.         /* Empirically, what should be the stack page with lowest    */
  483.         /* address is actually inaccessible.                */
  484.         stack_size = ((word)rl.rlim_cur & ~(HBLKSIZE-1)) - GC_page_sz;
  485.         stack = GC_stackbottom - stack_size + GC_page_sz;
  486.     } else {
  487.         stack = me -> stack;
  488.     }
  489.     if (stack > hottest || stack + stack_size < hottest) {
  490.         ABORT("sp out of bounds");
  491.     }
  492.     GC_is_fresh((struct hblk *)stack, divHBLKSZ(hottest - stack));
  493. }
  494.  
  495.  
  496. extern ptr_t GC_approx_sp();
  497.  
  498. /* We hold allocation lock.  We assume the world is stopped.    */
  499. void GC_push_all_stacks()
  500. {
  501.     register int i;
  502.     register GC_thread p;
  503.     register ptr_t sp = GC_approx_sp();
  504.     register ptr_t bottom, top;
  505.     struct rlimit rl;
  506.     
  507. #   define PUSH(bottom,top) \
  508.       if (GC_dirty_maintained) { \
  509.     GC_push_dirty((bottom), (top), GC_page_was_ever_dirty, \
  510.               GC_push_all_stack); \
  511.       } else { \
  512.         GC_push_all_stack((bottom), (top)); \
  513.       }
  514.     if (!GC_thr_initialized) GC_thr_init();
  515.     for (i = 0; i < THREAD_TABLE_SZ; i++) {
  516.       for (p = GC_threads[i]; p != 0; p = p -> next) {
  517.         if (p -> stack_size != 0) {
  518.             bottom = p -> stack;
  519.             top = p -> stack + p -> stack_size;
  520.         } else {
  521.             /* The original stack. */
  522.             if (getrlimit(RLIMIT_STACK, &rl) != 0) ABORT("getrlimit failed");
  523.             bottom = GC_stackbottom - rl.rlim_cur + GC_page_sz;
  524.             top = GC_stackbottom;
  525.         }
  526.         if ((word)sp > (word)bottom && (word)sp < (word)top) bottom = sp;
  527.         PUSH(bottom, top);
  528.       }
  529.     }
  530. }
  531.  
  532. /* The only thread that ever really performs a thr_join.    */
  533. void * GC_thr_daemon(void * dummy)
  534. {
  535.     void *status;
  536.     thread_t departed;
  537.     register GC_thread t;
  538.     register int i;
  539.     register int result;
  540.     
  541.     for(;;) {
  542.       start:
  543.         result = thr_join((thread_t)0, &departed, &status);
  544.         LOCK();
  545.         if (result != 0) {
  546.             /* No more threads; wait for create. */
  547.             for (i = 0; i < THREAD_TABLE_SZ; i++) {
  548.                 for (t = GC_threads[i]; t != 0; t = t -> next) {
  549.                     if (!(t -> flags & (DETACHED | FINISHED))) {
  550.                       UNLOCK();
  551.                       goto start; /* Thread started just before we */
  552.                                 /* acquired the lock.           */
  553.                     }
  554.                 }
  555.             }
  556.             cond_wait(&GC_create_cv, &GC_allocate_ml);
  557.             UNLOCK();
  558.         } else {
  559.             t = GC_lookup_thread(departed);
  560.             if (!(t -> flags & CLIENT_OWNS_STACK)) {
  561.                 GC_stack_free(t -> stack, t -> stack_size);
  562.             }
  563.             if (t -> flags & DETACHED) {
  564.                 GC_delete_thread(departed);
  565.             } else {
  566.                 t -> status = status;
  567.                 t -> flags |= FINISHED;
  568.                 cond_signal(&(t -> join_cv));
  569.                 cond_broadcast(&GC_prom_join_cv);
  570.             }
  571.             UNLOCK();
  572.         }
  573.     }
  574. }
  575.  
  576. /* We hold the allocation lock.    */
  577. GC_thr_init()
  578. {
  579.     GC_thread t;
  580.  
  581.     GC_thr_initialized = TRUE;
  582.     GC_min_stack_sz = ((thr_min_stack() + 128*1024 + HBLKSIZE-1)
  583.                    & ~(HBLKSIZE - 1));
  584.     GC_page_sz = sysconf(_SC_PAGESIZE);
  585.     cond_init(&GC_prom_join_cv, USYNC_THREAD, 0);
  586.     cond_init(&GC_create_cv, USYNC_THREAD, 0);
  587.     /* Add the initial thread, so we can stop it.    */
  588.       t = GC_new_thread(thr_self());
  589.       t -> stack_size = 0;
  590.       t -> flags = DETACHED;
  591.     if (thr_create(0 /* stack */, 0 /* stack_size */, GC_thr_daemon,
  592.                0 /* arg */, THR_DETACHED | THR_DAEMON,
  593.                0 /* thread_id */) != 0) {
  594.         ABORT("Cant fork daemon");
  595.     }
  596.     
  597. }
  598.  
  599. /* We acquire the allocation lock to prevent races with     */
  600. /* stopping/starting world.                    */
  601. /* This is no more correct than the underlying Solaris 2.X    */
  602. /* implementation.  Under 2.3 THIS IS BROKEN.            */
  603. int GC_thr_suspend(thread_t target_thread)
  604. {
  605.     GC_thread t;
  606.     int result;
  607.     
  608.     LOCK();
  609.     result = thr_suspend(target_thread);
  610.     if (result == 0) {
  611.         t = GC_lookup_thread(target_thread);
  612.         if (t == 0) ABORT("thread unknown to GC");
  613.         t -> flags |= SUSPENDED;
  614.     }
  615.     UNLOCK();
  616.     return(result);
  617. }
  618.  
  619. int GC_thr_continue(thread_t target_thread)
  620. {
  621.     GC_thread t;
  622.     int result;
  623.     
  624.     LOCK();
  625.     result = thr_continue(target_thread);
  626.     if (result == 0) {
  627.         t = GC_lookup_thread(target_thread);
  628.         if (t == 0) ABORT("thread unknown to GC");
  629.         t -> flags &= ~SUSPENDED;
  630.     }
  631.     UNLOCK();
  632.     return(result);
  633. }
  634.  
  635. int GC_thr_join(thread_t wait_for, thread_t *departed, void **status)
  636. {
  637.     register GC_thread t;
  638.     int result = 0;
  639.     
  640.     LOCK();
  641.     if (wait_for == 0) {
  642.         register int i;
  643.         register bool thread_exists;
  644.     
  645.         for (;;) {
  646.           thread_exists = FALSE;
  647.           for (i = 0; i < THREAD_TABLE_SZ; i++) {
  648.             for (t = GC_threads[i]; t != 0; t = t -> next) {
  649.               if (!(t -> flags & DETACHED)) {
  650.                 if (t -> flags & FINISHED) {
  651.                   goto found;
  652.                 }
  653.                 thread_exists = TRUE;
  654.               }
  655.             }
  656.           }
  657.           if (!thread_exists) {
  658.               result = ESRCH;
  659.               goto out;
  660.           }
  661.           cond_wait(&GC_prom_join_cv, &GC_allocate_ml);
  662.         }
  663.     } else {
  664.         t = GC_lookup_thread(wait_for);
  665.         if (t == 0 || t -> flags & DETACHED) {
  666.             result = ESRCH;
  667.             goto out;
  668.         }
  669.         if (wait_for == thr_self()) {
  670.             result = EDEADLK;
  671.             goto out;
  672.         }
  673.         while (!(t -> flags & FINISHED)) {
  674.             cond_wait(&(t -> join_cv), &GC_allocate_ml);
  675.         }
  676.         
  677.     }
  678.   found:
  679.     if (status) *status = t -> status;
  680.     if (departed) *departed = t -> id;
  681.     cond_destroy(&(t -> join_cv));
  682.     GC_delete_thread(t -> id);
  683.   out:
  684.     UNLOCK();
  685.     return(result);
  686. }
  687.  
  688.  
  689. int
  690. GC_thr_create(void *stack_base, size_t stack_size,
  691.               void *(*start_routine)(void *), void *arg, long flags,
  692.               thread_t *new_thread)
  693. {
  694.     int result;
  695.     GC_thread t;
  696.     thread_t my_new_thread;
  697.     word my_flags = 0;
  698.     void * stack = stack_base;
  699.    
  700.     LOCK();
  701.     if (!GC_thr_initialized) GC_thr_init();
  702.     if (stack == 0) {
  703.          if (stack_size == 0) stack_size = GC_min_stack_sz;
  704.          stack = (void *)GC_stack_alloc(&stack_size);
  705.          if (stack == 0) {
  706.              UNLOCK();
  707.              return(ENOMEM);
  708.          }
  709.     } else {
  710.         my_flags |= CLIENT_OWNS_STACK;
  711.     }
  712.     if (flags & THR_DETACHED) my_flags |= DETACHED;
  713.     if (flags & THR_SUSPENDED) my_flags |= SUSPENDED;
  714.     result = thr_create(stack, stack_size, start_routine,
  715.                    arg, flags & ~THR_DETACHED, &my_new_thread);
  716.     if (result == 0) {
  717.         t = GC_new_thread(my_new_thread);
  718.         t -> flags = my_flags;
  719.         if (!(my_flags & DETACHED)) cond_init(&(t -> join_cv), USYNC_THREAD, 0);
  720.         t -> stack = stack;
  721.         t -> stack_size = stack_size;
  722.         if (new_thread != 0) *new_thread = my_new_thread;
  723.         cond_signal(&GC_create_cv);
  724.     } else if (!(my_flags & CLIENT_OWNS_STACK)) {
  725.           GC_stack_free(stack, stack_size);
  726.     }        
  727.     UNLOCK();  
  728.     return(result);
  729. }
  730.  
  731. # else
  732.  
  733. #ifndef LINT
  734.   int GC_no_sunOS_threads;
  735. #endif
  736.  
  737. # endif /* SOLARIS_THREADS */
  738.  
  739.