Back to index

lightning-sunbird  0.9+nobinonly
solaris_pthreads.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  * Modified Peter C. for Solaris Posix Threads.
00017  */
00018 /* Boehm, September 14, 1994 4:44 pm PDT */
00019 /* $Id: solaris_pthreads.c,v 1.1 1999/09/30 02:23:10 beard%netscape.com Exp $ */
00020 
00021 # if defined(_SOLARIS_PTHREADS)
00022 # include "gc_priv.h"
00023 # include <pthread.h>
00024 # include <thread.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 # include "solaris_threads.h"
00040 # include <stdio.h>
00041 
00042 #undef pthread_join
00043 #undef pthread_create
00044 
00045 pthread_cond_t GC_prom_join_cv;           /* Broadcast when any thread terminates   */
00046 pthread_cond_t GC_create_cv;              /* Signalled when a new undetached */
00047                             /* thread starts.                  */
00048                             
00049 extern GC_bool GC_multithreaded;
00050 
00051 /* We use the allocation lock to protect thread-related data structures. */
00052 
00053 /* We stop the world using /proc primitives.  This makes some  */
00054 /* minimal assumptions about the threads implementation.       */
00055 /* We don't play by the rules, since the rules make this       */
00056 /* impossible (as of Solaris 2.3).  Also note that as of       */
00057 /* Solaris 2.3 the various thread and lwp suspension           */
00058 /* primitives failed to stop threads by the time the request   */
00059 /* is completed.                                        */
00060 
00061 
00062 
00063 int GC_pthread_join(pthread_t wait_for, void **status)
00064 {
00065        return GC_thr_join((thread_t)wait_for, NULL, status);
00066 }
00067 
00068 
00069 int
00070 GC_pthread_create(pthread_t *new_thread,
00071           const pthread_attr_t *attr_in,
00072           void * (*thread_execp)(void *), void *arg)
00073 {
00074     int result;
00075     GC_thread t;
00076     pthread_t my_new_thread;
00077     pthread_attr_t  attr;
00078     word my_flags = 0;
00079     int  flag;
00080     void * stack;
00081     size_t stack_size;
00082     int    n;
00083     struct sched_param schedparam;
00084    
00085     (void)pthread_attr_getstacksize(attr_in, &stack_size);
00086     (void)pthread_attr_getstackaddr(attr_in, &stack);
00087     (void)pthread_attr_init(&attr);
00088 
00089     LOCK();
00090     if (!GC_thr_initialized) {
00091            GC_thr_init();
00092     }
00093     GC_multithreaded++;
00094            
00095     if (stack == 0) {
00096        if (stack_size == 0)
00097               stack_size = GC_min_stack_sz;
00098        else
00099               stack_size += thr_min_stack();
00100 
00101        stack = (void *)GC_stack_alloc(&stack_size);
00102        if (stack == 0) {
00103            GC_multithreaded--;
00104            UNLOCK();
00105            errno = ENOMEM;
00106            return -1;
00107        }
00108     } else {
00109        my_flags |= CLIENT_OWNS_STACK;
00110     }
00111     (void)pthread_attr_setstacksize(&attr, stack_size);
00112     (void)pthread_attr_setstackaddr(&attr, stack);
00113     (void)pthread_attr_getscope(attr_in, &n);
00114     (void)pthread_attr_setscope(&attr, n);
00115     (void)pthread_attr_getschedparam(attr_in, &schedparam);
00116     (void)pthread_attr_setschedparam(&attr, &schedparam);
00117     (void)pthread_attr_getschedpolicy(attr_in, &n);
00118     (void)pthread_attr_setschedpolicy(&attr, n);
00119     (void)pthread_attr_getinheritsched(attr_in, &n);
00120     (void)pthread_attr_setinheritsched(&attr, n);
00121 
00122     (void)pthread_attr_getdetachstate(attr_in, &flag);
00123     if (flag == PTHREAD_CREATE_DETACHED) {
00124            my_flags |= DETACHED;
00125     }
00126     (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
00127     /*
00128      * thr_create can call malloc(), which if redirected will
00129      * attempt to acquire the allocation lock.
00130      * Unlock here to prevent deadlock.
00131      */
00132 
00133 
00134 #if 0
00135 #ifdef I386
00136     UNLOCK();
00137 #endif
00138 #endif
00139     result = 
00140            pthread_create(&my_new_thread, &attr, thread_execp, arg);
00141 #if 0
00142 #ifdef I386
00143     LOCK();
00144 #endif
00145 #endif
00146     if (result == 0) {
00147         t = GC_new_thread(my_new_thread);
00148         t -> flags = my_flags;
00149         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
00150         t -> stack = stack;
00151         t -> stack_size = stack_size;
00152         if (new_thread != 0) *new_thread = my_new_thread;
00153         pthread_cond_signal(&GC_create_cv);
00154     } else {
00155            if (!(my_flags & CLIENT_OWNS_STACK)) {
00156                   GC_stack_free(stack, stack_size);
00157            }        
00158            GC_multithreaded--;
00159     }
00160     UNLOCK();
00161     pthread_attr_destroy(&attr);
00162     return(result);
00163 }
00164 
00165 # else
00166 
00167 #ifndef LINT
00168   int GC_no_sunOS_pthreads;
00169 #endif
00170 
00171 # endif /* SOLARIS_THREADS */
00172