OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / boehm-gc / solaris_pthreads.c
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  * Modified Peter C. for Solaris Posix Threads.
17  */
18 /* Boehm, September 14, 1994 4:44 pm PDT */
19 /* $Id: solaris_pthreads.c,v 1.1 1999/04/07 14:56:06 tromey Exp $ */
20
21 # if defined(_SOLARIS_PTHREADS)
22 # include "gc_priv.h"
23 # include <pthread.h>
24 # include <thread.h>
25 # include <signal.h>
26 # include <fcntl.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>
34 # include <sys/lwp.h>
35 # include <sys/reg.h>
36 # define _CLASSIC_XOPEN_TYPES
37 # include <unistd.h>
38 # include <errno.h>
39 # include "solaris_threads.h"
40 # include <stdio.h>
41
42 #undef pthread_join
43 #undef pthread_create
44
45 pthread_cond_t GC_prom_join_cv;         /* Broadcast when any thread terminates */
46 pthread_cond_t GC_create_cv;            /* Signalled when a new undetached      */
47                                 /* thread starts.                       */
48                                 
49 extern bool GC_multithreaded;
50
51 /* We use the allocation lock to protect thread-related data structures. */
52
53 /* We stop the world using /proc primitives.  This makes some   */
54 /* minimal assumptions about the threads implementation.        */
55 /* We don't play by the rules, since the rules make this        */
56 /* impossible (as of Solaris 2.3).  Also note that as of        */
57 /* Solaris 2.3 the various thread and lwp suspension            */
58 /* primitives failed to stop threads by the time the request    */
59 /* is completed.                                                */
60
61
62
63 int GC_pthread_join(pthread_t wait_for, void **status)
64 {
65         return GC_thr_join((thread_t)wait_for, NULL, status);
66 }
67
68
69 int
70 GC_pthread_create(pthread_t *new_thread,
71           const pthread_attr_t *attr_in,
72           void * (*thread_execp)(void *), void *arg)
73 {
74     int result;
75     GC_thread t;
76     pthread_t my_new_thread;
77     pthread_attr_t  attr;
78     word my_flags = 0;
79     int  flag;
80     void * stack;
81     size_t stack_size;
82     int    n;
83     struct sched_param schedparam;
84    
85     (void)pthread_attr_getstacksize(attr_in, &stack_size);
86     (void)pthread_attr_getstackaddr(attr_in, &stack);
87     (void)pthread_attr_init(&attr);
88
89     LOCK();
90     if (!GC_thr_initialized) {
91             GC_thr_init();
92     }
93     GC_multithreaded++;
94             
95     if (stack == 0) {
96         if (stack_size == 0)
97                 stack_size = GC_min_stack_sz;
98         else
99                 stack_size += thr_min_stack();
100
101         stack = (void *)GC_stack_alloc(&stack_size);
102         if (stack == 0) {
103             GC_multithreaded--;
104             UNLOCK();
105             errno = ENOMEM;
106             return -1;
107         }
108     } else {
109         my_flags |= CLIENT_OWNS_STACK;
110     }
111     (void)pthread_attr_setstacksize(&attr, stack_size);
112     (void)pthread_attr_setstackaddr(&attr, stack);
113     (void)pthread_attr_getscope(attr_in, &n);
114     (void)pthread_attr_setscope(&attr, n);
115     (void)pthread_attr_getschedparam(attr_in, &schedparam);
116     (void)pthread_attr_setschedparam(&attr, &schedparam);
117     (void)pthread_attr_getschedpolicy(attr_in, &n);
118     (void)pthread_attr_setschedpolicy(&attr, n);
119     (void)pthread_attr_getinheritsched(attr_in, &n);
120     (void)pthread_attr_setinheritsched(&attr, n);
121
122     (void)pthread_attr_getdetachstate(attr_in, &flag);
123     if (flag == PTHREAD_CREATE_DETACHED) {
124             my_flags |= DETACHED;
125     }
126     (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
127     /*
128      * thr_create can call malloc(), which if redirected will
129      * attempt to acquire the allocation lock.
130      * Unlock here to prevent deadlock.
131      */
132
133
134 #if 0
135 #ifdef I386
136     UNLOCK();
137 #endif
138 #endif
139     result = 
140             pthread_create(&my_new_thread, &attr, thread_execp, arg);
141 #if 0
142 #ifdef I386
143     LOCK();
144 #endif
145 #endif
146     if (result == 0) {
147         t = GC_new_thread(my_new_thread);
148         t -> flags = my_flags;
149         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
150         t -> stack = stack;
151         t -> stack_size = stack_size;
152         if (new_thread != 0) *new_thread = my_new_thread;
153         pthread_cond_signal(&GC_create_cv);
154     } else {
155             if (!(my_flags & CLIENT_OWNS_STACK)) {
156                     GC_stack_free(stack, stack_size);
157             }        
158             GC_multithreaded--;
159     }
160     UNLOCK();
161     pthread_attr_destroy(&attr);
162     return(result);
163 }
164
165 # else
166
167 #ifndef LINT
168   int GC_no_sunOS_pthreads;
169 #endif
170
171 # endif /* SOLARIS_THREADS */
172