Mercurial > hg > audiodb
annotate lock.cpp @ 507:e7fd50483311
Free bits of the datum constructed in audioDB::query.
We're not quite safe: error calls between allocation of some of these
bits and pieces and their use will cause failure... but not freeing
things here is definitely wrong.
author | mas01cr |
---|---|
date | Tue, 13 Jan 2009 21:37:10 +0000 |
parents | 342822c2d49a |
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 } |