glibc/db2/mutex
Ulrich Drepper a5a0310d8e Update.
1997-09-30 18:03  Ulrich Drepper  <drepper@cygnus.com>

	* Makerules: Undo last change.
	* csu/Makefile: Define before-compile at the right place.

	* aclocal.m4: Remove a.out file created by assembler test.

	* set-init.c: Find set-hooks.h using <...>.

	Update to db 2.3.10.
	* db2/Makefile: Update.
	* db2/db.h: Likewise.
	* db2/db_185.h: Likewise.
	* db2/db_int.h: Likewise.
	* db2/btree/bt_close.c: Likewise.
	* db2/btree/bt_conv.c: Likewise.
	* db2/btree/bt_cursor.c: Likewise.
	* db2/btree/bt_put.c: Likewise.
	* db2/btree/bt_rec.c: Likewise.
	* db2/btree/bt_recno.c: Likewise.
	* db2/btree/btree.src: Likewise.
	* db2/btree/btree_auto.c: Likewise.
	* db2/clib/getlong.c: Likewise.
	* db2/db/db.c: Likewise.
	* db2/db/db_auto.c: Likewise.
	* db2/db/db_conv.c: Likewise.
	* db2/db/db_pr.c: Likewise.
	* db2/db/db_ret.c: Likewise.
	* db2/db/db_thread.c: Likewise.
	* db2/hash/hash.c: Likewise.
	* db2/hash/hash_auto.c: Likewise.
	* db2/hash/hash_conv.c: Likewise.
	* db2/hash/hash_dup.c: Likewise.
	* db2/hash/hash_func.c: Likewise.
	* db2/hash/hash_page.c: Likewise.
	* db2/hash/hash_rec.c: Likewise.
	* db2/include/btree.h: Likewise.
	* db2/include/btree_ext.h: Likewise.
	* db2/include/db.h.src: Likewise.
	* db2/include/db_185.h.src: Likewise.
	* db2/include/db_cxx.h: Likewise.
	* db2/include/db_ext.h: Likewise.
	* db2/include/db_int.h.src: Likewise.
	* db2/include/db_page.h: Likewise.
	* db2/include/db_shash.h: Likewise.
	* db2/include/lock.h: Likewise.
	* db2/include/log.h: Likewise.
	* db2/include/log_ext.h: Likewise.
	* db2/include/mp.h: Likewise.
	* db2/include/shqueue.h: Likewise.
	* db2/include/txn.h: Likewise.
	* db2/lock/lock.c: Likewise.
	* db2/lock/lock_deadlock.c: Likewise.
	* db2/log/log.c: Likewise.
	* db2/log/log_archive.c: Likewise.
	* db2/log/log_auto.c: Likewise.
	* db2/log/log_get.c: Likewise.
	* db2/log/log_put.c: Likewise.
	* db2/log/log_register.c: Likewise.
	* db2/mp/mp_bh.c: Likewise.
	* db2/mp/mp_fget.c: Likewise.
	* db2/mp/mp_fopen.c: Likewise.
	* db2/mp/mp_fput.c: Likewise.
	* db2/mp/mp_fset.c: Likewise.
	* db2/mp/mp_open.c: Likewise.
	* db2/mutex/mutex.c: Likewise.
	* db2/os/db_os_dir.c: Likewise.
	* db2/progs/db_checkpoint/db_checkpoint.c: Likewise.
	* db2/progs/db_deadlock/db_deadlock.c: Likewise.
	* db2/progs/db_dump185/db_dump185.c: Likewise.
	* db2/progs/db_load/db_load.c: Likewise.
	* db2/progs/db_recover/db_recover.c: Likewise.
	* db2/txn/txn.c: Likewise.
	* db2/txn/txn_auto.c: Likewise.

	* elf/link.h: Define struct libname_list outside struct link_map
	to not confuse C++ compilers.

	* include/features.h: Recognize _XOPEN_SOURCE == 500 and set
	__USE_UNIX98.
	* manual/creature.texi: Explain this.

	* libc.map: Add new functions.

	* libio/Makefile (routines): Add fseeko and ftello.
	* libio/ftello.c: New file.
	* libio/fseeko.c: New file.
	* libio/stdio.h: Add prototypes for new functions.
	* manual/stdio.texi: Document fseeko and ftello.

	* posix/Makefile (routines): Add pread and pwrite.
	* sysdeps/posix/pread.c: New file.
	* sysdeps/posix/pwrite.c: New file.
	* sysdeps/stub/pread.c: New file.
	* sysdeps/stub/pwrite.c: New file.
	* posix/unistd.h: Add prototypes for pread and pwrite.
	Pretty print header.
	Define gid_t, uid_t, off_t, pid_t if __USE_UNIX98.
	Declare ctermid and cuserid if __USE_UNIX98.
	(swab): Change to take void * arguments.
	* string/swab.c: Change parameter to void *.
	* posix/sys/types: Define gid_t, uid_t, off_t, pid_t only if not
	already happened.
	* manual/llio.texi: Document pread and pwrite.

	* string/strings.h: Don't simply include string.h.  Define BSD
	functions according to Unix98.
	* stdlib/tst-strtol.c: Include <string.h> not <strings.h>.
	* sunrpc/clnt_simp.c: Likewise.

	* malloc/Makefile (aux): Add set-freeres.
	* malloc/mtrace.c: Define function release_libc_mem which calls the
	__libc_subfreeres handler.
	(mtrace): Register release_libc_mem.
	* malloc/set-freeres.c: New file.

	* intl/dcgettext.c: Define free_mem function and add to
	__libc_subfreeres list.
	* intl/finddomain.c: Likewise.
	* intl/gettextP.h (struct loaded_domain): Add new fields use_mmap
	and mmap_size.  Add prototype for _nl_unloaded_domain.
	* intl/loadmsgcat.c: Define new function _nl_unload_domain.
	(_nl_load_domain): Store informaiton about mmap use and file size.
	* intl/localealias.c (read_alias_file): Optimize locale alias file
	reading by avoid frequen mallocs.
	Define free_mem function and add to __libc_subfreeres list.

	* locale/localeinfo.h: Make a difference between MAX_USAGE_COUNT and
	undeletable.
	Add prototype for _nl_unload_locale.
	* locale/C-collate: Mark data as undeletable by using UNDELETABLE.
	* locale/C-ctype: Likewise.
	* locale/C-messages: Likewise.
	* locale/C-monetary: Likewise.
	* locale/C-numeric: Likewise.
	* locale/C-time: Likewise.
	* locale/findlocale.c (_nl_find_locale, _nl_remove_locale): Handle
	MAX_USAGE_COUNT and UNDELETABLE.
	(free_mem): New function.  Add it to __libc_subfreeres list.
	* locale/loadlocale.c: Define _nl_unload_locale function.

	* misc/hsearch.c: Register hdestroy in __libc_subfreeres list.

	* stdlib/fmtmsg.c (addseverity): Handle illegal severity arguments
	correctly
	Define free_mem function and add to __libc_subfreeres list.

	* locale/programs/localedef.c (options): short form os verbose is v.
	Reported by Andreas Jaeger.

	* misc/sys/select.h: Define pselect only is __USE_POSIX since this
	header is used in some others as well for historical reasons.

	* resolv/resolv.h: Include <netinet/in.h> to make self-contained.

	* string/bits/string2.h: Add missing braces and optimize strcmp a
	bit more.
	* sysdeps/i386/i486/bits/string.h: Likewise.

	* sunrpc/rpc/auth_des.h: Include rpc/auth.h to be self-contained.
	Pretty print.

	* sysdeps/mach/hurd/cthreads.c: Add copyright text.

	* sysdeps/unix/sysv/linux/syscalls.list: Correct prctl entry.

	* sysdeps/unix/sysv/linux/sys/mman.h: Get definition of size_t.

	* time/time.h: Pretty print.

