2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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.
30 * mig_support.c - by Mary Thompson
32 * Routines to set and deallocate the mig reply port for the current thread.
33 * Called from mig-generated interfaces.
35 #include <mach/mach.h>
36 #include <pthread_internals.h>
40 #include "cthread_internals.h"
42 pthread_lock_t reply_port_lock
;
43 extern mach_port_t
_pthread_reply_port(pthread_t
);
44 static mach_port_t _task_reply_port
= MACH_PORT_NULL
;
47 * called in new child...
48 * clear lock to cover case where the parent had
49 * a thread holding this lock while another thread
54 UNLOCK(reply_port_lock
);
58 * Called by mach_init with 0 before cthread_init is
59 * called and again with 1 at the end of cthread_init.
66 LOCK_INIT(reply_port_lock
);
67 _task_reply_port
= mach_reply_port();
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.
80 register cproc_t self
;
83 int d
= cthread_debug
;
84 #endif /* CTHREADS_DEBUG */
87 cthread_debug
= FALSE
;
88 #endif /* CTHREADS_DEBUG */
89 pself
= pthread_self();
90 if ((pself
!= (pthread_t
)NULL
) && (pself
->sig
== _PTHREAD_SIG
)) {
91 if (pself
->reply_port
== MACH_PORT_NULL
) {
92 pself
->reply_port
= mach_reply_port();
94 return pself
->reply_port
;
97 if (self
== NO_CPROC
) {
100 #endif /* CTHREADS_DEBUG */
101 return(_task_reply_port
);
103 if (self
->reply_port
== MACH_PORT_NULL
) {
104 self
->reply_port
= mach_reply_port();
106 #ifdef CTHREADS_DEBUG
108 #endif /* CTHREADS_DEBUG */
109 return self
->reply_port
;
113 * Called by mig interface code after a timeout on the reply port.
114 * May also be called by user. The new mig calls with port passed in
115 * We are ignoring this , so is osfmk cthreads code
118 mig_dealloc_reply_port(mach_port_t migport
)
120 register cproc_t self
;
122 register mach_port_t port
;
123 #ifdef CTHREADS_DEBUG
124 int d
= cthread_debug
;
125 #endif /* CTHREADS_DEBUG */
127 #ifdef CTHREADS_DEBUG
128 cthread_debug
= FALSE
;
129 #endif /* CTHREADS_DEBUG */
130 pself
= pthread_self();
131 if ((pself
!= (pthread_t
)NULL
) && (pself
->sig
== _PTHREAD_SIG
)) {
132 port
= pself
->reply_port
;
133 if (port
!= MACH_PORT_NULL
&& port
!= _task_reply_port
) {
134 LOCK(reply_port_lock
);
135 pself
->reply_port
= _task_reply_port
;
136 (void) mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
137 pself
->reply_port
= MACH_PORT_NULL
;
138 UNLOCK(reply_port_lock
);
143 if (self
== NO_CPROC
) {
144 #ifdef CTHREADS_DEBUG
146 #endif /* CTHREADS_DEBUG */
149 ASSERT(self
!= NO_CPROC
);
150 port
= self
->reply_port
;
151 if (port
!= MACH_PORT_NULL
&& port
!= _task_reply_port
) {
152 LOCK(reply_port_lock
);
153 self
->reply_port
= _task_reply_port
;
154 (void) mach_port_mod_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, -1);
155 self
->reply_port
= MACH_PORT_NULL
;
156 UNLOCK(reply_port_lock
);
158 #ifdef CTHREADS_DEBUG
160 #endif /* CTHREADS_DEBUG */
163 /*************************************************************
164 * Called by mig interfaces after each RPC.
165 * Could be called by user.
166 ***********************************************************/
170 mach_port_t reply_port
)