]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/pthread_support.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / kern / pthread_support.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995-2005 Apple Computer, Inc. All Rights Reserved */
29 /*
30 * pthread_support.c
31 */
32
33
34 #define _PTHREAD_CONDATTR_T
35 #define _PTHREAD_COND_T
36 #define _PTHREAD_MUTEXATTR_T
37 #define _PTHREAD_MUTEX_T
38 #define _PTHREAD_RWLOCKATTR_T
39 #define _PTHREAD_RWLOCK_T
40
41 #undef pthread_mutexattr_t
42 #undef pthread_mutex_t
43 #undef pthread_condattr_t
44 #undef pthread_cond_t
45 #undef pthread_rwlockattr_t
46 #undef pthread_rwlock_t
47
48 #include <sys/param.h>
49 #include <sys/resourcevar.h>
50 #include <sys/proc_internal.h>
51 #include <sys/kauth.h>
52 #include <sys/systm.h>
53 #include <sys/timeb.h>
54 #include <sys/times.h>
55 #include <sys/acct.h>
56 #include <sys/file_internal.h>
57 #include <sys/kernel.h>
58 #include <sys/wait.h>
59 #include <sys/signalvar.h>
60 #include <sys/syslog.h>
61 #include <sys/stat.h>
62 #include <sys/lock.h>
63 #include <sys/kdebug.h>
64
65 #include <sys/mount.h>
66 #include <sys/sysproto.h>
67
68 #include <sys/vm.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71
72 #include <sys/pthread_internal.h>
73
74 #define PTHREAD_SYNCH_MAX 256
75 static pthread_mutex_t * pmutex_trans_array[PTHREAD_SYNCH_MAX];
76 static pthread_cond_t * pcond_trans_array[PTHREAD_SYNCH_MAX];
77 //static pthread_rwlock_t * prwlock_trans_array[PTHREAD_SYNCH_MAX];
78
79 pthread_mutex_t *
80 pthread_id_to_mutex(int mutexid)
81 {
82 pthread_mutex_t * mtx = NULL;
83
84
85 if (mutexid >= 0 && mutexid < PTHREAD_SYNCH_MAX) {
86 pthread_list_lock();
87 mtx = pmutex_trans_array[mutexid];
88 if (mtx) {
89 MTX_LOCK(mtx->lock);
90 mtx->refcount++;
91 MTX_UNLOCK(mtx->lock);
92 }
93 pthread_list_unlock();
94 }
95 return(mtx);
96 }
97
98
99 int
100 pthread_id_mutex_add(pthread_mutex_t * mutex)
101 {
102 int i;
103
104 pthread_list_lock();
105 for(i = 1; i < PTHREAD_SYNCH_MAX; i++) {
106 if (pmutex_trans_array[i] == 0) {
107 pmutex_trans_array[i] = mutex;
108 break;
109 }
110 }
111 pthread_list_unlock();
112 if (i == PTHREAD_SYNCH_MAX)
113 return(0);
114 return(i);
115 }
116
117
118 void
119 pthread_id_mutex_remove(int mutexid)
120 {
121 pthread_list_lock();
122 if (pmutex_trans_array[mutexid]) {
123 pmutex_trans_array[mutexid] = 0;
124 }
125 pthread_list_unlock();
126 }
127
128
129 void
130 pthread_mutex_release(pthread_mutex_t * mutex)
131 {
132 MTX_LOCK(mutex->lock);
133 mutex->refcount --;
134 MTX_UNLOCK(mutex->lock);
135 }
136
137
138 pthread_cond_t *
139 pthread_id_to_cond(int condid)
140 {
141 pthread_cond_t * cond = NULL;
142
143
144 if (condid >= 0 && condid < PTHREAD_SYNCH_MAX) {
145 pthread_list_lock();
146 cond = pcond_trans_array[condid];
147 if (cond) {
148 COND_LOCK(cond->lock);
149 cond->refcount++;
150 COND_UNLOCK(cond->lock);
151 }
152 pthread_list_unlock();
153 }
154 return(cond);
155 }
156
157
158 int
159 pthread_id_cond_add(pthread_cond_t * cond)
160 {
161 int i;
162
163 pthread_list_lock();
164 for(i = 1; i < PTHREAD_SYNCH_MAX; i++) {
165 if (pcond_trans_array[i] == 0) {
166 pcond_trans_array[i] = cond;
167 break;
168 }
169 }
170 pthread_list_unlock();
171 if (i == PTHREAD_SYNCH_MAX)
172 return(0);
173 return(i);
174 }
175
176
177 void
178 pthread_id_cond_remove(int condid)
179 {
180 pthread_list_lock();
181 if (pcond_trans_array[condid]) {
182 pcond_trans_array[condid] = 0;
183 }
184 pthread_list_unlock();
185 }
186
187
188 void
189 pthread_cond_release(pthread_cond_t * cond)
190 {
191 COND_LOCK(cond->lock);
192 cond->refcount --;
193 COND_UNLOCK(cond->lock);
194 }
195