2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/lib/libc_r/uthread/uthread_file.c,v 1.12.2.3 2002/10/22 14:44:03 fjoe Exp $
34 * POSIX stdio FILE locking functions. These assume that the locking
35 * is only required at FILE structure level, not at file descriptor
42 #include <sys/queue.h>
44 #include "pthread_private.h"
47 * Weak symbols for externally visible functions in this file:
49 #pragma weak flockfile=_flockfile
50 #pragma weak ftrylockfile=_ftrylockfile
51 #pragma weak funlockfile=_funlockfile
54 * The FILE lock structure. The FILE *fp is locked if the owner is
55 * not NULL. If not locked, the file lock structure can be
56 * reassigned to a different file by setting fp.
59 LIST_ENTRY(file_lock) entry; /* Entry if file list. */
60 TAILQ_HEAD(lock_head, pthread)
61 l_head; /* Head of queue for threads */
62 /* waiting on this lock. */
63 FILE *fp; /* The target file. */
64 struct pthread *owner; /* Thread that owns lock. */
65 int count; /* Lock count for owner. */
69 * The number of file lock lists into which the file pointer is
70 * hashed. Ideally, the FILE structure size would have been increased,
71 * but this causes incompatibility, so separate data structures are
77 * This macro casts a file pointer to a long integer and right
78 * shifts this by the number of bytes in a pointer. The shifted
79 * value is then remaindered using the maximum number of hash
80 * entries to produce and index into the array of static lock
81 * structures. If there is a collision, a linear search of the
82 * dynamic list of locks linked to each static lock is perfomed.
84 #define file_idx(_p) ((((u_long) _p) >> sizeof(void *)) % NUM_HEADS)
87 * Global array of file locks. The first lock for each hash bucket is
88 * allocated statically in the hope that there won't be too many
89 * collisions that require a malloc and an element added to the list.
91 struct static_file_lock {
92 LIST_HEAD(file_list_head, file_lock) head;
96 /* Set to non-zero when initialisation is complete: */
97 static int init_done = 0;
99 /* Lock for accesses to the hash table: */
100 static spinlock_t hash_lock = _SPINLOCK_INITIALIZER;
103 * Find a lock structure for a FILE, return NULL if the file is
108 find_lock(int idx, FILE *fp)
112 /* Check if the file is locked using the static structure: */
113 if (flh[idx].fl.fp == fp && flh[idx].fl.owner != NULL)
114 /* Return a pointer to the static lock: */
117 /* Point to the first dynamic lock: */
118 p = flh[idx].head.lh_first;
121 * Loop through the dynamic locks looking for the
124 while (p != NULL && (p->fp != fp || p->owner == NULL))
125 /* Not this file, try the next: */
126 p = p->entry.le_next;
132 * Lock a file, assuming that there is no lock structure currently
137 do_lock(int idx, FILE *fp)
139 struct pthread *curthread = _get_curthread();
142 /* Check if the static structure is not being used: */
143 if (flh[idx].fl.owner == NULL) {
144 /* Return a pointer to the static lock: */
148 /* Point to the first dynamic lock: */
149 p = flh[idx].head.lh_first;
152 * Loop through the dynamic locks looking for a
153 * lock structure that is not being used:
155 while (p != NULL && p->owner != NULL)
156 /* This one is used, try the next: */
157 p = p->entry.le_next;
161 * If an existing lock structure has not been found,
162 * allocate memory for a new one:
164 if (p == NULL && (p = (struct file_lock *)
165 malloc(sizeof(struct file_lock))) != NULL) {
166 /* Add the new element to the list: */
167 LIST_INSERT_HEAD(&flh[idx].head, p, entry);
170 /* Check if there is a lock structure to acquire: */
172 /* Acquire the lock for the running thread: */
174 p->owner = curthread;
176 TAILQ_INIT(&p->l_head);
182 _flockfile_debug(FILE * fp, char *fname, int lineno)
184 struct pthread *curthread = _get_curthread();
185 int idx = file_idx(fp);
188 /* Check if this is a real file: */
189 if (fp->_file >= 0) {
190 /* Lock the hash table: */
191 _SPINLOCK(&hash_lock);
193 /* Check if the static array has not been initialised: */
195 /* Initialise the global array: */
196 memset(flh,0,sizeof(flh));
198 /* Flag the initialisation as complete: */
202 /* Get a pointer to any existing lock for the file: */
203 if ((p = find_lock(idx, fp)) == NULL) {
205 * The file is not locked, so this thread can
208 p = do_lock(idx, fp);
210 /* Unlock the hash table: */
211 _SPINUNLOCK(&hash_lock);
214 * The file is already locked, so check if the
215 * running thread is the owner:
217 } else if (p->owner == curthread) {
219 * The running thread is already the
220 * owner, so increment the count of
221 * the number of times it has locked
226 /* Unlock the hash table: */
227 _SPINUNLOCK(&hash_lock);
229 /* Clear the interrupted flag: */
230 curthread->interrupted = 0;
233 * Prevent being context switched out while
234 * adding this thread to the file lock queue.
236 _thread_kern_sig_defer();
239 * The file is locked for another thread.
240 * Append this thread to the queue of
241 * threads waiting on the lock.
243 TAILQ_INSERT_TAIL(&p->l_head,curthread,qe);
244 curthread->flags |= PTHREAD_FLAGS_IN_FILEQ;
246 /* Unlock the hash table: */
247 _SPINUNLOCK(&hash_lock);
249 curthread->data.fp = fp;
251 /* Wait on the FILE lock: */
252 _thread_kern_sched_state(PS_FILE_WAIT, fname, lineno);
254 if ((curthread->flags & PTHREAD_FLAGS_IN_FILEQ) != 0) {
255 TAILQ_REMOVE(&p->l_head,curthread,qe);
256 curthread->flags &= ~PTHREAD_FLAGS_IN_FILEQ;
259 _thread_kern_sig_undefer();
261 if (curthread->interrupted != 0 &&
262 curthread->continuation != NULL)
263 curthread->continuation((void *)curthread);
269 _flockfile(FILE * fp)
271 _flockfile_debug(fp, __FILE__, __LINE__);
275 _ftrylockfile(FILE * fp)
277 struct pthread *curthread = _get_curthread();
279 int idx = file_idx(fp);
282 /* Check if this is a real file: */
283 if (fp->_file >= 0) {
284 /* Lock the hash table: */
285 _SPINLOCK(&hash_lock);
287 /* Get a pointer to any existing lock for the file: */
288 if ((p = find_lock(idx, fp)) == NULL) {
290 * The file is not locked, so this thread can
293 p = do_lock(idx, fp);
296 * The file is already locked, so check if the
297 * running thread is the owner:
299 } else if (p->owner == curthread) {
301 * The running thread is already the
302 * owner, so increment the count of
303 * the number of times it has locked
309 * The file is locked for another thread,
315 /* Check if the lock was obtained: */
317 /* Return success: */
320 /* Unlock the hash table: */
321 _SPINUNLOCK(&hash_lock);
328 _funlockfile(FILE * fp)
330 struct pthread *curthread = _get_curthread();
331 int idx = file_idx(fp);
334 /* Check if this is a real file: */
335 if (fp->_file >= 0) {
337 * Defer signals to protect the scheduling queues from
338 * access by the signal handler:
340 _thread_kern_sig_defer();
342 /* Lock the hash table: */
343 _SPINLOCK(&hash_lock);
346 * Get a pointer to the lock for the file and check that
347 * the running thread is the one with the lock:
349 if ((p = find_lock(idx, fp)) != NULL &&
350 p->owner == curthread) {
352 * Check if this thread has locked the FILE
357 * Decrement the count of the number of
358 * times the running thread has locked this
364 * The running thread will release the
369 /* Get the new owner of the lock: */
370 while ((p->owner = TAILQ_FIRST(&p->l_head)) != NULL) {
371 /* Pop the thread off the queue: */
372 TAILQ_REMOVE(&p->l_head,p->owner,qe);
373 p->owner->flags &= ~PTHREAD_FLAGS_IN_FILEQ;
375 if (p->owner->interrupted == 0) {
377 * This is the first lock for
382 /* Allow the new owner to run: */
383 PTHREAD_NEW_STATE(p->owner,PS_RUNNING);
385 /* End the loop when we find a
386 * thread that hasn't been
387 * cancelled or interrupted;
395 /* Unlock the hash table: */
396 _SPINUNLOCK(&hash_lock);
399 * Undefer and handle pending signals, yielding if
402 _thread_kern_sig_undefer();
407 _funlock_owned(struct pthread *pthread)
410 struct file_lock *p, *next_p;
413 * Defer signals to protect the scheduling queues from
414 * access by the signal handler:
416 _thread_kern_sig_defer();
418 /* Lock the hash table: */
419 _SPINLOCK(&hash_lock);
421 for (idx = 0; idx < NUM_HEADS; idx++) {
422 /* Check the static file lock first: */
424 next_p = LIST_FIRST(&flh[idx].head);
427 if (p->owner == pthread) {
429 * The running thread will release the
434 /* Get the new owner of the lock: */
435 while ((p->owner = TAILQ_FIRST(&p->l_head)) != NULL) {
436 /* Pop the thread off the queue: */
437 TAILQ_REMOVE(&p->l_head,p->owner,qe);
438 p->owner->flags &= ~PTHREAD_FLAGS_IN_FILEQ;
440 if (p->owner->interrupted == 0) {
442 * This is the first lock for
447 /* Allow the new owner to run: */
448 PTHREAD_NEW_STATE(p->owner,PS_RUNNING);
450 /* End the loop when we find a
451 * thread that hasn't been
452 * cancelled or interrupted;
460 next_p = LIST_NEXT(next_p, entry);
464 /* Unlock the hash table: */
465 _SPINUNLOCK(&hash_lock);
468 * Undefer and handle pending signals, yielding if
471 _thread_kern_sig_undefer();
475 _flockfile_backout(struct pthread *pthread)
477 int idx = file_idx(pthread->data.fp);
481 * Defer signals to protect the scheduling queues from
482 * access by the signal handler:
484 _thread_kern_sig_defer();
487 * Get a pointer to the lock for the file and check that
488 * the running thread is the one with the lock:
490 if (((pthread->flags & PTHREAD_FLAGS_IN_FILEQ) != 0) &&
491 ((p = find_lock(idx, pthread->data.fp)) != NULL)) {
492 /* Lock the hash table: */
493 _SPINLOCK(&hash_lock);
495 /* Remove the thread from the queue: */
496 TAILQ_REMOVE(&p->l_head, pthread, qe);
497 pthread->flags &= ~PTHREAD_FLAGS_IN_FILEQ;
499 /* Unlock the hash table: */
500 _SPINUNLOCK(&hash_lock);
504 * Undefer and handle pending signals, yielding if necessary:
506 _thread_kern_sig_undefer();