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 * 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.
29 * mig_support.c - by Mary Thompson
31 * Routines to set and deallocate the mig reply port for the current thread.
32 * Called from mig-generated interfaces.
34 #include <mach/mach.h>
35 #include <pthread_internals.h>
39 #include "cthread_internals.h"
41 pthread_lock_t reply_port_lock
;
42 extern mach_port_t
_pthread_reply_port(pthread_t
);
43 static mach_port_t _task_reply_port
= MACH_PORT_NULL
;
46 * called in new child...
47 * clear lock to cover case where the parent had
48 * a thread holding this lock while another thread
53 UNLOCK(reply_port_lock
);
57 * Called by mach_init with 0 before cthread_init is
58 * called and again with 1 at the end of cthread_init.
65 LOCK_INIT(reply_port_lock
);
66 _task_reply_port
= mach_reply_port();
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.
79 register cproc_t self
;
82 int d
= cthread_debug
;
86 cthread_debug
= FALSE
;
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();
93 return pself
->reply_port
;
96 if (self
== NO_CPROC
) {
100 return(_task_reply_port
);
102 if (self
->reply_port
== MACH_PORT_NULL
) {
103 self
->reply_port
= mach_reply_port();
105 #ifdef CTHREADS_DEBUG
107 #endif CTHREADS_DEBUG
108 return self
->reply_port
;
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
117 mig_dealloc_reply_port(mach_port_t migport
)
119 register cproc_t self
;
121 register mach_port_t port
;
122 #ifdef CTHREADS_DEBUG
123 int d
= cthread_debug
;
124 #endif CTHREADS_DEBUG
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
);
142 if (self
== NO_CPROC
) {
143 #ifdef CTHREADS_DEBUG
145 #endif CTHREADS_DEBUG
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
);
157 #ifdef CTHREADS_DEBUG
159 #endif CTHREADS_DEBUG
162 /*************************************************************
163 * Called by mig interfaces after each RPC.
164 * Could be called by user.
165 ***********************************************************/
169 mach_port_t reply_port
)