merge from trunk revisions 58933, 58935, 58936
[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 # if defined(GC_SOLARIS_PTHREADS)
20 # include "private/gc_priv.h"
21 # include <pthread.h>
22 # include <thread.h>
23 # include <signal.h>
24 # include <fcntl.h>
25 # include <sys/types.h>
26 # include <sys/mman.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
29 # include <sys/stat.h>
30 # include <sys/syscall.h>
31 # include <sys/procfs.h>
32 # include <sys/lwp.h>
33 # include <sys/reg.h>
34 # define _CLASSIC_XOPEN_TYPES
35 # include <unistd.h>
36 # include <errno.h>
37 # include "private/solaris_threads.h"
38 # include <stdio.h>
39
40 #undef pthread_join
41 #undef pthread_create
42
43 pthread_cond_t GC_prom_join_cv;         /* Broadcast when any thread terminates */
44 pthread_cond_t GC_create_cv;            /* Signalled when a new undetached      */
45                                 /* thread starts.                       */
46                                 
47 extern GC_bool GC_multithreaded;
48
49 /* We use the allocation lock to protect thread-related data structures. */
50
51 /* We stop the world using /proc primitives.  This makes some   */
52 /* minimal assumptions about the threads implementation.        */
53 /* We don't play by the rules, since the rules make this        */
54 /* impossible (as of Solaris 2.3).  Also note that as of        */
55 /* Solaris 2.3 the various thread and lwp suspension            */
56 /* primitives failed to stop threads by the time the request    */
57 /* is completed.                                                */
58
59
60
61 int GC_pthread_join(pthread_t wait_for, void **status)
62 {
63         return GC_thr_join((thread_t)wait_for, NULL, status);
64 }
65
66 int GC_pthread_detach(pthread_t thread)
67 {
68         GC_thread t;
69
70         LOCK();
71         t=GC_lookup_thread(thread);     
72         UNLOCK();
73         if (t) {
74                 LOCK();
75                 t->flags |= DETACHED;
76                 UNLOCK();
77                 return 0;
78         }
79         else
80                 return pthread_detach(thread);
81 }
82
83 int
84 GC_pthread_create(pthread_t *new_thread,
85           const pthread_attr_t *attr_in,
86           void * (*thread_execp)(void *), void *arg)
87 {
88     int result;
89     GC_thread t;
90     pthread_t my_new_thread;
91     pthread_attr_t  attr;
92     word my_flags = 0;
93     int  flag;
94     void * stack = 0;
95     size_t stack_size = 0;
96     int    n;
97     struct sched_param schedparam;
98    
99     (void)pthread_attr_init(&attr);
100     if (attr_in != 0) {
101         (void)pthread_attr_getstacksize(attr_in, &stack_size);
102         (void)pthread_attr_getstackaddr(attr_in, &stack);
103     }
104
105     LOCK();
106     if (!GC_is_initialized) {
107             GC_init_inner();
108     }
109     GC_multithreaded++;
110             
111     if (stack == 0) {
112         if (stack_size == 0)
113                 stack_size = 1048576;
114                           /* ^-- 1 MB (this was GC_min_stack_sz, but that
115                            * violates the pthread_create documentation which
116                            * says the default value if none is supplied is
117                            * 1MB) */
118         else
119                 stack_size += thr_min_stack();
120
121         stack = (void *)GC_stack_alloc(&stack_size);
122         if (stack == 0) {
123             GC_multithreaded--;
124             UNLOCK();
125             errno = ENOMEM;
126             return -1;
127         }
128     } else {
129         my_flags |= CLIENT_OWNS_STACK;
130     }
131     (void)pthread_attr_setstacksize(&attr, stack_size);
132     (void)pthread_attr_setstackaddr(&attr, stack);
133     if (attr_in != 0) {
134         (void)pthread_attr_getscope(attr_in, &n);
135         (void)pthread_attr_setscope(&attr, n);
136         (void)pthread_attr_getschedparam(attr_in, &schedparam);
137         (void)pthread_attr_setschedparam(&attr, &schedparam);
138         (void)pthread_attr_getschedpolicy(attr_in, &n);
139         (void)pthread_attr_setschedpolicy(&attr, n);
140         (void)pthread_attr_getinheritsched(attr_in, &n);
141         (void)pthread_attr_setinheritsched(&attr, n);
142
143         (void)pthread_attr_getdetachstate(attr_in, &flag);
144         if (flag == PTHREAD_CREATE_DETACHED) {
145                 my_flags |= DETACHED;
146         }
147         (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
148     }
149     /*
150      * thr_create can call malloc(), which if redirected will
151      * attempt to acquire the allocation lock.
152      * Unlock here to prevent deadlock.
153      */
154
155
156 #if 0
157 #ifdef I386
158     UNLOCK();
159 #endif
160 #endif
161     result = 
162             pthread_create(&my_new_thread, &attr, thread_execp, arg);
163 #if 0
164 #ifdef I386
165     LOCK();
166 #endif
167 #endif
168     if (result == 0) {
169         t = GC_new_thread(my_new_thread);
170         t -> flags = my_flags;
171         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
172         t -> stack = stack;
173         t -> stack_size = stack_size;
174         if (new_thread != 0) *new_thread = my_new_thread;
175         pthread_cond_signal(&GC_create_cv);
176     } else {
177             if (!(my_flags & CLIENT_OWNS_STACK)) {
178                     GC_stack_free(stack, stack_size);
179             }        
180             GC_multithreaded--;
181     }
182     UNLOCK();
183     pthread_attr_destroy(&attr);
184     return(result);
185 }
186
187 # else
188
189 #ifndef LINT
190   int GC_no_sunOS_pthreads;
191 #endif
192
193 # endif /* GC_SOLARIS_PTHREADS */
194