Merge from vendor branch TNF:
[pkgsrc.git] / devel / mit-pthreads / patches / patch-aw
1 $NetBSD$
2
3 --- /dev/null   Sun Dec 27 17:45:54 1998
4 +++ machdep/engine-i386-netbsd-1.3.c    Mon Mar  2 19:44:34 1998
5 @@ -0,0 +1,225 @@
6 +/* ==== machdep.c ============================================================
7 + * Copyright (c) 1993, 1994 Chris Provenzano, proven@athena.mit.edu
8 + *
9 + * Copyright (c) 1993 by Chris Provenzano, proven@mit.edu
10 + * All rights reserved.
11 + *
12 + * Redistribution and use in source and binary forms, with or without
13 + * modification, are permitted provided that the following conditions
14 + * are met:
15 + * 1. Redistributions of source code must retain the above copyright
16 + *    notice, this list of conditions and the following disclaimer.
17 + * 2. Redistributions in binary form must reproduce the above copyright
18 + *    notice, this list of conditions and the following disclaimer in the
19 + *    documentation and/or other materials provided with the distribution.
20 + * 3. All advertising materials mentioning features or use of this software
21 + *    must display the following acknowledgement:
22 + *  This product includes software developed by Chris Provenzano.
23 + * 4. The name of Chris Provenzano may not be used to endorse or promote
24 + *      products derived from this software without specific prior written
25 + *      permission.
26 + *
27 + * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
28 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 + * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY
31 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 + * SUCH DAMAGE.
38 + *
39 + * Description : Machine dependent functions for NetBSD on i386
40 + *
41 + *     1.00 93/08/04 proven
42 + *      -Started coding this file.
43 + */
44 +
45 +#ifndef lint
46 +static const char rcsid[] = "$Id: patch-aw,v 1.1 1998/12/28 22:22:02 bad Exp $";
47 +#endif
48 +
49 +#include <pthread.h>
50 +#include <sys/types.h>
51 +#include <sys/socket.h>
52 +#include <stdlib.h>
53 +#include <fcntl.h>
54 +#include <stdio.h>
55 +
56 +/* ==========================================================================
57 + * machdep_save_state()
58 + */
59 +int machdep_save_state(void)
60 +{
61 +    return(_setjmp(pthread_run->machdep_data.machdep_state));
62 +}
63 +
64 +/* ==========================================================================
65 + * machdep_save_state()
66 + */
67 +int machdep_save_float_state(struct pthread * pthread)
68 +{
69 +       char * fdata = (char *)pthread->machdep_data.machdep_float_state;
70 +
71 +       __asm__ ("fsave %0"::"m" (*fdata));
72 +}
73 +
74 +/* ==========================================================================
75 + * machdep_restore_state()
76 + */
77 +void machdep_restore_state(void)
78 +{
79 +    _longjmp(pthread_run->machdep_data.machdep_state, 1);
80 +}
81 +
82 +/* ==========================================================================
83 + * machdep_restore_float_state()
84 + */
85 +int machdep_restore_float_state(void)
86 +{
87 +       char * fdata = (char *)pthread_run->machdep_data.machdep_float_state;
88 +
89 +       __asm__ ("frstor %0"::"m" (*fdata));
90 +}
91 +
92 +/* ==========================================================================
93 + * machdep_set_thread_timer()
94 + */
95 +void machdep_set_thread_timer(struct machdep_pthread *machdep_pthread)
96 +{
97 +    if (setitimer(ITIMER_VIRTUAL, &(machdep_pthread->machdep_timer), NULL)) {
98 +        PANIC();
99 +    }
100 +}
101 +
102 +/* ==========================================================================
103 + * machdep_unset_thread_timer()
104 + */
105 +void machdep_unset_thread_timer(struct machdep_pthread *machdep_pthread)
106 +{
107 +    struct itimerval zeroval = { { 0, 0 }, { 0, 0 } };
108 +       int ret;
109 +
110 +       if (machdep_pthread) {
111 +       ret = setitimer(ITIMER_VIRTUAL, &zeroval, 
112 +                 &(machdep_pthread->machdep_timer));
113 +       } else {
114 +       ret = setitimer(ITIMER_VIRTUAL, &zeroval, NULL); 
115 +    }
116 +
117 +       if (ret) {
118 +               PANIC();
119 +       }
120 +}
121 +
122 +/* ==========================================================================
123 + * machdep_pthread_cleanup()
124 + */
125 +void *machdep_pthread_cleanup(struct machdep_pthread *machdep_pthread)
126 +{
127 +    return(machdep_pthread->machdep_stack);
128 +}
129 +
130 +/* ==========================================================================
131 + * machdep_pthread_start()
132 + */
133 +void machdep_pthread_start(void)
134 +{
135 +       context_switch_done();
136 +       pthread_sched_resume();
137 +
138 +    /* Run current threads start routine with argument */
139 +    pthread_exit(pthread_run->machdep_data.start_routine
140 +      (pthread_run->machdep_data.start_argument));
141 +
142 +    /* should never reach here */
143 +    PANIC();
144 +}
145 +
146 +/* ==========================================================================
147 + * __machdep_stack_free()
148 + */
149 +void __machdep_stack_free(void * stack)
150 +{       
151 +    free(stack);
152 +}
153
154 +/* ==========================================================================
155 + * __machdep_stack_alloc()
156 + */ 
157 +void * __machdep_stack_alloc(size_t size)
158 +{   
159 +    void * stack;
160 +    
161 +    return(malloc(size));
162 +}     
163 +    
164 +/* ==========================================================================
165 + * __machdep_pthread_create()
166 + */
167 +void __machdep_pthread_create(struct machdep_pthread *machdep_pthread,
168 +  void *(* start_routine)(), void *start_argument, 
169 +  long stack_size, long nsec, long flags)
170 +{
171 +    machdep_pthread->start_routine = start_routine;
172 +    machdep_pthread->start_argument = start_argument;
173 +
174 +    machdep_pthread->machdep_timer.it_value.tv_sec = 0;
175 +    machdep_pthread->machdep_timer.it_interval.tv_sec = 0;
176 +    machdep_pthread->machdep_timer.it_interval.tv_usec = 0;
177 +    machdep_pthread->machdep_timer.it_value.tv_usec = nsec / 1000;
178 +
179 +    _setjmp(machdep_pthread->machdep_state);
180 +    /*
181 +     * Set up new stact frame so that it looks like it
182 +     * returned from a longjmp() to the beginning of
183 +     * machdep_pthread_start().
184 +     */
185 +    machdep_pthread->machdep_state[0] = (int)machdep_pthread_start;
186 +
187 +    /* Stack starts high and builds down. */
188 +    machdep_pthread->machdep_state[2] =
189 +      (int)machdep_pthread->machdep_stack + stack_size;
190 +}
191 +
192 +/* ==========================================================================
193 + * machdep_sys_creat()
194 + */
195 +machdep_sys_creat(char * path, int mode)
196 +{
197 +        return(machdep_sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
198 +}
199
200 +/* ==========================================================================
201 + * machdep_sys_wait3() 
202 + */
203 +machdep_sys_wait3(int * b, int c, int * d)
204 +{
205 +        return(machdep_sys_wait4(0, b, c, d));
206 +}
207
208 +/* ==========================================================================
209 + * machdep_sys_waitpid()
210 + */
211 +machdep_sys_waitpid(int a, int * b, int c)
212 +{
213 +        return(machdep_sys_wait4(a, b, c, NULL));
214 +}  
215 +
216 +/* ==========================================================================
217 + * machdep_sys_getdtablesize()
218 + */
219 +machdep_sys_getdtablesize()
220 +{
221 +        return(sysconf(_SC_OPEN_MAX));
222 +}  
223 +
224 +/* ==========================================================================
225 + * machdep_sys_getdirentries()
226 + */
227 +machdep_sys_getdirentries(int fd, char * buf, int len, int * seek)
228 +{
229 +        return(machdep_sys_getdents(fd, buf, len));
230 +}