1997-09-29  Paul Eggert  <eggert@twinsun.com>

	* time/strftime.c: Synchronize with GNU Emacs strftime.c.
	(HAVE_MEMCPY): Define if emacs is defined and HAVE_BCOPY isn't.
	(gmtime_r, localtime_r): Undef before defining.
	(iso_week_days): Use __inline__, not inline.

1997-09-27  Andreas Schwab  <schwab@issan.informatik.uni-dortmund.de>

	* sysdeps/m68k/fpu/bits/mathinline.h: Rename exp2{,l,f} to
	__ieee754_exp2{,l,f}.
	* sysdeps/m68k/fpu/s_exp2.c: Likewise.
	* sysdeps/m68k/fpu/s_exp2l.c: Likewise.
	* sysdeps/m68k/fpu/s_exp2f.c: Likewise.

1997-09-27  Andreas Schwab  <schwab@issan.informatik.uni-dortmund.de>

	* elf/soinit.c (__EH_FRAME_BEGIN__): Don't make the .eh_frame
	section read-only, it contains relocations.
	* elf/sofini.c (__FRAME_END__): Likewise.

1997-09-29 03:08  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/i386/i486/bits/string.h [__PIC__] (__strspn_cg, __strcspn_cg,
	__strpbrk_cg, __strstr_cg): Optimize even more.  No spill register
	needed.  Patch by NIIBE Yutaka <gniibe@mri.co.jp>.

