1 /* $NetBSD: flockfile.c,v 1.11 2012/03/15 18:22:30 christos Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: flockfile.c,v 1.11 2012/03/15 18:22:30 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
43 #include "reentrant.h"
47 __weak_alias(flockfile
,_flockfile
)
48 __weak_alias(ftrylockfile
,_ftrylockfile
)
49 __weak_alias(funlockfile
,_funlockfile
)
54 * XXX This code makes the assumption that a thr_t (pthread_t) is a
62 __flockfile_internal(fp
, 0);
66 ftrylockfile(FILE *fp
)
70 if (__isthreaded
== 0)
74 mutex_lock(&_LOCK(fp
));
76 if (_LOCKOWNER(fp
) == thr_self()) {
78 } else if (_LOCKOWNER(fp
) == NULL
) {
79 _LOCKOWNER(fp
) = thr_self();
84 mutex_unlock(&_LOCK(fp
));
93 __funlockfile_internal(fp
, 0);
97 __flockfile_internal(FILE *fp
, int internal
)
100 if (__isthreaded
== 0)
103 mutex_lock(&_LOCK(fp
));
105 if (_LOCKOWNER(fp
) == thr_self()) {
110 /* danger! cond_wait() is a cancellation point. */
112 thr_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
113 while (_LOCKOWNER(fp
) != NULL
)
114 cond_wait(&_LOCKCOND(fp
), &_LOCK(fp
));
115 thr_setcancelstate(oldstate
, NULL
);
116 _LOCKOWNER(fp
) = thr_self();
119 _LOCKINTERNAL(fp
) = 1;
122 if (_LOCKINTERNAL(fp
) == 1)
123 /* stash cancellation state and disable */
124 thr_setcancelstate(PTHREAD_CANCEL_DISABLE
,
125 &_LOCKCANCELSTATE(fp
));
127 mutex_unlock(&_LOCK(fp
));
131 __funlockfile_internal(FILE *fp
, int internal
)
134 if (__isthreaded
== 0)
137 mutex_lock(&_LOCK(fp
));
141 if (_LOCKINTERNAL(fp
) == 0)
142 thr_setcancelstate(_LOCKCANCELSTATE(fp
), NULL
);
146 if (_LOCKCOUNT(fp
) == 0) {
147 _LOCKOWNER(fp
) = NULL
;
148 cond_signal(&_LOCKCOND(fp
));
151 mutex_unlock(&_LOCK(fp
));
154 #else /* _REENTRANT */
159 /* LINTED deliberate lack of effect */
166 ftrylockfile(FILE *fp
)
168 /* LINTED deliberate lack of effect */
175 funlockfile(FILE *fp
)
177 /* LINTED deliberate lack of effect */
183 #endif /* _REENTRANT */