]> git.saurik.com Git - apple/libc.git/blame - threads/mig_support.c
Libc-763.11.tar.gz
[apple/libc.git] / threads / mig_support.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71
A
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
e9ce8d39
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
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.
e9ce8d39
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Mach Operating System
25 * Copyright (c) 1989 Carnegie-Mellon University
26 * All rights reserved. The CMU software License Agreement specifies
27 * the terms and conditions for use and redistribution.
28 */
29/*
30 * mig_support.c - by Mary Thompson
31 *
32 * Routines to set and deallocate the mig reply port for the current thread.
33 * Called from mig-generated interfaces.
34 */
35#include <mach/mach.h>
36#include <pthread_internals.h>
37#include <pthread.h>
38
39#include "cthreads.h"
40#include "cthread_internals.h"
41
42pthread_lock_t reply_port_lock;
43extern mach_port_t _pthread_reply_port(pthread_t);
44static mach_port_t _task_reply_port = MACH_PORT_NULL;
45
46/*
47 * called in new child...
48 * clear lock to cover case where the parent had
49 * a thread holding this lock while another thread
50 * did the fork()
51 */
52void mig_fork_child()
53{
54 UNLOCK(reply_port_lock);
55}
56
57/*
58 * Called by mach_init with 0 before cthread_init is
59 * called and again with 1 at the end of cthread_init.
60 */
61void
62mig_init(init_done)
63 int init_done;
64{
65 if (init_done == 0) {
66 LOCK_INIT(reply_port_lock);
67 _task_reply_port = mach_reply_port();
68 }
69}
70
71/*
72 * Called by mig interface code whenever a reply port is needed.
73 * Tracing is masked during this call; otherwise, a call to printf()
74 * can result in a call to malloc() which eventually reenters
75 * mig_get_reply_port() and deadlocks.
76 */
1f2f436a 77mach_port_t _mig_get_reply_port()
e9ce8d39 78{
1f2f436a 79 pthread_t pself;
e9ce8d39 80
1f2f436a
A
81 pself = pthread_self();
82 if ((pself != (pthread_t)NULL) && (pself->sig == _PTHREAD_SIG)) {
83 return pself->reply_port;
84 }
85 return MACH_PORT_NULL;
e9ce8d39
A
86}
87
1f2f436a 88void _mig_set_reply_port(mach_port_t port)
e9ce8d39 89{
1f2f436a
A
90 pthread_t pself;
91 pself = pthread_self();
92
93 if ((pself != (pthread_t)NULL) && (pself->sig == _PTHREAD_SIG)) {
94 pself->reply_port = port;
95 }
e9ce8d39 96}