]> git.saurik.com Git - apple/libc.git/blame - threads.subproj/mig_support.c
Libc-166.tar.gz
[apple/libc.git] / threads.subproj / mig_support.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Mach Operating System
24 * Copyright (c) 1989 Carnegie-Mellon University
25 * All rights reserved. The CMU software License Agreement specifies
26 * the terms and conditions for use and redistribution.
27 */
28/*
29 * mig_support.c - by Mary Thompson
30 *
31 * Routines to set and deallocate the mig reply port for the current thread.
32 * Called from mig-generated interfaces.
33 */
34#include <mach/mach.h>
35#include <pthread_internals.h>
36#include <pthread.h>
37
38#include "cthreads.h"
39#include "cthread_internals.h"
40
41pthread_lock_t reply_port_lock;
42extern mach_port_t _pthread_reply_port(pthread_t);
43static mach_port_t _task_reply_port = MACH_PORT_NULL;
44
45/*
46 * called in new child...
47 * clear lock to cover case where the parent had
48 * a thread holding this lock while another thread
49 * did the fork()
50 */
51void mig_fork_child()
52{
53 UNLOCK(reply_port_lock);
54}
55
56/*
57 * Called by mach_init with 0 before cthread_init is
58 * called and again with 1 at the end of cthread_init.
59 */
60void
61mig_init(init_done)
62 int init_done;
63{
64 if (init_done == 0) {
65 LOCK_INIT(reply_port_lock);
66 _task_reply_port = mach_reply_port();
67 }
68}
69
70/*
71 * Called by mig interface code whenever a reply port is needed.
72 * Tracing is masked during this call; otherwise, a call to printf()
73 * can result in a call to malloc() which eventually reenters
74 * mig_get_reply_port() and deadlocks.
75 */
76mach_port_t
77mig_get_reply_port()
78{
79 register cproc_t self;
80 pthread_t pself;
81#ifdef CTHREADS_DEBUG
82 int d = cthread_debug;
83#endif CTHREADS_DEBUG
84
85#ifdef CTHREADS_DEBUG
86 cthread_debug = FALSE;
87#endif CTHREADS_DEBUG
88 pself = pthread_self();
89 if ((pself != (pthread_t)NULL) && (pself->sig == _PTHREAD_SIG)) {
90 if (pself->reply_port == MACH_PORT_NULL) {
91 pself->reply_port = mach_reply_port();
92 }
93 return pself->reply_port;
94 }
95 self = cproc_self();
96 if (self == NO_CPROC) {
97#ifdef CTHREADS_DEBUG
98 cthread_debug = d;
99#endif CTHREADS_DEBUG
100 return(_task_reply_port);
101 }
102 if (self->reply_port == MACH_PORT_NULL) {
103 self->reply_port = mach_reply_port();
104 }
105#ifdef CTHREADS_DEBUG
106 cthread_debug = d;
107#endif CTHREADS_DEBUG
108 return self->reply_port;
109}
110
111/*
112 * Called by mig interface code after a timeout on the reply port.
113 * May also be called by user. The new mig calls with port passed in
114 * We are ignoring this , so is osfmk cthreads code
115 */
116void
117mig_dealloc_reply_port(mach_port_t migport)
118{
119 register cproc_t self;
120 pthread_t pself;
121 register mach_port_t port;
122#ifdef CTHREADS_DEBUG
123 int d = cthread_debug;
124#endif CTHREADS_DEBUG
125
126#ifdef CTHREADS_DEBUG
127 cthread_debug = FALSE;
128#endif CTHREADS_DEBUG
129 pself = pthread_self();
130 if ((pself != (pthread_t)NULL) && (pself->sig == _PTHREAD_SIG)) {
131 port = pself->reply_port;
132 if (port != MACH_PORT_NULL && port != _task_reply_port) {
133 LOCK(reply_port_lock);
134 pself->reply_port = _task_reply_port;
135 (void) mach_port_destroy(mach_task_self(), port);
136 pself->reply_port = MACH_PORT_NULL;
137 UNLOCK(reply_port_lock);
138 }
139 return;
140 }
141 self = cproc_self();
142 if (self == NO_CPROC) {
143#ifdef CTHREADS_DEBUG
144 cthread_debug = d;
145#endif CTHREADS_DEBUG
146 return;
147 }
148 ASSERT(self != NO_CPROC);
149 port = self->reply_port;
150 if (port != MACH_PORT_NULL && port != _task_reply_port) {
151 LOCK(reply_port_lock);
152 self->reply_port = _task_reply_port;
153 (void) mach_port_destroy(mach_task_self(), port);
154 self->reply_port = MACH_PORT_NULL;
155 UNLOCK(reply_port_lock);
156 }
157#ifdef CTHREADS_DEBUG
158 cthread_debug = d;
159#endif CTHREADS_DEBUG
160}
161
162/*************************************************************
163 * Called by mig interfaces after each RPC.
164 * Could be called by user.
165 ***********************************************************/
166
167void
168mig_put_reply_port(
169 mach_port_t reply_port)
170{
171}