annotate lock.cpp @ 509:cc2b97d020b1

Code rearrangements to tease apart library code from C++ audioDB code. There should be precisely no functional changes in this commit. Instead, the only thing that has happened is that all the abstraction violation and other horribleness is concentrated in one place: the include of "audioDB-internals.h" in audioDB.h -- the separation will be complete once that include can be removed. This include is necessary because the command-line binary / SOAP server still does some things directly rather than through an API: not least of which the operations that have not yet been integrated into the API yet, but also some messing around with constants, flags and nominally internal functions. The intent is to remove as many of these as possible and think quite hard about the rest. In the meantime, the library is now much more self-contained: the only things it uses are in the audioDB_API.h and audioDB-internals.h headers; thus there are fewer nasty surprises lurking for readers of the code. The Makefile has been adjusted to take advantage of this rearrangement in the dependencies.
author mas01cr
date Thu, 15 Jan 2009 13:57:33 +0000
parents 342822c2d49a
children 4eedc18634f5
rev   line source
mas01cr@509 1 extern "C" {
mas01cr@509 2 #include "audioDB_API.h"
mas01cr@509 3 }
mas01cr@509 4 #include "audioDB-internals.h"
mas01cr@498 5
mas01cr@498 6 int acquire_lock(int fd, bool exclusive) {
mas01cr@498 7 struct flock lock;
mas01cr@498 8 int status;
mas01cr@498 9
mas01cr@498 10 lock.l_type = exclusive ? F_WRLCK : F_RDLCK;
mas01cr@498 11 lock.l_whence = SEEK_SET;
mas01cr@498 12 lock.l_start = 0;
mas01cr@498 13 lock.l_len = 0; /* "the whole file" */
mas01cr@498 14
mas01cr@498 15 retry:
mas01cr@498 16 do {
mas01cr@498 17 status = fcntl(fd, F_SETLKW, &lock);
mas01cr@498 18 } while (status != 0 && errno == EINTR);
mas01cr@498 19
mas01cr@498 20 if (status) {
mas01cr@498 21 if (errno == EAGAIN) {
mas01cr@498 22 sleep(1);
mas01cr@498 23 goto retry;
mas01cr@498 24 } else {
mas01cr@498 25 return status;
mas01cr@498 26 }
mas01cr@498 27 }
mas01cr@498 28 return 0;
mas01cr@498 29 }
mas01cr@498 30
mas01cr@498 31 int divest_lock(int fd) {
mas01cr@498 32 struct flock lock;
mas01cr@498 33
mas01cr@498 34 lock.l_type = F_UNLCK;
mas01cr@498 35 lock.l_whence = SEEK_SET;
mas01cr@498 36 lock.l_start = 0;
mas01cr@498 37 lock.l_len = 0;
mas01cr@498 38
mas01cr@498 39 return fcntl(fd, F_SETLKW, &lock);
mas01cr@498 40 }