diff options
author | Roland McGrath <roland@gnu.org> | 2000-03-27 18:19:46 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 2000-03-27 18:19:46 +0000 |
commit | 6e5801f728a121cc002d7f6912dab2d9b60a3743 (patch) | |
tree | 01463a6a6400102c46fe002ab5c21acf0ba27bf2 | |
parent | 0f3c84b5ef7c5083441eef53849830738ad731d9 (diff) |
2000-03-27 Roland McGrath <roland@baalperazim.frob.com>
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.
-rw-r--r-- | libthreads/Makefile | 12 | ||||
-rw-r--r-- | libthreads/libthreads.map | 19 | ||||
-rw-r--r-- | libthreads/lockfile.c | 101 |
3 files changed, 130 insertions, 2 deletions
diff --git a/libthreads/Makefile b/libthreads/Makefile index 5b2bc62a..ee862366 100644 --- a/libthreads/Makefile +++ b/libthreads/Makefile @@ -1,5 +1,5 @@ # -# Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc. +# Copyright (C) 1994,95,96,97,2000 Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -21,7 +21,7 @@ makemode := library # In GNU mig_support.c, sync.c and machine/lock.s are omitted; that work is # all done in libc. SRCS := call.c cprocs.c cthread_data.c cthreads.c stack.c \ - cancel-cond.c rwlock.c + cancel-cond.c rwlock.c lockfile.c I386SRCS := i386/csw.S i386/thread.c # In GNU machine/cthreads.h is omitted; that work is done in libc headers. @@ -50,3 +50,11 @@ lndist-i386-files: $(top_srcdir)/hurd-snap/$(dir)/i386 $(top_srcdir)/hurd-snap/$(dir)/i386: mkdir $@ + +ifeq ($(VERSIONING),yes) + +# Adding this dependency gets it included in the command line, +# where ld will read it as a linker script. +$(libname).so.$(hurd-version): $(srcdir)/$(libname).map + +endif diff --git a/libthreads/libthreads.map b/libthreads/libthreads.map new file mode 100644 index 00000000..28c85017 --- /dev/null +++ b/libthreads/libthreads.map @@ -0,0 +1,19 @@ +/* This is the version script file used for building libthreads.so. -*- C -*- + It is in the form of a linker script, to be including as an input + file in the link command, rather than with --version-script. */ + + /* We only define these symbols when built against a libio-using libc, + which we presume will be 2.2 that is expecting our lockfile.c hooks. + For now, the rest of the library's symbols remain unversioned. */ + +VERSION +{ + GLIBC_2.2 + { + global: + _IO_flockfile; _IO_funlockfile; _IO_ftrylockfile; + flockfile; funlockfile; ftrylockfile; + local: + _cthreads_flockfile; _cthreads_funlockfile; _cthreads_ftrylockfile; + }; +}; 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 <cthreads.h> /* Must come before <stdio.h>! */ + +#define _IO_MTSAFE_IO +#include <stdio.h> + +#include <assert.h> + + +#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 */ |