annotate libobjc/objc-sync.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* GNU Objective C Runtime @synchronized implementation
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Nicola Pero <nicola.pero@meta-innovation.com>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under the
kono
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
kono
parents:
diff changeset
9 Foundation; either version 3, or (at your option) any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
kono
parents:
diff changeset
14 details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /* This file implements objc_sync_enter() and objc_sync_exit(), the
kono
parents:
diff changeset
26 two functions required to support @synchronized().
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 objc_sync_enter(object) needs to get a recursive lock associated
kono
parents:
diff changeset
29 with 'object', and lock it.
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 objc_sync_exit(object) needs to get the recursive lock associated
kono
parents:
diff changeset
32 with 'object', and unlock it. */
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 /* To avoid the overhead of continuously allocating and deallocating
kono
parents:
diff changeset
35 locks, we implement a pool of locks. When a lock is needed for an
kono
parents:
diff changeset
36 object, we get a lock from the pool and associate it with the
kono
parents:
diff changeset
37 object.
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 The lock pool need to be protected by its own lock (the
kono
parents:
diff changeset
40 "protection" lock), which has to be locked then unlocked each time
kono
parents:
diff changeset
41 objc_sync_enter() and objc_sync_exit() are called. To reduce the
kono
parents:
diff changeset
42 contention on the protection lock, instead of a single pool with a
kono
parents:
diff changeset
43 single (global) protection lock we use a number of smaller pools,
kono
parents:
diff changeset
44 each with its own pool protection lock. To decide which lock pool
kono
parents:
diff changeset
45 to use for each object, we compute a hash from the object pointer.
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 The implementation of each lock pool uses a linked list of all the
kono
parents:
diff changeset
48 locks in the pool (both unlocked, and locked); this works in the
kono
parents:
diff changeset
49 assumption that the number of locks concurrently required is very
kono
parents:
diff changeset
50 low. In practice, it seems that you rarely see more than a few
kono
parents:
diff changeset
51 locks ever concurrently required.
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 A standard case is a thread acquiring a lock recursively, over and
kono
parents:
diff changeset
54 over again: for example when most methods of a class are protected
kono
parents:
diff changeset
55 by @synchronized(self) but they also call each other. We use
kono
parents:
diff changeset
56 thread-local storage to implement a cache and optimize this case.
kono
parents:
diff changeset
57 The cache stores locks that the thread successfully acquired,
kono
parents:
diff changeset
58 allowing objc_sync_enter() and objc_sync_exit() to locate a lock
kono
parents:
diff changeset
59 which is already held by the current thread without having to use
kono
parents:
diff changeset
60 any protection lock or synchronization mechanism. It can so detect
kono
parents:
diff changeset
61 recursive locks/unlocks, and transform them into no-ops that
kono
parents:
diff changeset
62 require no actual locking or synchronization mechanisms at all. */
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 /* You can disable the thread-local cache (most likely to benchmark
kono
parents:
diff changeset
65 the code with and without it) by compiling with
kono
parents:
diff changeset
66 -DSYNC_CACHE_DISABLE, or commenting out the following line. */
kono
parents:
diff changeset
67 /* #define SYNC_CACHE_DISABLE */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 /* If thread-local storage is not available, automatically disable the
kono
parents:
diff changeset
70 cache. */
kono
parents:
diff changeset
71 #ifndef HAVE_TLS
kono
parents:
diff changeset
72 # define SYNC_CACHE_DISABLE
kono
parents:
diff changeset
73 #endif
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 #include "objc-private/common.h"
kono
parents:
diff changeset
76 #include "objc/objc-sync.h" /* For objc_sync_enter(), objc_sync_exit() */
kono
parents:
diff changeset
77 #include "objc/runtime.h" /* For objc_malloc() */
kono
parents:
diff changeset
78 #include "objc/thr.h" /* For objc_mutex_loc() and similar */
kono
parents:
diff changeset
79 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* We have 32 pools of locks, each of them protected by its own
kono
parents:
diff changeset
82 protection lock. It's tempting to increase this number to reduce
kono
parents:
diff changeset
83 contention; but in our tests it is high enough. */
kono
parents:
diff changeset
84 #define SYNC_NUMBER_OF_POOLS 32
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 /* Given an object, it determines which pool contains the associated
kono
parents:
diff changeset
87 lock. */
kono
parents:
diff changeset
88 #define SYNC_OBJECT_HASH(OBJECT) ((((size_t)OBJECT >> 8) ^ (size_t)OBJECT) & (SYNC_NUMBER_OF_POOLS - 1))
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 /* The locks protecting each pool. */
kono
parents:
diff changeset
91 static objc_mutex_t sync_pool_protection_locks[SYNC_NUMBER_OF_POOLS];
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 /* The data structure (linked list) holding the locks. */
kono
parents:
diff changeset
94 typedef struct lock_node
kono
parents:
diff changeset
95 {
kono
parents:
diff changeset
96 /* Pointer to next entry on the list. NULL indicates end of list.
kono
parents:
diff changeset
97 You need to hold the appropriate sync_pool_protection_locks[N] to
kono
parents:
diff changeset
98 read or write this variable. */
kono
parents:
diff changeset
99 struct lock_node *next;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /* The (recursive) lock. Allocated when the node is created, and
kono
parents:
diff changeset
102 always not-NULL, and unchangeable, after that. */
kono
parents:
diff changeset
103 objc_mutex_t lock;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* This is how many times the objc_mutex_lock() has been called on
kono
parents:
diff changeset
106 the lock (it is 0 when the lock is unused). Used to track when
kono
parents:
diff changeset
107 the lock is no longer associated with an object and can be reused
kono
parents:
diff changeset
108 for another object. It records "real" locks, potentially (but
kono
parents:
diff changeset
109 not necessarily) by multiple threads. You need to hold the
kono
parents:
diff changeset
110 appropriate sync_pool_protection_locks[N] to read or write this
kono
parents:
diff changeset
111 variable. */
kono
parents:
diff changeset
112 unsigned int usage_count;
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 /* The object that the lock is associated with. This variable can
kono
parents:
diff changeset
115 only be written when holding the sync_pool_protection_locks[N]
kono
parents:
diff changeset
116 and when node->usage_count == 0, ie, the lock is not being used.
kono
parents:
diff changeset
117 You can read this variable either when you hold the
kono
parents:
diff changeset
118 sync_pool_protection_locks[N] or when you hold node->lock,
kono
parents:
diff changeset
119 because in that case you know that node->usage_count can't get to
kono
parents:
diff changeset
120 zero until you release the lock. It is valid to have usage_count
kono
parents:
diff changeset
121 == 0 and object != nil; in that case, the lock is not currently
kono
parents:
diff changeset
122 being used, but is still currently associated with the
kono
parents:
diff changeset
123 object. */
kono
parents:
diff changeset
124 id object;
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /* This is a counter reserved for use by the thread currently
kono
parents:
diff changeset
127 holding the lock. So, you need to hold node->lock to read or
kono
parents:
diff changeset
128 write this variable. It is normally 0, and if the cache is not
kono
parents:
diff changeset
129 being used, it is kept at 0 (even if recursive locks are being
kono
parents:
diff changeset
130 done; in that case, no difference is made between recursive and
kono
parents:
diff changeset
131 non-recursive locks: they all increase usage_count, and call
kono
parents:
diff changeset
132 objc_mutex_lock()). When the cache is being used, a thread may
kono
parents:
diff changeset
133 be able to find a lock that it already holds using the cache; in
kono
parents:
diff changeset
134 that case, to perform additional locks/unlocks it can
kono
parents:
diff changeset
135 increase/decrease the recursive_usage_count (which does not
kono
parents:
diff changeset
136 require any synchronization with other threads, since it's
kono
parents:
diff changeset
137 protected by the node->lock itself) instead of the usage_count
kono
parents:
diff changeset
138 (which requires locking the pool protection lock). And it can
kono
parents:
diff changeset
139 skip the call to objc_mutex_lock/unlock too. */
kono
parents:
diff changeset
140 unsigned int recursive_usage_count;
kono
parents:
diff changeset
141 } *lock_node_ptr;
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 /* The pools of locks. Each of them is a linked list of lock_nodes.
kono
parents:
diff changeset
145 In the list we keep both unlocked and locked nodes. */
kono
parents:
diff changeset
146 static lock_node_ptr sync_pool_array[SYNC_NUMBER_OF_POOLS];
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
149 /* We store a cache of locks acquired by each thread in thread-local
kono
parents:
diff changeset
150 storage. */
kono
parents:
diff changeset
151 static __thread lock_node_ptr *lock_cache = NULL;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 /* This is a conservative implementation that uses a static array of
kono
parents:
diff changeset
154 fixed size as cache. Because the cache is an array that we scan
kono
parents:
diff changeset
155 linearly, the bigger it is, the slower it gets. This does not
kono
parents:
diff changeset
156 matter much at small sizes (eg, the overhead of checking 8 cache
kono
parents:
diff changeset
157 slots instead of 4 is very small compared to the other overheads
kono
parents:
diff changeset
158 involved such as function calls and lock/unlock operations), but at
kono
parents:
diff changeset
159 large sizes it becomes important as obviously there is a size over
kono
parents:
diff changeset
160 which using the cache backfires: the lookup is so slow that the
kono
parents:
diff changeset
161 cache slows down the software instead of speeding it up. In
kono
parents:
diff changeset
162 practice, it seems that most threads use a small number of
kono
parents:
diff changeset
163 concurrent locks, so we have a conservative implementation with a
kono
parents:
diff changeset
164 fixed-size cache of 8 locks which gives a very predictable
kono
parents:
diff changeset
165 behaviour. If a thread locks lots of different locks, only the
kono
parents:
diff changeset
166 first 8 get the speed benefits of the cache, but the cache remains
kono
parents:
diff changeset
167 always small, fast and predictable.
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 SYNC_CACHE_SIZE is the size of the lock cache for each thread. */
kono
parents:
diff changeset
170 #define SYNC_CACHE_SIZE 8
kono
parents:
diff changeset
171 #endif /* SYNC_CACHE_DISABLE */
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 /* Called at startup by init.c. */
kono
parents:
diff changeset
174 void
kono
parents:
diff changeset
175 __objc_sync_init (void)
kono
parents:
diff changeset
176 {
kono
parents:
diff changeset
177 int i;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 for (i = 0; i < SYNC_NUMBER_OF_POOLS; i++)
kono
parents:
diff changeset
180 {
kono
parents:
diff changeset
181 lock_node_ptr new_node;
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 /* Create a protection lock for each pool. */
kono
parents:
diff changeset
184 sync_pool_protection_locks[i] = objc_mutex_allocate ();
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 /* Preallocate a lock per pool. */
kono
parents:
diff changeset
187 new_node = objc_malloc (sizeof (struct lock_node));
kono
parents:
diff changeset
188 new_node->lock = objc_mutex_allocate ();
kono
parents:
diff changeset
189 new_node->object = nil;
kono
parents:
diff changeset
190 new_node->usage_count = 0;
kono
parents:
diff changeset
191 new_node->recursive_usage_count = 0;
kono
parents:
diff changeset
192 new_node->next = NULL;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 sync_pool_array[i] = new_node;
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 int
kono
parents:
diff changeset
199 objc_sync_enter (id object)
kono
parents:
diff changeset
200 {
kono
parents:
diff changeset
201 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
202 int free_cache_slot;
kono
parents:
diff changeset
203 #endif
kono
parents:
diff changeset
204 int hash;
kono
parents:
diff changeset
205 lock_node_ptr node;
kono
parents:
diff changeset
206 lock_node_ptr unused_node;
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 if (object == nil)
kono
parents:
diff changeset
209 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
212 if (lock_cache == NULL)
kono
parents:
diff changeset
213 {
kono
parents:
diff changeset
214 /* Note that this calloc only happen only once per thread, the
kono
parents:
diff changeset
215 very first time a thread does a objc_sync_enter(). */
kono
parents:
diff changeset
216 lock_cache = objc_calloc (SYNC_CACHE_SIZE, sizeof (lock_node_ptr));
kono
parents:
diff changeset
217 }
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 /* Check the cache to see if we have a record of having already
kono
parents:
diff changeset
220 locked the lock corresponding to this object. While doing so,
kono
parents:
diff changeset
221 keep track of the first free cache node in case we need it
kono
parents:
diff changeset
222 later. */
kono
parents:
diff changeset
223 node = NULL;
kono
parents:
diff changeset
224 free_cache_slot = -1;
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 {
kono
parents:
diff changeset
227 int i;
kono
parents:
diff changeset
228 for (i = 0; i < SYNC_CACHE_SIZE; i++)
kono
parents:
diff changeset
229 {
kono
parents:
diff changeset
230 lock_node_ptr locked_node = lock_cache[i];
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 if (locked_node == NULL)
kono
parents:
diff changeset
233 {
kono
parents:
diff changeset
234 if (free_cache_slot == -1)
kono
parents:
diff changeset
235 free_cache_slot = i;
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237 else if (locked_node->object == object)
kono
parents:
diff changeset
238 {
kono
parents:
diff changeset
239 node = locked_node;
kono
parents:
diff changeset
240 break;
kono
parents:
diff changeset
241 }
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243 }
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 if (node != NULL)
kono
parents:
diff changeset
246 {
kono
parents:
diff changeset
247 /* We found the lock. Increase recursive_usage_count, which is
kono
parents:
diff changeset
248 protected by node->lock, which we already hold. */
kono
parents:
diff changeset
249 node->recursive_usage_count++;
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 /* There is no need to actually lock anything, since we already
kono
parents:
diff changeset
252 hold the lock. Correspondingly, objc_sync_exit() will just
kono
parents:
diff changeset
253 decrease recursive_usage_count and do nothing to unlock. */
kono
parents:
diff changeset
254 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256 #endif /* SYNC_CACHE_DISABLE */
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 /* The following is the standard lookup for the lock in the standard
kono
parents:
diff changeset
259 pool lock. It requires a pool protection lock. */
kono
parents:
diff changeset
260 hash = SYNC_OBJECT_HASH(object);
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 /* Search for an existing lock for 'object'. While searching, make
kono
parents:
diff changeset
263 note of any unused lock if we find any. */
kono
parents:
diff changeset
264 unused_node = NULL;
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 objc_mutex_lock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 node = sync_pool_array[hash];
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 while (node != NULL)
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 if (node->object == object)
kono
parents:
diff changeset
273 {
kono
parents:
diff changeset
274 /* We found the lock. */
kono
parents:
diff changeset
275 node->usage_count++;
kono
parents:
diff changeset
276 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
279 /* Put it in the cache. */
kono
parents:
diff changeset
280 if (free_cache_slot != -1)
kono
parents:
diff changeset
281 lock_cache[free_cache_slot] = node;
kono
parents:
diff changeset
282 #endif
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 /* Lock it. */
kono
parents:
diff changeset
285 objc_mutex_lock (node->lock);
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
288 }
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 if (unused_node == NULL && node->usage_count == 0)
kono
parents:
diff changeset
291 {
kono
parents:
diff changeset
292 /* We found the first unused node. Record it. */
kono
parents:
diff changeset
293 unused_node = node;
kono
parents:
diff changeset
294 }
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 node = node->next;
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 /* An existing lock for 'object' could not be found. */
kono
parents:
diff changeset
300 if (unused_node != NULL)
kono
parents:
diff changeset
301 {
kono
parents:
diff changeset
302 /* But we found a unused lock; use it. */
kono
parents:
diff changeset
303 unused_node->object = object;
kono
parents:
diff changeset
304 unused_node->usage_count = 1;
kono
parents:
diff changeset
305 unused_node->recursive_usage_count = 0;
kono
parents:
diff changeset
306 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
309 if (free_cache_slot != -1)
kono
parents:
diff changeset
310 lock_cache[free_cache_slot] = unused_node;
kono
parents:
diff changeset
311 #endif
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 objc_mutex_lock (unused_node->lock);
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
316 }
kono
parents:
diff changeset
317 else
kono
parents:
diff changeset
318 {
kono
parents:
diff changeset
319 /* There are no unused nodes; allocate a new node. */
kono
parents:
diff changeset
320 lock_node_ptr new_node;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 /* Create the node. */
kono
parents:
diff changeset
323 new_node = objc_malloc (sizeof (struct lock_node));
kono
parents:
diff changeset
324 new_node->lock = objc_mutex_allocate ();
kono
parents:
diff changeset
325 new_node->object = object;
kono
parents:
diff changeset
326 new_node->usage_count = 1;
kono
parents:
diff changeset
327 new_node->recursive_usage_count = 0;
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 /* Attach it at the beginning of the pool. */
kono
parents:
diff changeset
330 new_node->next = sync_pool_array[hash];
kono
parents:
diff changeset
331 sync_pool_array[hash] = new_node;
kono
parents:
diff changeset
332 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
335 if (free_cache_slot != -1)
kono
parents:
diff changeset
336 lock_cache[free_cache_slot] = new_node;
kono
parents:
diff changeset
337 #endif
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 objc_mutex_lock (new_node->lock);
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
342 }
kono
parents:
diff changeset
343 }
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 int
kono
parents:
diff changeset
346 objc_sync_exit (id object)
kono
parents:
diff changeset
347 {
kono
parents:
diff changeset
348 int hash;
kono
parents:
diff changeset
349 lock_node_ptr node;
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 if (object == nil)
kono
parents:
diff changeset
352 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 #ifndef SYNC_CACHE_DISABLE
kono
parents:
diff changeset
355 if (lock_cache != NULL)
kono
parents:
diff changeset
356 {
kono
parents:
diff changeset
357 int i;
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 /* Find the lock in the cache. */
kono
parents:
diff changeset
360 node = NULL;
kono
parents:
diff changeset
361 for (i = 0; i < SYNC_CACHE_SIZE; i++)
kono
parents:
diff changeset
362 {
kono
parents:
diff changeset
363 lock_node_ptr locked_node = lock_cache[i];
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 if (locked_node != NULL && locked_node->object == object)
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 node = locked_node;
kono
parents:
diff changeset
368 break;
kono
parents:
diff changeset
369 }
kono
parents:
diff changeset
370 }
kono
parents:
diff changeset
371 /* Note that, if a node was found in the cache, the variable i
kono
parents:
diff changeset
372 now holds the index where it was found, which will be used to
kono
parents:
diff changeset
373 remove it from the cache. */
kono
parents:
diff changeset
374 if (node != NULL)
kono
parents:
diff changeset
375 {
kono
parents:
diff changeset
376 if (node->recursive_usage_count > 0)
kono
parents:
diff changeset
377 {
kono
parents:
diff changeset
378 node->recursive_usage_count--;
kono
parents:
diff changeset
379 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
380 }
kono
parents:
diff changeset
381 else
kono
parents:
diff changeset
382 {
kono
parents:
diff changeset
383 /* We need to do a real unlock. */
kono
parents:
diff changeset
384 hash = SYNC_OBJECT_HASH(object);
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 /* TODO: If we had atomic increase/decrease operations
kono
parents:
diff changeset
387 with memory barriers, we could avoid the lock
kono
parents:
diff changeset
388 here! */
kono
parents:
diff changeset
389 objc_mutex_lock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
390 node->usage_count--;
kono
parents:
diff changeset
391 /* Normally, we do not reset object to nil here. We'll
kono
parents:
diff changeset
392 leave the lock associated with that object, at zero
kono
parents:
diff changeset
393 usage count. This makes it slightly more efficient to
kono
parents:
diff changeset
394 provide a lock for that object if (as likely)
kono
parents:
diff changeset
395 requested again. If the object is deallocated, we
kono
parents:
diff changeset
396 don't care. It will never match a new lock that is
kono
parents:
diff changeset
397 requested, and the node will be reused at some point.
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 But, if garbage collection is enabled, leaving a
kono
parents:
diff changeset
400 pointer to the object in memory might prevent the
kono
parents:
diff changeset
401 object from being released. In that case, we remove
kono
parents:
diff changeset
402 it (TODO: maybe we should avoid using the garbage
kono
parents:
diff changeset
403 collector at all ? Nothing is ever deallocated in
kono
parents:
diff changeset
404 this file). */
kono
parents:
diff changeset
405 #if OBJC_WITH_GC
kono
parents:
diff changeset
406 node->object = nil;
kono
parents:
diff changeset
407 #endif
kono
parents:
diff changeset
408 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 /* PS: Between objc_mutex_unlock
kono
parents:
diff changeset
411 (sync_pool_protection_locks[hash]) and
kono
parents:
diff changeset
412 objc_mutex_unlock (node->lock), the pool is unlocked
kono
parents:
diff changeset
413 so other threads may allocate this same lock to
kono
parents:
diff changeset
414 another object (!). This is not a problem, but it is
kono
parents:
diff changeset
415 curious. */
kono
parents:
diff changeset
416 objc_mutex_unlock (node->lock);
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 /* Remove the node from the cache. */
kono
parents:
diff changeset
419 lock_cache[i] = NULL;
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
422 }
kono
parents:
diff changeset
423 }
kono
parents:
diff changeset
424 }
kono
parents:
diff changeset
425 #endif
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 /* The cache either wasn't there, or didn't work (eg, we overflowed
kono
parents:
diff changeset
428 it at some point and stopped recording new locks in the cache).
kono
parents:
diff changeset
429 Proceed with a full search of the lock pool. */
kono
parents:
diff changeset
430 hash = SYNC_OBJECT_HASH(object);
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 objc_mutex_lock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 /* Search for an existing lock for 'object'. */
kono
parents:
diff changeset
435 node = sync_pool_array[hash];
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 while (node != NULL)
kono
parents:
diff changeset
438 {
kono
parents:
diff changeset
439 if (node->object == object)
kono
parents:
diff changeset
440 {
kono
parents:
diff changeset
441 /* We found the lock. */
kono
parents:
diff changeset
442 node->usage_count--;
kono
parents:
diff changeset
443 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 objc_mutex_unlock (node->lock);
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 /* No need to remove the node from the cache, since it
kono
parents:
diff changeset
448 wasn't found in the cache when we looked for it! */
kono
parents:
diff changeset
449 return OBJC_SYNC_SUCCESS;
kono
parents:
diff changeset
450 }
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 node = node->next;
kono
parents:
diff changeset
453 }
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 objc_mutex_unlock (sync_pool_protection_locks[hash]);
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 /* A lock for 'object' to unlock could not be found (!!). */
kono
parents:
diff changeset
458 return OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
kono
parents:
diff changeset
459 }