]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdio/_flock_stub.c
This commit was generated by cvs2svn to compensate for changes in r155506,
[FreeBSD/FreeBSD.git] / lib / libc / stdio / _flock_stub.c
1 /*
2  * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * POSIX stdio FILE locking functions. These assume that the locking
35  * is only required at FILE structure level, not at file descriptor
36  * level too.
37  *
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include "namespace.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <pthread.h>
48 #include "un-namespace.h"
49
50 #include "local.h"
51
52
53 /*
54  * Weak symbols for externally visible functions in this file:
55  */
56 __weak_reference(_flockfile, flockfile);
57 __weak_reference(_flockfile_debug_stub, _flockfile_debug);
58 __weak_reference(_ftrylockfile, ftrylockfile);
59 __weak_reference(_funlockfile, funlockfile);
60
61 /*
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
67  * and #define).
68  */
69 #define _lock _extra
70
71 void
72 _flockfile(FILE *fp)
73 {
74         pthread_t curthread = _pthread_self();
75
76         if (fp->_lock->fl_owner == curthread)
77                 fp->_lock->fl_count++;
78         else {
79                 /*
80                  * Make sure this mutex is treated as a private
81                  * internal mutex:
82                  */
83                 _pthread_mutex_lock(&fp->_lock->fl_mutex);
84                 fp->_lock->fl_owner = curthread;
85                 fp->_lock->fl_count = 1;
86         }
87 }
88
89 /*
90  * This can be overriden by the threads library if it is linked in.
91  */
92 void
93 _flockfile_debug_stub(FILE *fp, char *fname, int lineno)
94 {
95         _flockfile(fp);
96 }
97
98 int
99 _ftrylockfile(FILE *fp)
100 {
101         pthread_t curthread = _pthread_self();
102         int     ret = 0;
103
104         if (fp->_lock->fl_owner == curthread)
105                 fp->_lock->fl_count++;
106         /*
107          * Make sure this mutex is treated as a private
108          * internal mutex:
109          */
110         else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
111                 fp->_lock->fl_owner = curthread;
112                 fp->_lock->fl_count = 1;
113         }
114         else
115                 ret = -1;
116         return (ret);
117 }
118
119 void 
120 _funlockfile(FILE *fp)
121 {
122         pthread_t       curthread = _pthread_self();
123
124         /*
125          * Check if this file is owned by the current thread:
126          */
127         if (fp->_lock->fl_owner == curthread) {
128                 /*
129                  * Check if this thread has locked the FILE
130                  * more than once:
131                  */
132                 if (fp->_lock->fl_count > 1)
133                         /*
134                          * Decrement the count of the number of
135                          * times the running thread has locked this
136                          * file:
137                          */
138                         fp->_lock->fl_count--;
139                 else {
140                         /*
141                          * The running thread will release the
142                          * lock now:
143                          */
144                         fp->_lock->fl_count = 0;
145                         fp->_lock->fl_owner = NULL;
146                         _pthread_mutex_unlock(&fp->_lock->fl_mutex);
147                 }
148         }
149 }