2005-04-06 Zoltan Varga <vargaz@freemail.hu>
[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 = GC_lookup_thread(thread); 
69
70         if (t) {
71                 t->flags |= DETACHED;
72                 return 0;
73         }
74         else
75                 return pthread_detach(thread);
76 }
77
78 int
79 GC_pthread_create(pthread_t *new_thread,
80           const pthread_attr_t *attr_in,
81           void * (*thread_execp)(void *), void *arg)
82 {
83     int result;
84     GC_thread t;
85     pthread_t my_new_thread;
86     pthread_attr_t  attr;
87     word my_flags = 0;
88     int  flag;
89     void * stack = 0;
90     size_t stack_size = 0;
91     int    n;
92     struct sched_param schedparam;
93    
94     (void)pthread_attr_init(&attr);
95     if (attr_in != 0) {
96         (void)pthread_attr_getstacksize(attr_in, &stack_size);
97         (void)pthread_attr_getstackaddr(attr_in, &stack);
98     }
99
100     LOCK();
101     if (!GC_is_initialized) {
102             GC_init_inner();
103     }
104     GC_multithreaded++;
105             
106     if (stack == 0) {
107         if (stack_size == 0)
108                 stack_size = 1048576;
109                           /* ^-- 1 MB (this was GC_min_stack_sz, but that
110                            * violates the pthread_create documentation which
111                            * says the default value if none is supplied is
112                            * 1MB) */
113         else
114                 stack_size += thr_min_stack();
115
116         stack = (void *)GC_stack_alloc(&stack_size);
117         if (stack == 0) {
118             GC_multithreaded--;
119             UNLOCK();
120             errno = ENOMEM;
121             return -1;
122         }
123     } else {
124         my_flags |= CLIENT_OWNS_STACK;
125     }
126     (void)pthread_attr_setstacksize(&attr, stack_size);
127     (void)pthread_attr_setstackaddr(&attr, stack);
128     if (attr_in != 0) {
129         (void)pthread_attr_getscope(attr_in, &n);
130         (void)pthread_attr_setscope(&attr, n);
131         (void)pthread_attr_getschedparam(attr_in, &schedparam);
132         (void)pthread_attr_setschedparam(&attr, &schedparam);
133         (void)pthread_attr_getschedpolicy(attr_in, &n);
134         (void)pthread_attr_setschedpolicy(&attr, n);
135         (void)pthread_attr_getinheritsched(attr_in, &n);
136         (void)pthread_attr_setinheritsched(&attr, n);
137
138         (void)pthread_attr_getdetachstate(attr_in, &flag);
139         if (flag == PTHREAD_CREATE_DETACHED) {
140                 my_flags |= DETACHED;
141         }
142         (void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
143     }
144     /*
145      * thr_create can call malloc(), which if redirected will
146      * attempt to acquire the allocation lock.
147      * Unlock here to prevent deadlock.
148      */
149
150
151 #if 0
152 #ifdef I386
153     UNLOCK();
154 #endif
155 #endif
156     result = 
157             pthread_create(&my_new_thread, &attr, thread_execp, arg);
158 #if 0
159 #ifdef I386
160     LOCK();
161 #endif
162 #endif
163     if (result == 0) {
164         t = GC_new_thread(my_new_thread);
165         t -> flags = my_flags;
166         if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
167         t -> stack = stack;
168         t -> stack_size = stack_size;
169         if (new_thread != 0) *new_thread = my_new_thread;
170         pthread_cond_signal(&GC_create_cv);
171     } else {
172             if (!(my_flags & CLIENT_OWNS_STACK)) {
173                     GC_stack_free(stack, stack_size);
174             }        
175             GC_multithreaded--;
176     }
177     UNLOCK();
178     pthread_attr_destroy(&attr);
179     return(result);
180 }
181
182 # else
183
184 #ifndef LINT
185   int GC_no_sunOS_pthreads;
186 #endif
187
188 # endif /* GC_SOLARIS_PTHREADS */
189