From 6e5801f728a121cc002d7f6912dab2d9b60a3743 Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Mon, 27 Mar 2000 18:19:46 +0000 Subject: 2000-03-27 Roland McGrath Locking support for stdio streams with glibc-2.2 libio. EXPERIMENTAL! * lockfile.c: New file. * libthreads.map: New file. * Makefile (SRCS): Add lockfile.c. [$(VERSIONING) = yes] ($(libname).so.$(hurd-version)): Depend on $(srcdir)/$(libname).map to get version script into the link. --- libthreads/lockfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 libthreads/lockfile.c (limited to 'libthreads/lockfile.c') diff --git a/libthreads/lockfile.c b/libthreads/lockfile.c new file mode 100644 index 00000000..b0b019de --- /dev/null +++ b/libthreads/lockfile.c @@ -0,0 +1,101 @@ +/* lockfile - Handle locking and unlocking of stream. Hurd cthreads version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include /* Must come before ! */ + +#define _IO_MTSAFE_IO +#include + +#include + + +#ifdef _STDIO_USES_IOSTREAM + +#undef _IO_flockfile +#undef _IO_funlockfile +#undef _IO_ftrylockfile + +void +_cthreads_flockfile (_IO_FILE *fp) +{ + cthread_t self = cthread_self (); + + if (fp->_lock->owner == self) + { + assert (fp->_lock->count != 0); + ++fp->_lock->count; + } + else + { + __mutex_lock (&fp->_lock->mutex); + assert (fp->_lock->owner == 0); + fp->_lock->owner = self; + assert (fp->_lock->count == 0); + fp->_lock->count = 1; + } +} + +void +_cthreads_funlockfile (_IO_FILE *fp) +{ + if (--fp->_lock->count == 0) + { + assert (fp->_lock->owner == cthread_self ()); + fp->_lock->owner = 0; + __mutex_unlock (&fp->_lock->mutex); + } +} + +int +_cthreads_ftrylockfile (_IO_FILE *fp) +{ + cthread_t self = cthread_self (); + + if (fp->_lock->owner == self) + { + assert (fp->_lock->count != 0); + ++fp->_lock->count; + } + else if (__mutex_try_lock (&fp->_lock->mutex)) + { + assert (fp->_lock->owner == 0); + fp->_lock->owner = self; + assert (fp->_lock->count == 0); + fp->_lock->count = 1; + } + else + return 0; /* No lock for us. */ + + /* We got the lock. */ + return 1; +} + +#define strong_alias(src, dst) asm (".globl " #dst "; " #dst " = " #src) +#define weak_alias(src, dst) asm (".weak " #dst "; " #dst " = " #src) + +weak_alias (_cthreads_flockfile, _IO_flockfile); +weak_alias (_cthreads_funlockfile, _IO_funlockfile); +weak_alias (_cthreads_ftrylockfile, _IO_ftrylockfile); + +weak_alias (_cthreads_flockfile, flockfile); +weak_alias (_cthreads_funlockfile, funlockfile); +weak_alias (_cthreads_ftrylockfile, ftrylockfile); + + +#endif /* _STDIO_USES_IOSTREAM */ -- cgit v1.2.3