]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * cthreads.c - by Eric Cooper | |
25 | * | |
26 | * Implementation of cthread_fork, cthread_join, cthread_exit, etc. | |
27 | * HISTORY | |
28 | * 22-July-93 Blaine Garst | |
29 | * fixed association of read_thread info | |
30 | * fixed kernel cache set up of cproc info | |
31 | * | |
32 | */ | |
33 | #include "pthread_internals.h" | |
34 | #include <stdlib.h> | |
35 | #include <sys/queue.h> | |
36 | #include "cthreads.h" | |
37 | #include "cthread_internals.h" | |
38 | /* | |
39 | * C Threads imports: | |
40 | */ | |
41 | extern void cproc_init(); | |
42 | extern thread_port_t cproc_create(); | |
43 | extern void mig_init(); | |
44 | extern void _pthread_set_self(pthread_t); | |
45 | ||
46 | extern void pthread_workqueue_atfork_prepare(void); | |
47 | extern void pthread_workqueue_atfork_parent(void); | |
48 | extern void pthread_workqueue_atfork_child(void); | |
49 | /* | |
50 | * Mach imports: | |
51 | */ | |
52 | extern void mig_fork_child(); | |
53 | ||
54 | /* | |
55 | * C library imports: | |
56 | */ | |
57 | extern int _setjmp(jmp_buf env); | |
58 | extern void _longjmp(jmp_buf env, int val); | |
59 | ||
60 | /* | |
61 | * Thread status bits. | |
62 | */ | |
63 | #define T_MAIN 0x1 | |
64 | #define T_RETURNED 0x2 | |
65 | #define T_DETACHED 0x4 | |
66 | ||
67 | #ifdef CTHREADS_DEBUG | |
68 | int cthread_debug = FALSE; | |
69 | #endif /* CTHREADS_DEBUG */ | |
70 | ||
71 | /* | |
72 | * Routines for supporting fork() of multi-threaded programs. | |
73 | */ | |
74 | ||
75 | ||
76 | extern void _malloc_fork_prepare(), _malloc_fork_parent(); | |
77 | extern void _malloc_fork_child(); | |
78 | extern void fork_mach_init(); | |
79 | extern void _cproc_fork_child(), _stack_fork_child(); | |
80 | extern void _asl_fork_child(void); | |
81 | extern void _pthread_fork_child(pthread_t); | |
82 | extern void _pthread_fork_child_postinit(); | |
83 | extern void _notify_fork_child(void); | |
84 | ||
85 | static pthread_t psaved_self = 0; | |
86 | static pthread_lock_t psaved_self_global_lock = LOCK_INITIALIZER; | |
87 | static pthread_lock_t pthread_atfork_lock = LOCK_INITIALIZER; | |
88 | struct pthread_atfork_entry { | |
89 | TAILQ_ENTRY(pthread_atfork_entry) qentry; | |
90 | void (*prepare)(void); | |
91 | void (*parent)(void); | |
92 | void (*child)(void); | |
93 | }; | |
94 | static TAILQ_HEAD(pthread_atfork_queue_head, pthread_atfork_entry) pthread_atfork_queue = TAILQ_HEAD_INITIALIZER(pthread_atfork_queue); | |
95 | ||
96 | int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)) | |
97 | { | |
98 | struct pthread_atfork_entry *e; | |
99 | ||
100 | e = malloc(sizeof(struct pthread_atfork_entry)); | |
101 | if (e == NULL) | |
102 | return (ENOMEM); | |
103 | ||
104 | e->prepare = prepare; | |
105 | e->parent = parent; | |
106 | e->child = child; | |
107 | ||
108 | _spin_lock(&pthread_atfork_lock); | |
109 | TAILQ_INSERT_TAIL(&pthread_atfork_queue, e, qentry); | |
110 | _spin_unlock(&pthread_atfork_lock); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | void _cthread_fork_prepare() | |
116 | /* | |
117 | * Prepare cthreads library to fork() a multi-threaded program. All cthread | |
118 | * library critical section locks are acquired before a fork() and released | |
119 | * afterwards to insure no cthread data structure is left in an inconsistent | |
120 | * state in the child, which comes up with only the forking thread running. | |
121 | */ | |
122 | { | |
123 | struct pthread_atfork_entry *e; | |
124 | ||
125 | _spin_lock(&pthread_atfork_lock); | |
126 | #ifdef TAILQ_FOREACH_REVERSE_LEGACY_ORDER | |
127 | TAILQ_FOREACH_REVERSE(e, &pthread_atfork_queue, qentry, pthread_atfork_queue_head) | |
128 | #else /* !TAILQ_FOREACH_REVERSE_LEGACY_ORDER */ | |
129 | TAILQ_FOREACH_REVERSE(e, &pthread_atfork_queue, pthread_atfork_queue_head, qentry) | |
130 | #endif /* TAILQ_FOREACH_REVERSE_LEGACY_ORDER */ | |
131 | { | |
132 | if (e->prepare != NULL) | |
133 | e->prepare(); | |
134 | } | |
135 | ||
136 | _spin_lock(&psaved_self_global_lock); | |
137 | psaved_self = pthread_self(); | |
138 | _spin_lock(&psaved_self->lock); | |
139 | _malloc_fork_prepare(); | |
140 | ||
141 | pthread_workqueue_atfork_prepare(); | |
142 | } | |
143 | ||
144 | void _cthread_fork_parent() | |
145 | /* | |
146 | * Called in the parent process after a fork syscall. | |
147 | * Releases locks acquired by cthread_fork_prepare(). | |
148 | */ | |
149 | { | |
150 | struct pthread_atfork_entry *e; | |
151 | ||
152 | _malloc_fork_parent(); | |
153 | _spin_unlock(&psaved_self->lock); | |
154 | _spin_unlock(&psaved_self_global_lock); | |
155 | ||
156 | TAILQ_FOREACH(e, &pthread_atfork_queue, qentry) { | |
157 | if (e->parent != NULL) | |
158 | e->parent(); | |
159 | } | |
160 | _spin_unlock(&pthread_atfork_lock); | |
161 | ||
162 | pthread_workqueue_atfork_parent(); | |
163 | } | |
164 | ||
165 | void _cthread_fork_child() | |
166 | /* | |
167 | * Called in the child process after a fork syscall. Releases locks acquired | |
168 | * by cthread_fork_prepare(). Deallocates cthread data structures which | |
169 | * described other threads in our parent. Makes this thread the main thread. | |
170 | */ | |
171 | { | |
172 | pthread_t p = psaved_self; | |
173 | ||
174 | _pthread_set_self(p); | |
175 | _spin_unlock(&psaved_self_global_lock); | |
176 | mig_fork_child(); | |
177 | _malloc_fork_child(); | |
178 | p->kernel_thread = mach_thread_self(); | |
179 | p->reply_port = mach_reply_port(); | |
180 | p->__cleanup_stack = NULL; | |
181 | p->death = MACH_PORT_NULL; | |
182 | p->joiner = NULL; | |
183 | p->detached |= _PTHREAD_CREATE_PARENT; | |
184 | _spin_unlock(&p->lock); | |
185 | ||
186 | _pthread_fork_child(p); | |
187 | } | |
188 | ||
189 | void _cthread_fork_child_postinit() | |
190 | { | |
191 | struct pthread_atfork_entry *e; | |
192 | ||
193 | __is_threaded = 0; | |
194 | ||
195 | _pthread_fork_child_postinit(); | |
196 | mig_init(1); /* enable multi-threaded mig interfaces */ | |
197 | ||
198 | pthread_workqueue_atfork_child(); | |
199 | ||
200 | TAILQ_FOREACH(e, &pthread_atfork_queue, qentry) { | |
201 | if (e->child != NULL) | |
202 | e->child(); | |
203 | } | |
204 | LOCK_INIT(pthread_atfork_lock); | |
205 | } |