Back to index

plt-scheme  4.2.1
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 by Peter C. for Solaris Posix Threads.
00017  */
00018 
00019 # include "private/gc_priv.h"
00020 
00021 # if defined(GC_SOLARIS_PTHREADS)
00022 # include <pthread.h>
00023 # include <thread.h>
00024 # include <signal.h>
00025 # include <fcntl.h>
00026 # include <sys/types.h>
00027 # include <sys/mman.h>
00028 # include <sys/time.h>
00029 # include <sys/resource.h>
00030 # include <sys/stat.h>
00031 # include <sys/syscall.h>
00032 # include <sys/procfs.h>
00033 # include <sys/lwp.h>
00034 # include <sys/reg.h>
00035 # define _CLASSIC_XOPEN_TYPES
00036 # include <unistd.h>
00037 # include <errno.h>
00038 # include "private/solaris_threads.h"
00039 # include <stdio.h>
00040 
00041 #undef pthread_join
00042 #undef pthread_create
00043 
00044 pthread_cond_t GC_prom_join_cv;           /* Broadcast when any thread terminates   */
00045 pthread_cond_t GC_create_cv;              /* Signalled when a new undetached */
00046                             /* thread starts.                  */
00047                             
00048 extern GC_bool GC_multithreaded;
00049 
00050 /* We use the allocation lock to protect thread-related data structures. */
00051 
00052 /* We stop the world using /proc primitives.  This makes some  */
00053 /* minimal assumptions about the threads implementation.       */
00054 /* We don't play by the rules, since the rules make this       */
00055 /* impossible (as of Solaris 2.3).  Also note that as of       */
00056 /* Solaris 2.3 the various thread and lwp suspension           */
00057 /* primitives failed to stop threads by the time the request   */
00058 /* is completed.                                        */
00059 
00060 
00061 
00062 int GC_pthread_join(pthread_t wait_for, void **status)
00063 {
00064        return GC_thr_join((thread_t)wait_for, NULL, status);
00065 }
00066 
00067 
00068 int
00069 GC_pthread_create(pthread_t *new_thread,
00070           const pthread_attr_t *attr_in,
00071           void * (*thread_execp)(void *), void *arg)
00072 {
00073     int result;
00074     GC_thread t;
00075     pthread_t my_new_thread;
00076     pthread_attr_t  attr;
00077     word my_flags = 0;
00078     int  flag;
00079     void * stack = 0;
00080     size_t stack_size = 0;
00081     int    n;
00082     struct sched_param schedparam;
00083    
00084     (void)pthread_attr_init(&attr);
00085     if (attr_in != 0) {
00086        (void)pthread_attr_getstacksize(attr_in, &stack_size);
00087        (void)pthread_attr_getstackaddr(attr_in, &stack);
00088     }
00089 
00090     LOCK();
00091     if (!GC_is_initialized) {
00092            GC_init_inner();
00093     }
00094     GC_multithreaded++;
00095            
00096     if (stack == 0) {
00097        if (stack_size == 0)
00098               stack_size = 1048576;
00099                        /* ^-- 1 MB (this was GC_min_stack_sz, but that
00100                         * violates the pthread_create documentation which
00101                         * says the default value if none is supplied is
00102                         * 1MB) */
00103        else
00104               stack_size += thr_min_stack();
00105 
00106        stack = (void *)GC_stack_alloc(&stack_size);
00107        if (stack == 0) {
00108            GC_multithreaded--;
00109            UNLOCK();
00110            errno = ENOMEM;
00111            return -1;
00112        }
00113     } else {
00114        my_flags |= CLIENT_OWNS_STACK;
00115     }
00116     (void)pthread_attr_setstacksize(&attr, stack_size);
00117     (void)pthread_attr_setstackaddr(&attr, stack);
00118     if (attr_in != 0) {
00119        (void)pthread_attr_getscope(attr_in, &n);
00120        (void)pthread_attr_setscope(&attr, n);
00121        (void)pthread_attr_getschedparam(attr_in, &schedparam);
00122        (void)pthread_attr_setschedparam(&attr, &schedparam);
00123        (void)pthread_attr_getschedpolicy(attr_in, &n);
00124        (void)pthread_attr_setschedpolicy(&attr, n);
00125        (void)pthread_attr_getinheritsched(attr_in, &n);
00126        (void)pthread_attr_setinheritsched(&attr, n);
00127 
00128        (void)pthread_attr_getdetachstate(attr_in, &flag);
00129        if (flag == PTHREAD_CREATE_DETACHED) {
00130               my_flags |= DETACHED;
00131        }
00132        (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
00133     }
00134     /*
00135      * thr_create can call malloc(), which if redirected will
00136      * attempt to acquire the allocation lock.
00137      * Unlock here to prevent deadlock.
00138      */
00139 
00140 
00141 #if 0
00142 #ifdef I386
00143     UNLOCK();
00144 #endif
00145 #endif
00146     result = 
00147            pthread_create(&my_new_thread, &attr, thread_execp, arg);
00148 #if 0
00149 #ifdef I386
00150     LOCK();
00151 #endif
00152 #endif
00153     if (result == 0) {
00154         t = GC_new_thread(my_new_thread);
00155         t -> flags = my_flags;
00156         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
00157         t -> stack = stack;
00158         t -> stack_size = stack_size;
00159         if (new_thread != 0) *new_thread = my_new_thread;
00160         pthread_cond_signal(&GC_create_cv);
00161     } else {
00162            if (!(my_flags & CLIENT_OWNS_STACK)) {
00163                   GC_stack_free(stack, stack_size);
00164            }        
00165            GC_multithreaded--;
00166     }
00167     UNLOCK();
00168     pthread_attr_destroy(&attr);
00169     return(result);
00170 }
00171 
00172 # else
00173 
00174 #ifndef LINT
00175   int GC_no_sunOS_pthreads;
00176 #endif
00177 
00178 # endif /* GC_SOLARIS_PTHREADS */
00179