xref: /netbsd-src/lib/libc/stdio/flockfile.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: flockfile.c,v 1.4 2003/02/01 03:25:00 nathanw Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nathan J. Williams.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: flockfile.c,v 1.4 2003/02/01 03:25:00 nathanw Exp $");
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 
46 #include <assert.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include "reentrant.h"
51 #include "local.h"
52 
53 #ifdef __weak_alias
54 __weak_alias(flockfile,_flockfile)
55 __weak_alias(ftrylockfile,_ftrylockfile)
56 __weak_alias(funlockfile,_funlockfile)
57 #endif
58 
59 #ifdef _REENTRANT
60 /*
61  * XXX This code makes the assumption that a thr_t (pthread_t) is a
62  * XXX pointer.
63  */
64 
65 extern int __isthreaded;
66 
67 void
68 flockfile(FILE *fp)
69 {
70 
71 	if (__isthreaded == 0)
72 		return;
73 
74 	mutex_lock(&_LOCK(fp));
75 
76 	if (_LOCKOWNER(fp) == thr_self()) {
77 		_LOCKCOUNT(fp)++;
78 	} else {
79 		while (_LOCKOWNER(fp) != NULL)
80 			cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
81 		_LOCKOWNER(fp) = thr_self();
82 		_LOCKCOUNT(fp) = 1;
83 	}
84 
85 	mutex_unlock(&_LOCK(fp));
86 }
87 
88 int
89 ftrylockfile(FILE *fp)
90 {
91 	int retval;
92 
93 	if (__isthreaded == 0)
94 		return 0;
95 
96 	retval = 0;
97 	mutex_lock(&_LOCK(fp));
98 
99 	if (_LOCKOWNER(fp) == thr_self()) {
100 		_LOCKCOUNT(fp)++;
101 	} else if (_LOCKOWNER(fp) == NULL) {
102 		_LOCKOWNER(fp) = thr_self();
103 		_LOCKCOUNT(fp) = 1;
104 	} else
105 		retval = -1;
106 
107 	mutex_unlock(&_LOCK(fp));
108 
109 	return retval;
110 }
111 
112 void
113 funlockfile(FILE *fp)
114 {
115 
116 	if (__isthreaded == 0)
117 		return;
118 
119 	mutex_lock(&_LOCK(fp));
120 
121 	_LOCKCOUNT(fp)--;
122 	if (_LOCKCOUNT(fp) == 0) {
123 		_LOCKOWNER(fp) = NULL;
124 		cond_signal(&_LOCKCOND(fp));
125 	}
126 
127 	mutex_unlock(&_LOCK(fp));
128 }
129 
130 #else /* _REENTRANT */
131 
132 void
133 flockfile(FILE *fp)
134 {
135 	/* LINTED deliberate lack of effect */
136 	(void)fp;
137 
138 	return;
139 }
140 
141 int
142 ftrylockfile(FILE *fp)
143 {
144 	/* LINTED deliberate lack of effect */
145 	(void)fp;
146 
147 	return (0);
148 }
149 
150 void
151 funlockfile(FILE *fp)
152 {
153 	/* LINTED deliberate lack of effect */
154 	(void)fp;
155 
156 	return;
157 }
158 
159 #endif /* _REENTRANT */
160