Back to index

plt-scheme  4.2.1
solaris_threads.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  */
00013 /*
00014  * Support code for Solaris threads.  Provides functionality we wish Sun
00015  * had provided.  Relies on some information we probably shouldn't rely on.
00016  */
00017 /* Boehm, September 14, 1994 4:44 pm PDT */
00018 
00019 # include "private/gc_priv.h"
00020 
00021 # if defined(GC_SOLARIS_THREADS) || defined(GC_SOLARIS_PTHREADS)
00022 # include "private/solaris_threads.h"
00023 # include <thread.h>
00024 # include <synch.h>
00025 # include <signal.h>
00026 # include <fcntl.h>
00027 # include <sys/types.h>
00028 # include <sys/mman.h>
00029 # include <sys/time.h>
00030 # include <sys/resource.h>
00031 # include <sys/stat.h>
00032 # include <sys/syscall.h>
00033 # include <sys/procfs.h>
00034 # include <sys/lwp.h>
00035 # include <sys/reg.h>
00036 # define _CLASSIC_XOPEN_TYPES
00037 # include <unistd.h>
00038 # include <errno.h>
00039 
00040 #ifdef HANDLE_FORK
00041   --> Not yet supported.  Try porting the code from linux_threads.c.
00042 #endif
00043 
00044 /*
00045  * This is the default size of the LWP arrays. If there are more LWPs
00046  * than this when a stop-the-world GC happens, set_max_lwps will be
00047  * called to cope.
00048  * This must be higher than the number of LWPs at startup time.
00049  * The threads library creates a thread early on, so the min. is 3
00050  */
00051 # define DEFAULT_MAX_LWPS   4
00052 
00053 #undef thr_join
00054 #undef thr_create
00055 #undef thr_suspend
00056 #undef thr_continue
00057 
00058 cond_t GC_prom_join_cv;            /* Broadcast when any thread terminates   */
00059 cond_t GC_create_cv;        /* Signalled when a new undetached */
00060                             /* thread starts.                  */
00061                             
00062 
00063 #ifdef MMAP_STACKS
00064 static int GC_zfd;
00065 #endif /* MMAP_STACKS */
00066 
00067 /* We use the allocation lock to protect thread-related data structures. */
00068 
00069 /* We stop the world using /proc primitives.  This makes some  */
00070 /* minimal assumptions about the threads implementation.       */
00071 /* We don't play by the rules, since the rules make this       */
00072 /* impossible (as of Solaris 2.3).  Also note that as of       */
00073 /* Solaris 2.3 the various thread and lwp suspension           */
00074 /* primitives failed to stop threads by the time the request   */
00075 /* is completed.                                        */
00076 
00077 
00078 static sigset_t old_mask;
00079 
00080 /* Sleep for n milliseconds, n < 1000     */
00081 void GC_msec_sleep(int n)
00082 {
00083     struct timespec ts;
00084                             
00085     ts.tv_sec = 0;
00086     ts.tv_nsec = 1000000*n;
00087     if (syscall(SYS_nanosleep, &ts, 0) < 0) {
00088        ABORT("nanosleep failed");
00089     }
00090 }
00091 /* Turn off preemption;  gross but effective.           */
00092 /* Caller has allocation lock.                          */
00093 /* Actually this is not needed under Solaris 2.3 and    */
00094 /* 2.4, but hopefully that'll change.                   */
00095 void preempt_off()
00096 {
00097     sigset_t set;
00098 
00099     (void)sigfillset(&set);
00100     sigdelset(&set, SIGABRT);
00101     syscall(SYS_sigprocmask, SIG_SETMASK, &set, &old_mask);
00102 }
00103 
00104 void preempt_on()
00105 {
00106     syscall(SYS_sigprocmask, SIG_SETMASK, &old_mask, NULL);
00107 }
00108 
00109 int GC_main_proc_fd = -1;
00110 
00111 
00112 struct lwp_cache_entry {
00113     lwpid_t lc_id;
00114     int lc_descr;    /* /proc file descriptor.   */
00115 }  GC_lwp_cache_default[DEFAULT_MAX_LWPS];
00116 
00117 static int max_lwps = DEFAULT_MAX_LWPS;
00118 static struct lwp_cache_entry *GC_lwp_cache = GC_lwp_cache_default;
00119 
00120 static prgregset_t GC_lwp_registers_default[DEFAULT_MAX_LWPS];
00121 static prgregset_t *GC_lwp_registers = GC_lwp_registers_default;
00122 
00123 /* Return a file descriptor for the /proc entry corresponding  */
00124 /* to the given lwp.  The file descriptor may be stale if the  */
00125 /* lwp exited and a new one was forked.                        */
00126 static int open_lwp(lwpid_t id)
00127 {
00128     int result;
00129     static int next_victim = 0;
00130     register int i;
00131     
00132     for (i = 0; i < max_lwps; i++) {
00133        if (GC_lwp_cache[i].lc_id == id) return(GC_lwp_cache[i].lc_descr);
00134     }
00135     result = syscall(SYS_ioctl, GC_main_proc_fd, PIOCOPENLWP, &id);
00136     /*
00137      * If PIOCOPENLWP fails, try closing fds in the cache until it succeeds.
00138      */
00139     if (result < 0 && errno == EMFILE) {
00140            for (i = 0; i < max_lwps; i++) {
00141               if (GC_lwp_cache[i].lc_id != 0) {
00142                      (void)syscall(SYS_close, GC_lwp_cache[i].lc_descr);
00143                      result = syscall(SYS_ioctl, GC_main_proc_fd, PIOCOPENLWP, &id);
00144                      if (result >= 0 || (result < 0 && errno != EMFILE))
00145                             break;
00146               }
00147            }
00148     }
00149     if (result < 0) {
00150        if (errno == EMFILE) {
00151               ABORT("Too many open files");
00152        }
00153         return(-1) /* exited? */;
00154     }
00155     if (GC_lwp_cache[next_victim].lc_id != 0)
00156         (void)syscall(SYS_close, GC_lwp_cache[next_victim].lc_descr);
00157     GC_lwp_cache[next_victim].lc_id = id;
00158     GC_lwp_cache[next_victim].lc_descr = result;
00159     if (++next_victim >= max_lwps)
00160        next_victim = 0;
00161     return(result);
00162 }
00163 
00164 static void uncache_lwp(lwpid_t id)
00165 {
00166     register int i;
00167     
00168     for (i = 0; i < max_lwps; i++) {
00169        if (GC_lwp_cache[i].lc_id == id) {
00170            (void)syscall(SYS_close, GC_lwp_cache[id].lc_descr);
00171            GC_lwp_cache[i].lc_id = 0;
00172            break;
00173        }
00174     }
00175 }
00176        /* Sequence of current lwp ids     */
00177 static lwpid_t GC_current_ids_default[DEFAULT_MAX_LWPS + 1];
00178 static lwpid_t *GC_current_ids = GC_current_ids_default;
00179 
00180        /* Temporary used below (can be big if large number of LWPs) */
00181 static lwpid_t last_ids_default[DEFAULT_MAX_LWPS + 1];
00182 static lwpid_t *last_ids = last_ids_default;
00183 
00184 
00185 #define ROUNDUP(n)    WORDS_TO_BYTES(ROUNDED_UP_WORDS(n))
00186 
00187 static void set_max_lwps(GC_word n)
00188 {
00189     char *mem;
00190     char *oldmem;
00191     int required_bytes = ROUNDUP(n * sizeof(struct lwp_cache_entry))
00192        + ROUNDUP(n * sizeof(prgregset_t))
00193        + ROUNDUP((n + 1) * sizeof(lwpid_t))
00194        + ROUNDUP((n + 1) * sizeof(lwpid_t));
00195 
00196     GC_expand_hp_inner(divHBLKSZ((word)required_bytes));
00197     oldmem = mem = GC_scratch_alloc(required_bytes);
00198     if (0 == mem) ABORT("No space for lwp data structures");
00199 
00200     /*
00201      * We can either flush the old lwp cache or copy it over. Do the latter.
00202      */
00203     memcpy(mem, GC_lwp_cache, max_lwps * sizeof(struct lwp_cache_entry));
00204     GC_lwp_cache = (struct lwp_cache_entry*)mem;
00205     mem += ROUNDUP(n * sizeof(struct lwp_cache_entry));
00206 
00207     BZERO(GC_lwp_registers, max_lwps * sizeof(GC_lwp_registers[0]));
00208     GC_lwp_registers = (prgregset_t *)mem;
00209     mem += ROUNDUP(n * sizeof(prgregset_t));
00210 
00211 
00212     GC_current_ids = (lwpid_t *)mem;
00213     mem += ROUNDUP((n + 1) * sizeof(lwpid_t));
00214 
00215     last_ids = (lwpid_t *)mem;
00216     mem += ROUNDUP((n + 1)* sizeof(lwpid_t));
00217 
00218     if (mem > oldmem + required_bytes)
00219        ABORT("set_max_lwps buffer overflow");
00220 
00221     max_lwps = n;
00222 }
00223 
00224 
00225 /* Stop all lwps in process.  Assumes preemption is off.       */
00226 /* Caller has allocation lock (and any other locks he may      */
00227 /* need).                                               */
00228 static void stop_all_lwps()
00229 {
00230     int lwp_fd;
00231     char buf[30];
00232     prstatus_t status;
00233     register int i;
00234     GC_bool changed;
00235     lwpid_t me = _lwp_self();
00236 
00237     if (GC_main_proc_fd == -1) {
00238        sprintf(buf, "/proc/%d", getpid());
00239        GC_main_proc_fd = syscall(SYS_open, buf, O_RDONLY);
00240         if (GC_main_proc_fd < 0) {
00241               if (errno == EMFILE)
00242                      ABORT("/proc open failed: too many open files");
00243               GC_printf1("/proc open failed: errno %d", errno);
00244               abort();
00245         }
00246     }
00247     BZERO(GC_lwp_registers, sizeof (prgregset_t) * max_lwps);
00248     for (i = 0; i < max_lwps; i++)
00249        last_ids[i] = 0;
00250     for (;;) {
00251         if (syscall(SYS_ioctl, GC_main_proc_fd, PIOCSTATUS, &status) < 0)
00252            ABORT("Main PIOCSTATUS failed");
00253        if (status.pr_nlwp < 1)
00254               ABORT("Invalid number of lwps returned by PIOCSTATUS");
00255        if (status.pr_nlwp >= max_lwps) {
00256               set_max_lwps(status.pr_nlwp*2 + 10);
00257               /*
00258                * The data in the old GC_current_ids and
00259                * GC_lwp_registers has been trashed. Cleaning out last_ids
00260                * will make sure every LWP gets re-examined.
00261                */
00262               for (i = 0; i < max_lwps; i++)
00263                      last_ids[i] = 0;
00264               continue;
00265        }
00266         if (syscall(SYS_ioctl, GC_main_proc_fd, PIOCLWPIDS, GC_current_ids) < 0)
00267             ABORT("PIOCLWPIDS failed");
00268         changed = FALSE;
00269         for (i = 0; GC_current_ids[i] != 0 && i < max_lwps; i++) {
00270             if (GC_current_ids[i] != last_ids[i]) {
00271                 changed = TRUE;
00272                 if (GC_current_ids[i] != me) {
00273                   /* PIOCSTOP doesn't work without a writable         */
00274                   /* descriptor.  And that makes the process          */
00275                   /* undebuggable.                             */
00276                     if (_lwp_suspend(GC_current_ids[i]) < 0) {
00277                         /* Could happen if the lwp exited */
00278                         uncache_lwp(GC_current_ids[i]);
00279                         GC_current_ids[i] = me; /* ignore */
00280                     }
00281                 }
00282             }
00283         }
00284         /*
00285          * In the unlikely event something does a fork between the
00286         * PIOCSTATUS and the PIOCLWPIDS. 
00287          */
00288         if (i >= max_lwps)
00289               continue;
00290         /* All lwps in GC_current_ids != me have been suspended.  Note       */
00291         /* that _lwp_suspend is idempotent.                           */
00292         for (i = 0; GC_current_ids[i] != 0; i++) {
00293             if (GC_current_ids[i] != last_ids[i]) {
00294                 if (GC_current_ids[i] != me) {
00295                     lwp_fd = open_lwp(GC_current_ids[i]);
00296                   if (lwp_fd == -1)
00297                   {
00298                          GC_current_ids[i] = me;
00299                          continue;
00300                   }
00301                   /* LWP should be stopped.  Empirically it sometimes */
00302                   /* isn't, and more frequently the PR_STOPPED flag   */
00303                   /* is not set.  Wait for PR_STOPPED.         */
00304                     if (syscall(SYS_ioctl, lwp_fd,
00305                                 PIOCSTATUS, &status) < 0) {
00306                      /* Possible if the descriptor was stale, or */
00307                      /* we encountered the 2.3 _lwp_suspend bug. */
00308                      uncache_lwp(GC_current_ids[i]);
00309                         GC_current_ids[i] = me; /* handle next time. */
00310                     } else {
00311                         while (!(status.pr_flags & PR_STOPPED)) {
00312                             GC_msec_sleep(1);
00313                          if (syscall(SYS_ioctl, lwp_fd,
00314                                    PIOCSTATUS, &status) < 0) {
00315                                    ABORT("Repeated PIOCSTATUS failed");
00316                          }
00317                          if (status.pr_flags & PR_STOPPED) break;
00318                          
00319                          GC_msec_sleep(20);
00320                          if (syscall(SYS_ioctl, lwp_fd,
00321                                    PIOCSTATUS, &status) < 0) {
00322                                    ABORT("Repeated PIOCSTATUS failed");
00323                          }
00324                         }
00325                         if (status.pr_who !=  GC_current_ids[i]) {
00326                             /* can happen if thread was on death row */
00327                             uncache_lwp(GC_current_ids[i]);
00328                             GC_current_ids[i] = me; /* handle next time. */
00329                             continue;     
00330                         }
00331                         /* Save registers where collector can */
00332                      /* find them.                 */
00333                          BCOPY(status.pr_reg, GC_lwp_registers[i],
00334                               sizeof (prgregset_t));
00335                     }
00336                 }
00337             }
00338         }
00339         if (!changed) break;
00340         for (i = 0; i < max_lwps; i++) last_ids[i] = GC_current_ids[i];
00341     }
00342 }
00343 
00344 /* Restart all lwps in process.  Assumes preemption is off.    */
00345 static void restart_all_lwps()
00346 {
00347     int lwp_fd;
00348     register int i;
00349     GC_bool changed;
00350     lwpid_t me = _lwp_self();
00351 #   define PARANOID
00352 
00353     for (i = 0; GC_current_ids[i] != 0; i++) {
00354 #      ifdef PARANOID
00355          if (GC_current_ids[i] != me) {
00356            int lwp_fd = open_lwp(GC_current_ids[i]);
00357            prstatus_t status;
00358            
00359            if (lwp_fd < 0) ABORT("open_lwp failed");
00360            if (syscall(SYS_ioctl, lwp_fd,
00361                      PIOCSTATUS, &status) < 0) {
00362                 ABORT("PIOCSTATUS failed in restart_all_lwps");
00363            }
00364            if (memcmp(status.pr_reg, GC_lwp_registers[i],
00365                      sizeof (prgregset_t)) != 0) {
00366                   int j;
00367 
00368                   for(j = 0; j < NPRGREG; j++)
00369                   {
00370                          GC_printf3("%i: %x -> %x\n", j,
00371                                    GC_lwp_registers[i][j],
00372                                    status.pr_reg[j]);
00373                   }
00374               ABORT("Register contents changed");
00375            }
00376            if (!status.pr_flags & PR_STOPPED) {
00377               ABORT("lwp no longer stopped");
00378            }
00379 #ifdef SPARC
00380            {
00381                   gwindows_t windows;
00382              if (syscall(SYS_ioctl, lwp_fd,
00383                      PIOCGWIN, &windows) < 0) {
00384                 ABORT("PIOCSTATUS failed in restart_all_lwps");
00385              }
00386              if (windows.wbcnt > 0) ABORT("unsaved register windows");
00387            }
00388 #endif
00389          }
00390 #      endif /* PARANOID */
00391        if (GC_current_ids[i] == me) continue;
00392         if (_lwp_continue(GC_current_ids[i]) < 0) {
00393             ABORT("Failed to restart lwp");
00394         }
00395     }
00396     if (i >= max_lwps) ABORT("Too many lwps");
00397 }
00398 
00399 GC_bool GC_multithreaded = 0;
00400 
00401 void GC_stop_world()
00402 {
00403     preempt_off();
00404     if (GC_multithreaded)
00405         stop_all_lwps();
00406 }
00407 
00408 void GC_start_world()
00409 {
00410     if (GC_multithreaded)
00411         restart_all_lwps();
00412     preempt_on();
00413 }
00414 
00415 void GC_thr_init(void);
00416 
00417 GC_bool GC_thr_initialized = FALSE;
00418 
00419 size_t GC_min_stack_sz;
00420 
00421 
00422 /*
00423  * stack_head is stored at the top of free stacks
00424  */
00425 struct stack_head {
00426        struct stack_head    *next;
00427        ptr_t                base;
00428        thread_t             owner;
00429 };
00430 
00431 # define N_FREE_LISTS 25
00432 struct stack_head *GC_stack_free_lists[N_FREE_LISTS] = { 0 };
00433               /* GC_stack_free_lists[i] is free list for stacks of    */
00434               /* size GC_min_stack_sz*2**i.                           */
00435               /* Free lists are linked through stack_head stored      */                   /* at top of stack.                              */
00436 
00437 /* Return a stack of size at least *stack_size.  *stack_size is       */
00438 /* replaced by the actual stack size.                          */
00439 /* Caller holds allocation lock.                        */
00440 ptr_t GC_stack_alloc(size_t * stack_size)
00441 {
00442     register size_t requested_sz = *stack_size;
00443     register size_t search_sz = GC_min_stack_sz;
00444     register int index = 0; /* = log2(search_sz/GC_min_stack_sz) */
00445     register ptr_t base;
00446     register struct stack_head *result;
00447     
00448     while (search_sz < requested_sz) {
00449         search_sz *= 2;
00450         index++;
00451     }
00452     if ((result = GC_stack_free_lists[index]) == 0
00453         && (result = GC_stack_free_lists[index+1]) != 0) {
00454         /* Try next size up. */
00455         search_sz *= 2; index++;
00456     }
00457     if (result != 0) {
00458         base =  GC_stack_free_lists[index]->base;
00459         GC_stack_free_lists[index] = GC_stack_free_lists[index]->next;
00460     } else {
00461 #ifdef MMAP_STACKS
00462         base = (ptr_t)mmap(0, search_sz + GC_page_size,
00463                           PROT_READ|PROT_WRITE, MAP_PRIVATE |MAP_NORESERVE,
00464                           GC_zfd, 0);
00465        if (base == (ptr_t)-1)
00466        {
00467               *stack_size = 0;
00468               return NULL;
00469        }
00470 
00471        mprotect(base, GC_page_size, PROT_NONE);
00472        /* Should this use divHBLKSZ(search_sz + GC_page_size) ? -- cf */
00473        GC_is_fresh((struct hblk *)base, divHBLKSZ(search_sz));
00474        base += GC_page_size;
00475 
00476 #else
00477         base = (ptr_t) GC_scratch_alloc(search_sz + 2*GC_page_size);
00478        if (base == NULL)
00479        {
00480               *stack_size = 0;
00481               return NULL;
00482        }
00483 
00484         base = (ptr_t)(((word)base + GC_page_size) & ~(GC_page_size - 1));
00485         /* Protect hottest page to detect overflow. */
00486 #      ifdef SOLARIS23_MPROTECT_BUG_FIXED
00487             mprotect(base, GC_page_size, PROT_NONE);
00488 #      endif
00489         GC_is_fresh((struct hblk *)base, divHBLKSZ(search_sz));
00490 
00491         base += GC_page_size;
00492 #endif
00493     }
00494     *stack_size = search_sz;
00495     return(base);
00496 }
00497 
00498 /* Caller holds  allocationlock.                               */
00499 void GC_stack_free(ptr_t stack, size_t size)
00500 {
00501     register int index = 0;
00502     register size_t search_sz = GC_min_stack_sz;
00503     register struct stack_head *head;
00504     
00505 #ifdef MMAP_STACKS
00506     /* Zero pointers */
00507     mmap(stack, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_NORESERVE|MAP_FIXED,
00508         GC_zfd, 0);
00509 #endif
00510     while (search_sz < size) {
00511         search_sz *= 2;
00512         index++;
00513     }
00514     if (search_sz != size) ABORT("Bad stack size");
00515 
00516     head = (struct stack_head *)(stack + search_sz - sizeof(struct stack_head));
00517     head->next = GC_stack_free_lists[index];
00518     head->base = stack;
00519     GC_stack_free_lists[index] = head;
00520 }
00521 
00522 void GC_my_stack_limits();
00523 
00524 /* Notify virtual dirty bit implementation that known empty parts of  */
00525 /* stacks do not contain useful data.                                 */ 
00526 /* Caller holds allocation lock.                               */
00527 void GC_old_stacks_are_fresh()
00528 {
00529 /* No point in doing this for MMAP stacks - and pointers are zero'd out */
00530 /* by the mmap in GC_stack_free */
00531 #ifndef MMAP_STACKS
00532     register int i;
00533     register struct stack_head *s;
00534     register ptr_t p;
00535     register size_t sz;
00536     register struct hblk * h;
00537     int dummy;
00538     
00539     for (i = 0, sz= GC_min_stack_sz; i < N_FREE_LISTS;
00540          i++, sz *= 2) {
00541          for (s = GC_stack_free_lists[i]; s != 0; s = s->next) {
00542              p = s->base;
00543              h = (struct hblk *)(((word)p + HBLKSIZE-1) & ~(HBLKSIZE-1));
00544              if ((ptr_t)h == p) {
00545                  GC_is_fresh((struct hblk *)p, divHBLKSZ(sz));
00546              } else {
00547                  GC_is_fresh((struct hblk *)p, divHBLKSZ(sz) - 1);
00548                  BZERO(p, (ptr_t)h - p);
00549              }
00550          }
00551     }
00552 #endif /* MMAP_STACKS */
00553     GC_my_stack_limits();
00554 }
00555 
00556 /* The set of all known threads.  We intercept thread creation and    */
00557 /* joins.  We never actually create detached threads.  We allocate all       */
00558 /* new thread stacks ourselves.  These allow us to maintain this      */
00559 /* data structure.                                             */
00560 
00561 # define THREAD_TABLE_SZ 128       /* Must be power of 2       */
00562 volatile GC_thread GC_threads[THREAD_TABLE_SZ];
00563 
00564 void GC_push_thread_structures GC_PROTO((void))
00565 {
00566     GC_push_all((ptr_t)(GC_threads), (ptr_t)(GC_threads)+sizeof(GC_threads));
00567 }
00568 
00569 /* Add a thread to GC_threads.  We assume it wasn't already there.    */
00570 /* Caller holds allocation lock.                               */
00571 GC_thread GC_new_thread(thread_t id)
00572 {
00573     int hv = ((word)id) % THREAD_TABLE_SZ;
00574     GC_thread result;
00575     static struct GC_Thread_Rep first_thread;
00576     static GC_bool first_thread_used = FALSE;
00577     
00578     if (!first_thread_used) {
00579        result = &first_thread;
00580        first_thread_used = TRUE;
00581        /* Dont acquire allocation lock, since we may already hold it. */
00582     } else {
00583         result = (struct GC_Thread_Rep *)
00584                GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL);
00585     }
00586     if (result == 0) return(0);
00587     result -> id = id;
00588     result -> next = GC_threads[hv];
00589     GC_threads[hv] = result;
00590     /* result -> finished = 0; */
00591     (void) cond_init(&(result->join_cv), USYNC_THREAD, 0);
00592     return(result);
00593 }
00594 
00595 /* Delete a thread from GC_threads.  We assume it is there.    */
00596 /* (The code intentionally traps if it wasn't.)                */
00597 /* Caller holds allocation lock.                        */
00598 void GC_delete_thread(thread_t id)
00599 {
00600     int hv = ((word)id) % THREAD_TABLE_SZ;
00601     register GC_thread p = GC_threads[hv];
00602     register GC_thread prev = 0;
00603     
00604     while (p -> id != id) {
00605         prev = p;
00606         p = p -> next;
00607     }
00608     if (prev == 0) {
00609         GC_threads[hv] = p -> next;
00610     } else {
00611         prev -> next = p -> next;
00612     }
00613 }
00614 
00615 /* Return the GC_thread correpsonding to a given thread_t.     */
00616 /* Returns 0 if it's not there.                                */
00617 /* Caller holds  allocation lock.                       */
00618 GC_thread GC_lookup_thread(thread_t id)
00619 {
00620     int hv = ((word)id) % THREAD_TABLE_SZ;
00621     register GC_thread p = GC_threads[hv];
00622     
00623     while (p != 0 && p -> id != id) p = p -> next;
00624     return(p);
00625 }
00626 
00627 /* Solaris 2/Intel uses an initial stack size limit slightly bigger than the
00628    SPARC default of 8 MB.  Account for this to warn only if the user has
00629    raised the limit beyond the default.
00630 
00631    This is identical to DFLSSIZ defined in <sys/vm_machparam.h>.  This file
00632    is installed in /usr/platform/`uname -m`/include, which is not in the
00633    default include directory list, so copy the definition here.  */
00634 #ifdef I386
00635 # define MAX_ORIG_STACK_SIZE (8 * 1024 * 1024 + ((USRSTACK) & 0x3FFFFF))
00636 #else
00637 # define MAX_ORIG_STACK_SIZE (8 * 1024 * 1024)
00638 #endif
00639 
00640 word GC_get_orig_stack_size() {
00641     struct rlimit rl;
00642     static int warned = 0;
00643     int result;
00644 
00645     if (getrlimit(RLIMIT_STACK, &rl) != 0) ABORT("getrlimit failed");
00646     result = (word)rl.rlim_cur & ~(HBLKSIZE-1);
00647     if (result > MAX_ORIG_STACK_SIZE) {
00648        if (!warned) {
00649            WARN("Large stack limit(%ld): only scanning 8 MB\n", result);
00650            warned = 1;
00651        }
00652        result = MAX_ORIG_STACK_SIZE;
00653     }
00654     return result;
00655 }
00656 
00657 /* Notify dirty bit implementation of unused parts of my stack. */
00658 /* Caller holds allocation lock.                        */
00659 void GC_my_stack_limits()
00660 {
00661     int dummy;
00662     register ptr_t hottest = (ptr_t)((word)(&dummy) & ~(HBLKSIZE-1));
00663     register GC_thread me = GC_lookup_thread(thr_self());
00664     register size_t stack_size = me -> stack_size;
00665     register ptr_t stack;
00666     
00667     if (stack_size == 0) {
00668       /* original thread */
00669         /* Empirically, what should be the stack page with lowest     */
00670         /* address is actually inaccessible.                          */
00671         stack_size = GC_get_orig_stack_size() - GC_page_size;
00672         stack = GC_stackbottom - stack_size + GC_page_size;
00673     } else {
00674         stack = me -> stack;
00675     }
00676     if (stack > hottest || stack + stack_size < hottest) {
00677        ABORT("sp out of bounds");
00678     }
00679     GC_is_fresh((struct hblk *)stack, divHBLKSZ(hottest - stack));
00680 }
00681 
00682 
00683 /* We hold allocation lock.  Should do exactly the right thing if the */
00684 /* world is stopped.  Should not fail if it isn't.                    */
00685 void GC_push_all_stacks()
00686 {
00687     register int i;
00688     register GC_thread p;
00689     register ptr_t sp = GC_approx_sp();
00690     register ptr_t bottom, top;
00691     struct rlimit rl;
00692     
00693 #   define PUSH(bottom,top) \
00694       if (GC_dirty_maintained) { \
00695        GC_push_selected((bottom), (top), GC_page_was_ever_dirty, \
00696                     GC_push_all_stack); \
00697       } else { \
00698         GC_push_all_stack((bottom), (top)); \
00699       }
00700     GC_push_all_stack((ptr_t)GC_lwp_registers,
00701                     (ptr_t)GC_lwp_registers
00702                     + max_lwps * sizeof(GC_lwp_registers[0]));
00703     for (i = 0; i < THREAD_TABLE_SZ; i++) {
00704       for (p = GC_threads[i]; p != 0; p = p -> next) {
00705         if (p -> stack_size != 0) {
00706             bottom = p -> stack;
00707             top = p -> stack + p -> stack_size;
00708         } else {
00709             /* The original stack. */
00710             bottom = GC_stackbottom - GC_get_orig_stack_size() + GC_page_size;
00711             top = GC_stackbottom;
00712         }
00713         if ((word)sp > (word)bottom && (word)sp < (word)top) bottom = sp;
00714         PUSH(bottom, top);
00715       }
00716     }
00717 }
00718 
00719 
00720 int GC_is_thread_stack(ptr_t addr)
00721 {
00722     register int i;
00723     register GC_thread p;
00724     register ptr_t bottom, top;
00725     
00726     for (i = 0; i < THREAD_TABLE_SZ; i++) {
00727       for (p = GC_threads[i]; p != 0; p = p -> next) {
00728         if (p -> stack_size != 0) {
00729             if (p -> stack <= addr &&
00730               addr < p -> stack + p -> stack_size)
00731                   return 1;
00732        }
00733       }
00734     }
00735     return 0;
00736 }
00737 
00738 /* The only thread that ever really performs a thr_join.       */
00739 void * GC_thr_daemon(void * dummy)
00740 {
00741     void *status;
00742     thread_t departed;
00743     register GC_thread t;
00744     register int i;
00745     register int result;
00746     
00747     for(;;) {
00748       start:
00749         result = thr_join((thread_t)0, &departed, &status);
00750        LOCK();
00751        if (result != 0) {
00752            /* No more threads; wait for create. */
00753            for (i = 0; i < THREAD_TABLE_SZ; i++) {
00754                for (t = GC_threads[i]; t != 0; t = t -> next) {
00755                     if (!(t -> flags & (DETACHED | FINISHED))) {
00756                       UNLOCK();
00757                       goto start; /* Thread started just before we */
00758                                      /* acquired the lock.               */
00759                     }
00760                 }
00761             }
00762             cond_wait(&GC_create_cv, &GC_allocate_ml);
00763             UNLOCK();
00764        } else {
00765            t = GC_lookup_thread(departed);
00766            GC_multithreaded--;
00767            if (!(t -> flags & CLIENT_OWNS_STACK)) {
00768               GC_stack_free(t -> stack, t -> stack_size);
00769            }
00770            if (t -> flags & DETACHED) {
00771               GC_delete_thread(departed);
00772            } else {
00773                t -> status = status;
00774               t -> flags |= FINISHED;
00775               cond_signal(&(t -> join_cv));
00776               cond_broadcast(&GC_prom_join_cv);
00777            }
00778            UNLOCK();
00779        }
00780     }
00781 }
00782 
00783 /* We hold the allocation lock, or caller ensures that 2 instances    */
00784 /* cannot be invoked concurrently.                             */
00785 void GC_thr_init(void)
00786 {
00787     GC_thread t;
00788     thread_t tid;
00789     int ret;
00790 
00791     if (GC_thr_initialized)
00792            return;
00793     GC_thr_initialized = TRUE;
00794     GC_min_stack_sz = ((thr_min_stack() + 32*1024 + HBLKSIZE-1)
00795                      & ~(HBLKSIZE - 1));
00796 #ifdef MMAP_STACKS
00797     GC_zfd = open("/dev/zero", O_RDONLY);
00798     if (GC_zfd == -1)
00799            ABORT("Can't open /dev/zero");
00800 #endif /* MMAP_STACKS */
00801     cond_init(&GC_prom_join_cv, USYNC_THREAD, 0);
00802     cond_init(&GC_create_cv, USYNC_THREAD, 0);
00803     /* Add the initial thread, so we can stop it.       */
00804       t = GC_new_thread(thr_self());
00805       t -> stack_size = 0;
00806       t -> flags = DETACHED | CLIENT_OWNS_STACK;
00807     ret = thr_create(0 /* stack */, 0 /* stack_size */, GC_thr_daemon,
00808                    0 /* arg */, THR_DETACHED | THR_DAEMON,
00809                    &tid /* thread_id */);
00810     if (ret != 0) {
00811        GC_err_printf1("Thr_create returned %ld\n", ret);
00812        ABORT("Cant fork daemon");
00813     }
00814     thr_setprio(tid, 126);
00815 }
00816 
00817 /* We acquire the allocation lock to prevent races with        */
00818 /* stopping/starting world.                             */
00819 /* This is no more correct than the underlying Solaris 2.X     */
00820 /* implementation.  Under 2.3 THIS IS BROKEN.                  */
00821 int GC_thr_suspend(thread_t target_thread)
00822 {
00823     GC_thread t;
00824     int result;
00825     
00826     LOCK();
00827     result = thr_suspend(target_thread);
00828     if (result == 0) {
00829        t = GC_lookup_thread(target_thread);
00830        if (t == 0) ABORT("thread unknown to GC");
00831         t -> flags |= SUSPNDED;
00832     }
00833     UNLOCK();
00834     return(result);
00835 }
00836 
00837 int GC_thr_continue(thread_t target_thread)
00838 {
00839     GC_thread t;
00840     int result;
00841     
00842     LOCK();
00843     result = thr_continue(target_thread);
00844     if (result == 0) {
00845        t = GC_lookup_thread(target_thread);
00846        if (t == 0) ABORT("thread unknown to GC");
00847         t -> flags &= ~SUSPNDED;
00848     }
00849     UNLOCK();
00850     return(result);
00851 }
00852 
00853 int GC_thr_join(thread_t wait_for, thread_t *departed, void **status)
00854 {
00855     register GC_thread t;
00856     int result = 0;
00857     
00858     LOCK();
00859     if (wait_for == 0) {
00860         register int i;
00861         register GC_bool thread_exists;
00862     
00863        for (;;) {
00864          thread_exists = FALSE;
00865          for (i = 0; i < THREAD_TABLE_SZ; i++) {
00866            for (t = GC_threads[i]; t != 0; t = t -> next) {
00867               if (!(t -> flags & DETACHED)) {
00868                 if (t -> flags & FINISHED) {
00869                   goto found;
00870                 }
00871                 thread_exists = TRUE;
00872               }
00873             }
00874           }
00875           if (!thread_exists) {
00876               result = ESRCH;
00877              goto out;
00878           }
00879           cond_wait(&GC_prom_join_cv, &GC_allocate_ml);
00880         }
00881     } else {
00882         t = GC_lookup_thread(wait_for);
00883        if (t == 0 || t -> flags & DETACHED) {
00884            result = ESRCH;
00885            goto out;
00886        }
00887        if (wait_for == thr_self()) {
00888            result = EDEADLK;
00889            goto out;
00890        }
00891        while (!(t -> flags & FINISHED)) {
00892             cond_wait(&(t -> join_cv), &GC_allocate_ml);
00893        }
00894        
00895     }
00896   found:
00897     if (status) *status = t -> status;
00898     if (departed) *departed = t -> id;
00899     cond_destroy(&(t -> join_cv));
00900     GC_delete_thread(t -> id);
00901   out:
00902     UNLOCK();
00903     return(result);
00904 }
00905 
00906 
00907 int
00908 GC_thr_create(void *stack_base, size_t stack_size,
00909               void *(*start_routine)(void *), void *arg, long flags,
00910               thread_t *new_thread)
00911 {
00912     int result;
00913     GC_thread t;
00914     thread_t my_new_thread;
00915     word my_flags = 0;
00916     void * stack = stack_base;
00917    
00918     LOCK();
00919     if (!GC_is_initialized) GC_init_inner();
00920     GC_multithreaded++;
00921     if (stack == 0) {
00922        if (stack_size == 0) stack_size = 1024*1024;
00923        stack = (void *)GC_stack_alloc(&stack_size);
00924        if (stack == 0) {
00925            GC_multithreaded--;
00926            UNLOCK();
00927            return(ENOMEM);
00928        }
00929     } else {
00930        my_flags |= CLIENT_OWNS_STACK;
00931     }
00932     if (flags & THR_DETACHED) my_flags |= DETACHED;
00933     if (flags & THR_SUSPENDED) my_flags |= SUSPNDED;
00934     result = thr_create(stack, stack_size, start_routine,
00935                       arg, flags & ~THR_DETACHED, &my_new_thread);
00936     if (result == 0) {
00937         t = GC_new_thread(my_new_thread);
00938         t -> flags = my_flags;
00939         if (!(my_flags & DETACHED)) cond_init(&(t -> join_cv), USYNC_THREAD, 0);
00940         t -> stack = stack;
00941         t -> stack_size = stack_size;
00942         if (new_thread != 0) *new_thread = my_new_thread;
00943         cond_signal(&GC_create_cv);
00944     } else {
00945        GC_multithreaded--;
00946         if (!(my_flags & CLIENT_OWNS_STACK)) {
00947            GC_stack_free(stack, stack_size);
00948        }
00949     }        
00950     UNLOCK();  
00951     return(result);
00952 }
00953 
00954 # else /* !GC_SOLARIS_THREADS */
00955 
00956 #ifndef LINT
00957   int GC_no_sunOS_threads;
00958 #endif
00959 #endif