2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * POSIX stdio FILE locking functions. These assume that the locking
34 * is only required at FILE structure level, not at file descriptor
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "namespace.h"
47 #include "un-namespace.h"
53 * Weak symbols for externally visible functions in this file:
55 __weak_reference(_flockfile, flockfile);
56 __weak_reference(_flockfile_debug_stub, _flockfile_debug);
57 __weak_reference(_ftrylockfile, ftrylockfile);
58 __weak_reference(_funlockfile, funlockfile);
60 void _flockfile_debug_stub(FILE *, char *, int);
61 int _ftrylockfile(FILE *);
66 pthread_t curthread = _pthread_self();
68 if (fp->_fl_owner == curthread)
72 * Make sure this mutex is treated as a private
75 _pthread_mutex_lock(&fp->_fl_mutex);
76 fp->_fl_owner = curthread;
82 * This can be overriden by the threads library if it is linked in.
85 _flockfile_debug_stub(FILE *fp, char *fname, int lineno)
91 _ftrylockfile(FILE *fp)
93 pthread_t curthread = _pthread_self();
96 if (fp->_fl_owner == curthread)
99 * Make sure this mutex is treated as a private
102 else if (_pthread_mutex_trylock(&fp->_fl_mutex) == 0) {
103 fp->_fl_owner = curthread;
112 _funlockfile(FILE *fp)
114 pthread_t curthread = _pthread_self();
117 * Check if this file is owned by the current thread:
119 if (fp->_fl_owner == curthread) {
121 * Check if this thread has locked the FILE
124 if (fp->_fl_count > 1)
126 * Decrement the count of the number of
127 * times the running thread has locked this
133 * The running thread will release the
137 fp->_fl_owner = NULL;
138 _pthread_mutex_unlock(&fp->_fl_mutex);