1997-09-28 08:27  Thorsten Kukuk  <kukuk@vt.uni-paderborn.de>

	* nis/nis_call.c (__do_niscall2): Fix return code, add missing
	  break in switch case.

	* nis/nis_mkdir.c: Fix return codes to match Solaris version.
	* nis/nis_rmdir.c: Likewise.

	* nis/rpcsvc/yp_prot.h: Rename struct keydat to struct keydat_t
	for C++.

1997-09-28 04:32  Ulrich Drepper  <drepper@cygnus.com>

	* configure.in: Fix typo.
	Patch by Zack Weinberg <zack@rabi.phys.columbia.edu>.

1997-09-25 20:14  Philip Blundell  <Philip.Blundell@pobox.com>

	* sysdeps/unix/sysv/linux/scsi/sg.h: New file.
	* sysdeps/unix/sysv/linux/Makefile: Install <scsi/sg.h>.
1997-09-30 17:10:40 +00:00
..
68020.gcc Update. 1997-08-27 20:26:10 +00:00
alpha.dec Update. 1997-08-27 20:26:10 +00:00
alpha.gcc Update. 1997-08-27 20:26:10 +00:00
mutex.c Update. 1997-09-30 17:10:40 +00:00
parisc.gcc Update. 1997-08-27 20:26:10 +00:00
parisc.hp Update. 1997-08-27 20:26:10 +00:00
README Update. 1997-08-27 20:26:10 +00:00
sparc.gcc Update. 1997-08-27 20:26:10 +00:00
uts4.cc.s Update. 1997-08-27 20:26:10 +00:00
x86.gcc Update. 1997-09-05 01:43:20 +00:00

# @(#)README	10.1 (Sleepycat) 4/12/97

Resource locking routines: lock based on a db_mutex_t.  All this gunk
(including trying to make assembly code portable), is necessary because
System V semaphores require system calls for uncontested locks and we
don't want to make two system calls per resource lock.

First, this is how it works.  The db_mutex_t structure contains a resource
test-and-set lock (tsl), a file offset, a pid for debugging and statistics
information.

If HAVE_SPINLOCKS is defined (i.e. we know how to do test-and-sets for
this compiler/architecture combination), we try and lock the resource tsl
TSL_DEFAULT_SPINS times.  If we can't acquire the lock that way, we use
a system call to sleep for 10ms, 20ms, 40ms, etc.  (The time is bounded
at 1 second, just in case.)  Using the timer backoff means that there are
two assumptions: that locks are held for brief periods (never over system
calls or I/O) and that locks are not hotly contested.

