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