Home | History | Annotate | Line # | Download | only in stdio
flockfile.c revision 1.8.30.1
      1 /*	$NetBSD: flockfile.c,v 1.8.30.1 2008/05/18 12:30:19 yamt 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  *
     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.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: flockfile.c,v 1.8.30.1 2008/05/18 12:30:19 yamt Exp $");
     35 #endif /* LIBC_SCCS and not lint */
     36 
     37 #include "namespace.h"
     38 
     39 #include <assert.h>
     40 #include <errno.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include "reentrant.h"
     44 #include "local.h"
     45 
     46 #ifdef __weak_alias
     47 __weak_alias(flockfile,_flockfile)
     48 __weak_alias(ftrylockfile,_ftrylockfile)
     49 __weak_alias(funlockfile,_funlockfile)
     50 #endif
     51 
     52 #ifdef _REENTRANT
     53 /*
     54  * XXX This code makes the assumption that a thr_t (pthread_t) is a
     55  * XXX pointer.
     56  */
     57 
     58 extern int __isthreaded;
     59 
     60 void
     61 flockfile(FILE *fp)
     62 {
     63 
     64 	__flockfile_internal(fp, 0);
     65 }
     66 
     67 int
     68 ftrylockfile(FILE *fp)
     69 {
     70 	int retval;
     71 
     72 	if (__isthreaded == 0)
     73 		return 0;
     74 
     75 	retval = 0;
     76 	mutex_lock(&_LOCK(fp));
     77 
     78 	if (_LOCKOWNER(fp) == thr_self()) {
     79 		_LOCKCOUNT(fp)++;
     80 	} else if (_LOCKOWNER(fp) == NULL) {
     81 		_LOCKOWNER(fp) = thr_self();
     82 		_LOCKCOUNT(fp) = 1;
     83 	} else
     84 		retval = -1;
     85 
     86 	mutex_unlock(&_LOCK(fp));
     87 
     88 	return retval;
     89 }
     90 
     91 void
     92 funlockfile(FILE *fp)
     93 {
     94 
     95 	__funlockfile_internal(fp, 0);
     96 }
     97 
     98 void
     99 __flockfile_internal(FILE *fp, int internal)
    100 {
    101 
    102 	if (__isthreaded == 0)
    103 		return;
    104 
    105 	mutex_lock(&_LOCK(fp));
    106 
    107 	if (_LOCKOWNER(fp) == thr_self()) {
    108 		_LOCKCOUNT(fp)++;
    109 		if (internal)
    110 			_LOCKINTERNAL(fp)++;
    111 	} else {
    112 		/* danger! cond_wait() is a cancellation point. */
    113 		int oldstate;
    114 		thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
    115 		while (_LOCKOWNER(fp) != NULL)
    116 			cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
    117 		thr_setcancelstate(oldstate, NULL);
    118 		_LOCKOWNER(fp) = thr_self();
    119 		_LOCKCOUNT(fp) = 1;
    120 		if (internal)
    121 			_LOCKINTERNAL(fp) = 1;
    122 	}
    123 
    124 	if (_LOCKINTERNAL(fp) == 1)
    125 		/* stash cancellation state and disable */
    126 		thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
    127 		    &_LOCKCANCELSTATE(fp));
    128 
    129 	mutex_unlock(&_LOCK(fp));
    130 }
    131 
    132 void
    133 __funlockfile_internal(FILE *fp, int internal)
    134 {
    135 
    136 	if (__isthreaded == 0)
    137 		return;
    138 
    139 	mutex_lock(&_LOCK(fp));
    140 
    141 	if (internal) {
    142 		_LOCKINTERNAL(fp)--;
    143 		if (_LOCKINTERNAL(fp) == 0)
    144 			thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
    145 	}
    146 
    147 	_LOCKCOUNT(fp)--;
    148 	if (_LOCKCOUNT(fp) == 0) {
    149 		_LOCKOWNER(fp) = NULL;
    150 		cond_signal(&_LOCKCOND(fp));
    151 	}
    152 
    153 	mutex_unlock(&_LOCK(fp));
    154 }
    155 
    156 #else /* _REENTRANT */
    157 
    158 void
    159 flockfile(FILE *fp)
    160 {
    161 	/* LINTED deliberate lack of effect */
    162 	(void)fp;
    163 
    164 	return;
    165 }
    166 
    167 int
    168 ftrylockfile(FILE *fp)
    169 {
    170 	/* LINTED deliberate lack of effect */
    171 	(void)fp;
    172 
    173 	return (0);
    174 }
    175 
    176 void
    177 funlockfile(FILE *fp)
    178 {
    179 	/* LINTED deliberate lack of effect */
    180 	(void)fp;
    181 
    182 	return;
    183 }
    184 
    185 #endif /* _REENTRANT */
    186