merge -r 60439:60440
[mono.git] / libgc / 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 by Peter C. for Solaris Posix Threads.
17  */
18
19 # include "private/gc_priv.h"
20
21 # if defined(GC_SOLARIS_PTHREADS)
22 # include <pthread.h>
23 # include <thread.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 # include <errno.h>
38 # include "private/solaris_threads.h"
39 # include <stdio.h>
40
41 #undef pthread_join
42 #undef pthread_create
43
44 pthread_cond_t GC_prom_join_cv;         /* Broadcast when any thread terminates */
45 pthread_cond_t GC_create_cv;            /* Signalled when a new undetached      */
46                                 /* thread starts.                       */
47                                 
48 extern GC_bool GC_multithreaded;
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
62 int GC_pthread_join(pthread_t wait_for, void **status)
63 {
64         return GC_thr_join((thread_t)wait_for, NULL, status);
65 }
66
67 int GC_pthread_detach(pthread_t thread)
68 {
69         GC_thread t;
70
71         LOCK();
72         t=GC_lookup_thread(thread);     
73         UNLOCK();
74         if (t) {
75                 LOCK();
76                 t->flags |= DETACHED;
77                 UNLOCK();
78                 return 0;
79         }
80         else
81                 return pthread_detach(thread);
82 }
83
84 int
85 GC_pthread_create(pthread_t *new_thread,
86           const pthread_attr_t *attr_in,
87           void * (*thread_execp)(void *), void *arg)
88 {
89     int result;
90     GC_thread t;
91     pthread_t my_new_thread;
92     pthread_attr_t  attr;
93     word my_flags = 0;
94     int  flag;
95     void * stack = 0;
96     size_t stack_size = 0;
97     int    n;
98     struct sched_param schedparam;
99    
100     (void)pthread_attr_init(&attr);
101     if (attr_in != 0) {
102         (void)pthread_attr_getstacksize(attr_in, &stack_size);
103         (void)pthread_attr_getstackaddr(attr_in, &stack);
104     }
105
106     LOCK();
107     if (!GC_is_initialized) {
108             GC_init_inner();
109     }
110     GC_multithreaded++;
111             
112     if (stack == 0) {
113         if (stack_size == 0)
114                 stack_size = 1048576;
115                           /* ^-- 1 MB (this was GC_min_stack_sz, but that
116                            * violates the pthread_create documentation which
117                            * says the default value if none is supplied is
118                            * 1MB) */
119         else
120                 stack_size += thr_min_stack();
121
122         stack = (void *)GC_stack_alloc(&stack_size);
123         if (stack == 0) {
124             GC_multithreaded--;
125             UNLOCK();
126             errno = ENOMEM;
127             return -1;
128         }
129     } else {
130         my_flags |= CLIENT_OWNS_STACK;
131     }
132     (void)pthread_attr_setstacksize(&attr, stack_size);
133     (void)pthread_attr_setstackaddr(&attr, stack);
134     if (attr_in != 0) {
135         (void)pthread_attr_getscope(attr_in, &n);
136         (void)pthread_attr_setscope(&attr, n);
137         (void)pthread_attr_getschedparam(attr_in, &schedparam);
138         (void)pthread_attr_setschedparam(&attr, &schedparam);
139         (void)pthread_attr_getschedpolicy(attr_in, &n);
140         (void)pthread_attr_setschedpolicy(&attr, n);
141         (void)pthread_attr_getinheritsched(attr_in, &n);
142         (void)pthread_attr_setinheritsched(&attr, n);
143
144         (void)pthread_attr_getdetachstate(attr_in, &flag);
145         if (flag == PTHREAD_CREATE_DETACHED) {
146                 my_flags |= DETACHED;
147         }
148         (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
149     }
150     /*
151      * thr_create can call malloc(), which if redirected will
152      * attempt to acquire the allocation lock.
153      * Unlock here to prevent deadlock.
154      */
155
156
157 #if 0
158 #ifdef I386
159     UNLOCK();
160 #endif
161 #endif
162     result = 
163             pthread_create(&my_new_thread, &attr, thread_execp, arg);
164 #if 0
165 #ifdef I386
166     LOCK();
167 #endif
168 #endif
169     if (result == 0) {
170         t = GC_new_thread(my_new_thread);
171         t -> flags = my_flags;
172         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
173         t -> stack = stack;
174         t -> stack_size = stack_size;
175         if (new_thread != 0) *new_thread = my_new_thread;
176         pthread_cond_signal(&GC_create_cv);
177     } else {
178             if (!(my_flags & CLIENT_OWNS_STACK)) {
179                     GC_stack_free(stack, stack_size);
180             }        
181             GC_multithreaded--;
182     }
183     UNLOCK();
184     pthread_attr_destroy(&attr);
185     return(result);
186 }
187
188 # else
189
190 #ifndef LINT
191   int GC_no_sunOS_pthreads;
192 #endif
193
194 # endif /* GC_SOLARIS_PTHREADS */
195