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 * Mach Operating System
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
32 * mig_support.c - by Mary Thompson
34 * Routines to set and deallocate the mig reply port for the current thread.
35 * Called from mig-generated interfaces.
37 #include <mach/mach.h>
38 #include <pthread_internals.h>
42 #include "cthread_internals.h"
44 pthread_lock_t reply_port_lock
;
45 extern mach_port_t
_pthread_reply_port(pthread_t
);
46 static mach_port_t _task_reply_port
= MACH_PORT_NULL
;
49 * called in new child...
50 * clear lock to cover case where the parent had
51 * a thread holding this lock while another thread
56 UNLOCK(reply_port_lock
);
60 * Called by mach_init with 0 before cthread_init is
61 * called and again with 1 at the end of cthread_init.
68 LOCK_INIT(reply_port_lock
);
69 _task_reply_port
= mach_reply_port();
74 * Called by mig interface code whenever a reply port is needed.
75 * Tracing is masked during this call; otherwise, a call to printf()
76 * can result in a call to malloc() which eventually reenters
77 * mig_get_reply_port() and deadlocks.
82 register cproc_t self
;
85 int d
= cthread_debug
;
89 cthread_debug
= FALSE
;
91 pself
= pthread_self();
92 if ((pself
!= (pthread_t
)NULL
) && (pself
->sig
== _PTHREAD_SIG
)) {
93 if (pself
->reply_port
== MACH_PORT_NULL
) {
94 pself
->reply_port
= mach_reply_port();
96 return pself
->reply_port
;
99 if (self
== NO_CPROC
) {
100 #ifdef CTHREADS_DEBUG
102 #endif CTHREADS_DEBUG
103 return(_task_reply_port
);
105 if (self
->reply_port
== MACH_PORT_NULL
) {
106 self
->reply_port
= mach_reply_port();
108 #ifdef CTHREADS_DEBUG
110 #endif CTHREADS_DEBUG
111 return self
->reply_port
;
115 * Called by mig interface code after a timeout on the reply port.
116 * May also be called by user. The new mig calls with port passed in
117 * We are ignoring this , so is osfmk cthreads code
120 mig_dealloc_reply_port(mach_port_t migport
)
122 register cproc_t self
;
124 register mach_port_t port
;
125 #ifdef CTHREADS_DEBUG
126 int d
= cthread_debug
;
127 #endif CTHREADS_DEBUG
129 #ifdef CTHREADS_DEBUG
130 cthread_debug
= FALSE
;
131 #endif CTHREADS_DEBUG
132 pself
= pthread_self();
133 if ((pself
!= (pthread_t
)NULL
) && (pself
->sig
== _PTHREAD_SIG
)) {
134 port
= pself
->reply_port
;
135 if (port
!= MACH_PORT_NULL
&& port
!= _task_reply_port
) {
136 LOCK(reply_port_lock
);
137 pself
->reply_port
= _task_reply_port
;
138 (void) mach_port_destroy(mach_task_self(), port
);
139 pself
->reply_port
= MACH_PORT_NULL
;
140 UNLOCK(reply_port_lock
);
145 if (self
== NO_CPROC
) {
146 #ifdef CTHREADS_DEBUG
148 #endif CTHREADS_DEBUG
151 ASSERT(self
!= NO_CPROC
);
152 port
= self
->reply_port
;
153 if (port
!= MACH_PORT_NULL
&& port
!= _task_reply_port
) {
154 LOCK(reply_port_lock
);
155 self
->reply_port
= _task_reply_port
;
156 (void) mach_port_destroy(mach_task_self(), port
);
157 self
->reply_port
= MACH_PORT_NULL
;
158 UNLOCK(reply_port_lock
);
160 #ifdef CTHREADS_DEBUG
162 #endif CTHREADS_DEBUG
165 /*************************************************************
166 * Called by mig interfaces after each RPC.
167 * Could be called by user.
168 ***********************************************************/
172 mach_port_t reply_port
)