1*f14fb602SLionel Sambuc /* $NetBSD: flockfile.c,v 1.11 2012/03/15 18:22:30 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2002 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Nathan J. Williams.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*f14fb602SLionel Sambuc __RCSID("$NetBSD: flockfile.c,v 1.11 2012/03/15 18:22:30 christos Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <assert.h>
402fe8fb19SBen Gras #include <errno.h>
412fe8fb19SBen Gras #include <stdio.h>
422fe8fb19SBen Gras #include <string.h>
432fe8fb19SBen Gras #include "reentrant.h"
442fe8fb19SBen Gras #include "local.h"
452fe8fb19SBen Gras
462fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(flockfile,_flockfile)472fe8fb19SBen Gras __weak_alias(flockfile,_flockfile)
482fe8fb19SBen Gras __weak_alias(ftrylockfile,_ftrylockfile)
492fe8fb19SBen Gras __weak_alias(funlockfile,_funlockfile)
502fe8fb19SBen Gras #endif
512fe8fb19SBen Gras
522fe8fb19SBen Gras #ifdef _REENTRANT
532fe8fb19SBen Gras /*
542fe8fb19SBen Gras * XXX This code makes the assumption that a thr_t (pthread_t) is a
552fe8fb19SBen Gras * XXX pointer.
562fe8fb19SBen Gras */
572fe8fb19SBen Gras
582fe8fb19SBen Gras void
592fe8fb19SBen Gras flockfile(FILE *fp)
602fe8fb19SBen Gras {
612fe8fb19SBen Gras
622fe8fb19SBen Gras __flockfile_internal(fp, 0);
632fe8fb19SBen Gras }
642fe8fb19SBen Gras
652fe8fb19SBen Gras int
ftrylockfile(FILE * fp)662fe8fb19SBen Gras ftrylockfile(FILE *fp)
672fe8fb19SBen Gras {
682fe8fb19SBen Gras int retval;
692fe8fb19SBen Gras
702fe8fb19SBen Gras if (__isthreaded == 0)
712fe8fb19SBen Gras return 0;
722fe8fb19SBen Gras
732fe8fb19SBen Gras retval = 0;
742fe8fb19SBen Gras mutex_lock(&_LOCK(fp));
752fe8fb19SBen Gras
762fe8fb19SBen Gras if (_LOCKOWNER(fp) == thr_self()) {
772fe8fb19SBen Gras _LOCKCOUNT(fp)++;
782fe8fb19SBen Gras } else if (_LOCKOWNER(fp) == NULL) {
792fe8fb19SBen Gras _LOCKOWNER(fp) = thr_self();
802fe8fb19SBen Gras _LOCKCOUNT(fp) = 1;
812fe8fb19SBen Gras } else
822fe8fb19SBen Gras retval = -1;
832fe8fb19SBen Gras
842fe8fb19SBen Gras mutex_unlock(&_LOCK(fp));
852fe8fb19SBen Gras
862fe8fb19SBen Gras return retval;
872fe8fb19SBen Gras }
882fe8fb19SBen Gras
892fe8fb19SBen Gras void
funlockfile(FILE * fp)902fe8fb19SBen Gras funlockfile(FILE *fp)
912fe8fb19SBen Gras {
922fe8fb19SBen Gras
932fe8fb19SBen Gras __funlockfile_internal(fp, 0);
942fe8fb19SBen Gras }
952fe8fb19SBen Gras
962fe8fb19SBen Gras void
__flockfile_internal(FILE * fp,int internal)972fe8fb19SBen Gras __flockfile_internal(FILE *fp, int internal)
982fe8fb19SBen Gras {
992fe8fb19SBen Gras
1002fe8fb19SBen Gras if (__isthreaded == 0)
1012fe8fb19SBen Gras return;
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras mutex_lock(&_LOCK(fp));
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras if (_LOCKOWNER(fp) == thr_self()) {
1062fe8fb19SBen Gras _LOCKCOUNT(fp)++;
1072fe8fb19SBen Gras if (internal)
1082fe8fb19SBen Gras _LOCKINTERNAL(fp)++;
1092fe8fb19SBen Gras } else {
1102fe8fb19SBen Gras /* danger! cond_wait() is a cancellation point. */
1112fe8fb19SBen Gras int oldstate;
1122fe8fb19SBen Gras thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
1132fe8fb19SBen Gras while (_LOCKOWNER(fp) != NULL)
1142fe8fb19SBen Gras cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
1152fe8fb19SBen Gras thr_setcancelstate(oldstate, NULL);
1162fe8fb19SBen Gras _LOCKOWNER(fp) = thr_self();
1172fe8fb19SBen Gras _LOCKCOUNT(fp) = 1;
1182fe8fb19SBen Gras if (internal)
1192fe8fb19SBen Gras _LOCKINTERNAL(fp) = 1;
1202fe8fb19SBen Gras }
1212fe8fb19SBen Gras
1222fe8fb19SBen Gras if (_LOCKINTERNAL(fp) == 1)
1232fe8fb19SBen Gras /* stash cancellation state and disable */
1242fe8fb19SBen Gras thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
1252fe8fb19SBen Gras &_LOCKCANCELSTATE(fp));
1262fe8fb19SBen Gras
1272fe8fb19SBen Gras mutex_unlock(&_LOCK(fp));
1282fe8fb19SBen Gras }
1292fe8fb19SBen Gras
1302fe8fb19SBen Gras void
__funlockfile_internal(FILE * fp,int internal)1312fe8fb19SBen Gras __funlockfile_internal(FILE *fp, int internal)
1322fe8fb19SBen Gras {
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras if (__isthreaded == 0)
1352fe8fb19SBen Gras return;
1362fe8fb19SBen Gras
1372fe8fb19SBen Gras mutex_lock(&_LOCK(fp));
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras if (internal) {
1402fe8fb19SBen Gras _LOCKINTERNAL(fp)--;
1412fe8fb19SBen Gras if (_LOCKINTERNAL(fp) == 0)
1422fe8fb19SBen Gras thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
1432fe8fb19SBen Gras }
1442fe8fb19SBen Gras
1452fe8fb19SBen Gras _LOCKCOUNT(fp)--;
1462fe8fb19SBen Gras if (_LOCKCOUNT(fp) == 0) {
1472fe8fb19SBen Gras _LOCKOWNER(fp) = NULL;
1482fe8fb19SBen Gras cond_signal(&_LOCKCOND(fp));
1492fe8fb19SBen Gras }
1502fe8fb19SBen Gras
1512fe8fb19SBen Gras mutex_unlock(&_LOCK(fp));
1522fe8fb19SBen Gras }
1532fe8fb19SBen Gras
1542fe8fb19SBen Gras #else /* _REENTRANT */
1552fe8fb19SBen Gras
1562fe8fb19SBen Gras void
1572fe8fb19SBen Gras flockfile(FILE *fp)
1582fe8fb19SBen Gras {
1592fe8fb19SBen Gras /* LINTED deliberate lack of effect */
1602fe8fb19SBen Gras (void)fp;
1612fe8fb19SBen Gras
1622fe8fb19SBen Gras return;
1632fe8fb19SBen Gras }
1642fe8fb19SBen Gras
1652fe8fb19SBen Gras int
1662fe8fb19SBen Gras ftrylockfile(FILE *fp)
1672fe8fb19SBen Gras {
1682fe8fb19SBen Gras /* LINTED deliberate lack of effect */
1692fe8fb19SBen Gras (void)fp;
1702fe8fb19SBen Gras
171*f14fb602SLionel Sambuc return 0;
1722fe8fb19SBen Gras }
1732fe8fb19SBen Gras
1742fe8fb19SBen Gras void
1752fe8fb19SBen Gras funlockfile(FILE *fp)
1762fe8fb19SBen Gras {
1772fe8fb19SBen Gras /* LINTED deliberate lack of effect */
1782fe8fb19SBen Gras (void)fp;
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras return;
1812fe8fb19SBen Gras }
1822fe8fb19SBen Gras
1832fe8fb19SBen Gras #endif /* _REENTRANT */
184