2 * Copyright (c) 1998 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 REGENTS 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
34 * POSIX stdio FILE locking functions. These assume that the locking
35 * is only required at FILE structure level, not at file descriptor
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "namespace.h"
48 #include "un-namespace.h"
54 * Weak symbols for externally visible functions in this file:
56 __weak_reference(_flockfile, flockfile);
57 __weak_reference(_flockfile_debug_stub, _flockfile_debug);
58 __weak_reference(_ftrylockfile, ftrylockfile);
59 __weak_reference(_funlockfile, funlockfile);
62 * We need to retain binary compatibility for a while. So pretend
63 * that _lock is part of FILE * even though it is dereferenced off
64 * _extra now. When we stop encoding the size of FILE into binaries
65 * this can be changed in stdio.h. This will reduce the amount of
66 * code that has to change in the future (just remove this comment
74 pthread_t curthread = _pthread_self();
76 if (fp->_lock->fl_owner == curthread)
77 fp->_lock->fl_count++;
80 * Make sure this mutex is treated as a private
83 _pthread_mutex_lock(&fp->_lock->fl_mutex);
84 fp->_lock->fl_owner = curthread;
85 fp->_lock->fl_count = 1;
90 * This can be overriden by the threads library if it is linked in.
93 _flockfile_debug_stub(FILE *fp, char *fname, int lineno)
99 _ftrylockfile(FILE *fp)
101 pthread_t curthread = _pthread_self();
104 if (fp->_lock->fl_owner == curthread)
105 fp->_lock->fl_count++;
107 * Make sure this mutex is treated as a private
110 else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
111 fp->_lock->fl_owner = curthread;
112 fp->_lock->fl_count = 1;
120 _funlockfile(FILE *fp)
122 pthread_t curthread = _pthread_self();
125 * Check if this file is owned by the current thread:
127 if (fp->_lock->fl_owner == curthread) {
129 * Check if this thread has locked the FILE
132 if (fp->_lock->fl_count > 1)
134 * Decrement the count of the number of
135 * times the running thread has locked this
138 fp->_lock->fl_count--;
141 * The running thread will release the
144 fp->_lock->fl_count = 0;
145 fp->_lock->fl_owner = NULL;
146 _pthread_mutex_unlock(&fp->_lock->fl_mutex);