2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * cthreads.c - by Eric Cooper
28 * Implementation of cthread_fork, cthread_join, cthread_exit, etc.
30 * 22-July-93 Blaine Garst
31 * fixed association of read_thread info
32 * fixed kernel cache set up of cproc info
37 #include "cthread_internals.h"
38 #include "pthread_internals.h"
42 extern void cproc_init();
43 extern thread_port_t
cproc_create();
44 extern void mig_init();
45 extern void _pthread_set_self(pthread_t
);
50 extern void mig_fork_child();
55 extern int _setjmp(jmp_buf env
);
56 extern void _longjmp(jmp_buf env
, int val
);
62 #define T_RETURNED 0x2
63 #define T_DETACHED 0x4
66 int cthread_debug
= FALSE
;
70 * Routines for supporting fork() of multi-threaded programs.
74 extern void _malloc_fork_prepare(), _malloc_fork_parent();
75 extern void _malloc_fork_child();
76 extern void _cproc_fork_child(), _stack_fork_child();
77 extern void _lu_fork_child(void);
78 extern void _pthread_fork_child(void);
80 static pthread_t psaved_self
= 0;
81 static pthread_lock_t psaved_self_global_lock
= LOCK_INITIALIZER
;
83 void _cthread_fork_prepare()
85 * Prepare cthreads library to fork() a multi-threaded program. All cthread
86 * library critical section locks are acquired before a fork() and released
87 * afterwards to insure no cthread data structure is left in an inconsistent
88 * state in the child, which comes up with only the forking thread running.
91 _spin_lock(&psaved_self_global_lock
);
92 psaved_self
= pthread_self();
93 _spin_lock(&psaved_self
->lock
);
94 _malloc_fork_prepare();
97 void _cthread_fork_parent()
99 * Called in the parent process after a fork syscall.
100 * Releases locks acquired by cthread_fork_prepare().
103 _malloc_fork_parent();
104 _spin_unlock(&psaved_self
->lock
);
105 _spin_unlock(&psaved_self_global_lock
);
109 void _cthread_fork_child()
111 * Called in the child process after a fork syscall. Releases locks acquired
112 * by cthread_fork_prepare(). Deallocates cthread data structures which
113 * described other threads in our parent. Makes this thread the main thread.
115 * The mach_init() routine must be called in the child before this routine.
118 pthread_t p
= psaved_self
;
120 _pthread_set_self(p
);
121 _spin_unlock(&psaved_self_global_lock
);
123 _malloc_fork_child();
124 p
->kernel_thread
= mach_thread_self();
125 p
->reply_port
= MACH_PORT_NULL
;
127 p
->cleanup_stack
= NULL
;
128 p
->death
= MACH_PORT_NULL
;
130 p
->detached
|= _PTHREAD_CREATE_PARENT
;
131 _spin_unlock(&p
->lock
);
137 _pthread_fork_child();
141 mig_init(1); /* enable multi-threaded mig interfaces */