2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * cthreads.c - by Eric Cooper
25 * Implementation of cthread_fork, cthread_join, cthread_exit, etc.
27 * 22-July-93 Blaine Garst
28 * fixed association of read_thread info
29 * fixed kernel cache set up of cproc info
34 #include "cthread_internals.h"
35 #include "pthread_internals.h"
39 extern void cproc_init();
40 extern thread_port_t
cproc_create();
41 extern void mig_init();
42 extern void _pthread_set_self(pthread_t
);
47 extern void mig_fork_child();
52 extern int _setjmp(jmp_buf env
);
53 extern void _longjmp(jmp_buf env
, int val
);
59 #define T_RETURNED 0x2
60 #define T_DETACHED 0x4
63 int cthread_debug
= FALSE
;
67 * Routines for supporting fork() of multi-threaded programs.
71 extern void _malloc_fork_prepare(), _malloc_fork_parent();
72 extern void _malloc_fork_child();
73 extern void _cproc_fork_child(), _stack_fork_child();
74 extern void _lu_fork_child(void);
75 extern void _pthread_fork_child(void);
77 static pthread_t psaved_self
= 0;
78 static pthread_lock_t psaved_self_global_lock
= LOCK_INITIALIZER
;
80 void _cthread_fork_prepare()
82 * Prepare cthreads library to fork() a multi-threaded program. All cthread
83 * library critical section locks are acquired before a fork() and released
84 * afterwards to insure no cthread data structure is left in an inconsistent
85 * state in the child, which comes up with only the forking thread running.
88 _spin_lock(&psaved_self_global_lock
);
89 psaved_self
= pthread_self();
90 _spin_lock(&psaved_self
->lock
);
91 _malloc_fork_prepare();
94 void _cthread_fork_parent()
96 * Called in the parent process after a fork syscall.
97 * Releases locks acquired by cthread_fork_prepare().
100 _malloc_fork_parent();
101 _spin_unlock(&psaved_self
->lock
);
102 _spin_unlock(&psaved_self_global_lock
);
106 void _cthread_fork_child()
108 * Called in the child process after a fork syscall. Releases locks acquired
109 * by cthread_fork_prepare(). Deallocates cthread data structures which
110 * described other threads in our parent. Makes this thread the main thread.
112 * The mach_init() routine must be called in the child before this routine.
115 pthread_t p
= psaved_self
;
117 _pthread_set_self(p
);
118 _spin_unlock(&psaved_self_global_lock
);
120 _malloc_fork_child();
121 p
->kernel_thread
= mach_thread_self();
122 p
->reply_port
= MACH_PORT_NULL
;
124 p
->cleanup_stack
= NULL
;
125 p
->death
= MACH_PORT_NULL
;
126 p
->joiners
= MACH_PORT_NULL
;
128 p
->detached
= _PTHREAD_CREATE_PARENT
;
129 _spin_unlock(&p
->lock
);
135 _pthread_fork_child();
138 mig_init(1); /* enable multi-threaded mig interfaces */