Mercurial > hg > audiodb
annotate lock.cpp @ 498:342822c2d49a
Merge api-inversion branch (-r656:771, but I don't expect to return to
that branch) into the trunk.
I expect there to be minor performance regressions (e.g. in the SOAP
server index cacheing, which I have forcibly removed) and minor
unplugged memory leaks (e.g. in audioDB::query(), where I don't free up
the datum). I hope that these leaks and performance regressions can be
plugged in short order. I also expect that some (but maybe not all) of
the issues currently addressed in the memory-leaks branch are superseded
or fixed by this merge.
There remains much work to be done; go forth and do it.
author | mas01cr |
---|---|
date | Sat, 10 Jan 2009 16:47:57 +0000 |
parents | |
children | cc2b97d020b1 |
rev | line source |
---|---|
mas01cr@498 | 1 #include "audioDB.h" |
mas01cr@498 | 2 |
mas01cr@498 | 3 int acquire_lock(int fd, bool exclusive) { |
mas01cr@498 | 4 struct flock lock; |
mas01cr@498 | 5 int status; |
mas01cr@498 | 6 |
mas01cr@498 | 7 lock.l_type = exclusive ? F_WRLCK : F_RDLCK; |
mas01cr@498 | 8 lock.l_whence = SEEK_SET; |
mas01cr@498 | 9 lock.l_start = 0; |
mas01cr@498 | 10 lock.l_len = 0; /* "the whole file" */ |
mas01cr@498 | 11 |
mas01cr@498 | 12 retry: |
mas01cr@498 | 13 do { |
mas01cr@498 | 14 status = fcntl(fd, F_SETLKW, &lock); |
mas01cr@498 | 15 } while (status != 0 && errno == EINTR); |
mas01cr@498 | 16 |
mas01cr@498 | 17 if (status) { |
mas01cr@498 | 18 if (errno == EAGAIN) { |
mas01cr@498 | 19 sleep(1); |
mas01cr@498 | 20 goto retry; |
mas01cr@498 | 21 } else { |
mas01cr@498 | 22 return status; |
mas01cr@498 | 23 } |
mas01cr@498 | 24 } |
mas01cr@498 | 25 return 0; |
mas01cr@498 | 26 } |
mas01cr@498 | 27 |
mas01cr@498 | 28 int divest_lock(int fd) { |
mas01cr@498 | 29 struct flock lock; |
mas01cr@498 | 30 |
mas01cr@498 | 31 lock.l_type = F_UNLCK; |
mas01cr@498 | 32 lock.l_whence = SEEK_SET; |
mas01cr@498 | 33 lock.l_start = 0; |
mas01cr@498 | 34 lock.l_len = 0; |
mas01cr@498 | 35 |
mas01cr@498 | 36 return fcntl(fd, F_SETLKW, &lock); |
mas01cr@498 | 37 } |