If HAVE_SPINLOCKS is not defined, i.e. we can't do test-and-sets, we use
a file descriptor to do byte locking on a file at a specified offset.  In
this case, ALL of the locking is done in the kernel.  Because file
descriptors are allocated per process, we have to provide the file
descriptor as part of the lock/unlock call.  We still have to do timer
backoff because we need to be able to block ourselves, i.e. the lock
manager causes processes to wait by having the process acquire a mutex
and then attempting to re-acquire the mutex.  There's no way to use kernel
locking to block yourself, i.e. if you hold a lock and attempt to
re-acquire it, the attempt will succeed.

Next, let's talk about why it doesn't work the way a reasonable person
would think it should work.

Ideally, we'd have the ability to try to lock the resource tsl, and if
that fails, increment a counter of waiting processes, then block in the
kernel until the tsl is released.  The process holding the resource tsl
would see the wait counter when it went to release the resource tsl, and
would wake any waiting processes up after releasing the lock.  This would
actually require both another tsl (call it the mutex tsl) and
synchronization between the call that blocks in the kernel and the actual
resource tsl.  The mutex tsl would be used to protect accesses to the
db_mutex_t itself.  Locking the mutex tsl would be done by a busy loop,
which is safe because processes would never block holding that tsl (all
they would do is try to obtain the resource tsl and set/check the wait
count).  The problem in this model is that the blocking call into the
kernel requires a blocking semaphore, i.e. one whose normal state is
locked.

The only portable forms of locking under UNIX are fcntl(2) on a file
descriptor/offset, and System V semaphores.  Neither of these locking
methods are sufficient to solve the problem.

The problem with fcntl locking is that only the process that obtained the
lock can release it.  Remember, we want the normal state of the kernel
semaphore to be locked.  So, if the creator of the db_mutex_t were to
initialize the lock to "locked", then a second process locks the resource
tsl, and then a third process needs to block, waiting for the resource
tsl, when the second process wants to wake up the third process, it can't
because it's not the holder of the lock!  For the second process to be
the holder of the lock, we would have to make a system call per
uncontested lock, which is what we were trying to get away from in the
first place.

There are some hybrid schemes, such as signaling the holder of the lock,
or using a different blocking offset depending on which process is
holding the lock, but it gets complicated fairly quickly.  I'm open to
suggestions, but I'm not holding my breath.

Regardless, we use this form of locking when HAVE_SPINLOCKS is not
defined, (i.e. we're locking in the kernel) because it doesn't have the
limitations found in System V semaphores, and because the normal state of
the kernel object in that case is unlocked, so the process releasing the
lock is also the holder of the lock.

The System V semaphore design has a number of other limitations that make
it inappropriate for this task.  Namely:

First, the semaphore key name space is separate from the file system name
space (although there exist methods for using file names to create
semaphore keys).  If we use a well-known key, there's no reason to believe
that any particular key will not already be in use, either by another
instance of the DB application or some other application, in which case
the DB application will fail.  If we create a key, then we have to use a
file system name to rendezvous and pass around the key.

Second, System V semaphores traditionally have compile-time, system-wide
limits on the number of semaphore keys that you can have.  Typically, that
number is far too low for any practical purpose.  Since the semaphores
permit more than a single slot per semaphore key, we could try and get
around that limit by using multiple slots, but that means that the file
that we're using for rendezvous is going to have to contain slot
information as well as semaphore key information, and we're going to be
reading/writing it on every db_mutex_t init or destroy operation.  Anyhow,
similar compile-time, system-wide limits on the numbers of slots per
semaphore key kick in, and you're right back where you started.

My fantasy is that once POSIX.1 standard mutexes are in wide-spread use,
we can switch to them.  My guess is that it won't happen, because the
POSIX semaphores are only required to work for threads within a process,
and not independent processes.

Note: there are races in the statistics code, but since it's just that,
I didn't bother fixing them.  (The fix requires a mutex tsl, so, when/if
this code is fixed to do rational locking (see above), then change the
statistics update code to acquire/release the mutex tsl.