]> git.saurik.com Git - apple/libc.git/blob - threads/mig_support.c
4c5856bf65e003c7fede4ba2d45310b65d6dc8f1
[apple/libc.git] / threads / mig_support.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
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.
30 */
31 /*
32 * mig_support.c - by Mary Thompson
33 *
34 * Routines to set and deallocate the mig reply port for the current thread.
35 * Called from mig-generated interfaces.
36 */
37 #include <mach/mach.h>
38 #include <pthread_internals.h>
39 #include <pthread.h>
40
41 #include "cthreads.h"
42 #include "cthread_internals.h"
43
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;
47
48 /*
49 * called in new child...
50 * clear lock to cover case where the parent had
51 * a thread holding this lock while another thread
52 * did the fork()
53 */
54 void mig_fork_child()
55 {
56 UNLOCK(reply_port_lock);
57 }
58
59 /*
60 * Called by mach_init with 0 before cthread_init is
61 * called and again with 1 at the end of cthread_init.
62 */
63 void
64 mig_init(init_done)
65 int init_done;
66 {
67 if (init_done == 0) {
68 LOCK_INIT(reply_port_lock);
69 _task_reply_port = mach_reply_port();
70 }
71 }
72
73 /*
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.
78 */
79 mach_port_t
80 mig_get_reply_port()
81 {
82 register cproc_t self;
83 pthread_t pself;
84 #ifdef CTHREADS_DEBUG
85 int d = cthread_debug;
86 #endif CTHREADS_DEBUG
87
88 #ifdef CTHREADS_DEBUG
89 cthread_debug = FALSE;
90 #endif CTHREADS_DEBUG
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();
95 }
96 return pself->reply_port;
97 }
98 self = cproc_self();
99 if (self == NO_CPROC) {
100 #ifdef CTHREADS_DEBUG
101 cthread_debug = d;
102 #endif CTHREADS_DEBUG
103 return(_task_reply_port);
104 }
105 if (self->reply_port == MACH_PORT_NULL) {
106 self->reply_port = mach_reply_port();
107 }
108 #ifdef CTHREADS_DEBUG
109 cthread_debug = d;
110 #endif CTHREADS_DEBUG
111 return self->reply_port;
112 }
113
114 /*
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
118 */
119 void
120 mig_dealloc_reply_port(mach_port_t migport)
121 {
122 register cproc_t self;
123 pthread_t pself;
124 register mach_port_t port;
125 #ifdef CTHREADS_DEBUG
126 int d = cthread_debug;
127 #endif CTHREADS_DEBUG
128
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);
141 }
142 return;
143 }
144 self = cproc_self();
145 if (self == NO_CPROC) {
146 #ifdef CTHREADS_DEBUG
147 cthread_debug = d;
148 #endif CTHREADS_DEBUG
149 return;
150 }
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);
159 }
160 #ifdef CTHREADS_DEBUG
161 cthread_debug = d;
162 #endif CTHREADS_DEBUG
163 }
164
165 /*************************************************************
166 * Called by mig interfaces after each RPC.
167 * Could be called by user.
168 ***********************************************************/
169
170 void
171 mig_put_reply_port(
172 mach_port_t reply_port)
173 {
174 }