Mercurial > hg > audiodb
annotate lock.cpp @ 497:9d8aee621afb api-inversion
More libtests fixups.
Include audiodb_close() calls everywhere (whoops).
Add the facility to run tests under valgrind. Unfortunately the
error-exitcode flag doesn't actually cause an error exit if the only
thing wrong is memory leaks, but it will if there are actual memory
errors, which is a start.
author | mas01cr |
---|---|
date | Sat, 10 Jan 2009 16:07:43 +0000 |
parents | 8fb85fbcaba6 |
children | cc2b97d020b1 |
rev | line source |
---|---|
mas01cr@496 | 1 #include "audioDB.h" |
mas01cr@496 | 2 |
mas01cr@496 | 3 int acquire_lock(int fd, bool exclusive) { |
mas01cr@496 | 4 struct flock lock; |
mas01cr@496 | 5 int status; |
mas01cr@496 | 6 |
mas01cr@496 | 7 lock.l_type = exclusive ? F_WRLCK : F_RDLCK; |
mas01cr@496 | 8 lock.l_whence = SEEK_SET; |
mas01cr@496 | 9 lock.l_start = 0; |
mas01cr@496 | 10 lock.l_len = 0; /* "the whole file" */ |
mas01cr@496 | 11 |
mas01cr@496 | 12 retry: |
mas01cr@496 | 13 do { |
mas01cr@496 | 14 status = fcntl(fd, F_SETLKW, &lock); |
mas01cr@496 | 15 } while (status != 0 && errno == EINTR); |
mas01cr@496 | 16 |
mas01cr@496 | 17 if (status) { |
mas01cr@496 | 18 if (errno == EAGAIN) { |
mas01cr@496 | 19 sleep(1); |
mas01cr@496 | 20 goto retry; |
mas01cr@496 | 21 } else { |
mas01cr@496 | 22 return status; |
mas01cr@496 | 23 } |
mas01cr@496 | 24 } |
mas01cr@496 | 25 return 0; |
mas01cr@496 | 26 } |
mas01cr@496 | 27 |
mas01cr@496 | 28 int divest_lock(int fd) { |
mas01cr@496 | 29 struct flock lock; |
mas01cr@496 | 30 |
mas01cr@496 | 31 lock.l_type = F_UNLCK; |
mas01cr@496 | 32 lock.l_whence = SEEK_SET; |
mas01cr@496 | 33 lock.l_start = 0; |
mas01cr@496 | 34 lock.l_len = 0; |
mas01cr@496 | 35 |
mas01cr@496 | 36 return fcntl(fd, F_SETLKW, &lock); |
mas01cr@496 | 37 } |