Mercurial > hg > audiodb
comparison audioDB.cpp @ 30:657e638b87ef
Add fcntl() locking to the database file handle.
New procedures audioDB::get_lock() and audioDB::release_lock(), the
latter of which is unused because we rely on the close() call in the
destructor to release our locks.
author | mas01cr |
---|---|
date | Tue, 21 Aug 2007 16:59:33 +0000 |
parents | 0357114be285 |
children | 9503faa2981c |
comparison
equal
deleted
inserted
replaced
27:0357114be285 | 30:657e638b87ef |
---|---|
425 -------------------------------------------------------------------------- | 425 -------------------------------------------------------------------------- |
426 O2_MAXFILES * 02_MEANNUMFEATURES * sizeof(DOUBLE) | 426 O2_MAXFILES * 02_MEANNUMFEATURES * sizeof(DOUBLE) |
427 | 427 |
428 */ | 428 */ |
429 | 429 |
430 void audioDB::get_lock(int fd, bool exclusive) { | |
431 struct flock lock; | |
432 int status; | |
433 | |
434 lock.l_type = exclusive ? F_WRLCK : F_RDLCK; | |
435 lock.l_whence = SEEK_SET; | |
436 lock.l_start = 0; | |
437 lock.l_len = 0; /* "the whole file" */ | |
438 | |
439 retry: | |
440 do { | |
441 status = fcntl(fd, F_SETLKW, &lock); | |
442 } while (status != 0 && errno == EINTR); | |
443 | |
444 if (status) { | |
445 if (errno == EAGAIN) { | |
446 sleep(1); | |
447 goto retry; | |
448 } else { | |
449 error("fcntl lock error"); | |
450 } | |
451 } | |
452 } | |
453 | |
454 void audioDB::release_lock(int fd) { | |
455 struct flock lock; | |
456 int status; | |
457 | |
458 lock.l_type = F_UNLCK; | |
459 lock.l_whence = SEEK_SET; | |
460 lock.l_start = 0; | |
461 lock.l_len = 0; | |
462 | |
463 status = fcntl(fd, F_SETLKW, &lock); | |
464 | |
465 if (status) | |
466 error("fcntl unlock error"); | |
467 } | |
468 | |
430 void audioDB::create(const char* dbName){ | 469 void audioDB::create(const char* dbName){ |
431 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) | 470 if ((dbfid = open (dbName, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) |
432 error("Can't open database file", dbName); | 471 error("Can't open database file", dbName); |
472 get_lock(dbfid, 1); | |
433 | 473 |
434 // go to the location corresponding to the last byte | 474 // go to the location corresponding to the last byte |
435 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1) | 475 if (lseek (dbfid, O2_DEFAULTDBSIZE - 1, SEEK_SET) == -1) |
436 error("lseek error in db file"); | 476 error("lseek error in db file"); |
437 | 477 |
471 // initTables - memory map files passed as arguments | 511 // initTables - memory map files passed as arguments |
472 // Precondition: database has already been created | 512 // Precondition: database has already been created |
473 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile=0){ | 513 void audioDB::initTables(const char* dbName, bool forWrite, const char* inFile=0){ |
474 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0) | 514 if ((dbfid = open (dbName, forWrite ? O_RDWR : O_RDONLY)) < 0) |
475 error("Can't open database file", dbName); | 515 error("Can't open database file", dbName); |
476 | 516 get_lock(dbfid, forWrite); |
517 | |
477 // open the input file | 518 // open the input file |
478 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) | 519 if (inFile && (infid = open (inFile, O_RDONLY)) < 0) |
479 error("can't open input file for reading", inFile); | 520 error("can't open input file for reading", inFile); |
480 | 521 |
481 // find size of input file | 522 // find size of input file |
665 | 706 |
666 void audioDB::batchinsert(const char* dbName, const char* inFile){ | 707 void audioDB::batchinsert(const char* dbName, const char* inFile){ |
667 | 708 |
668 if ((dbfid = open (dbName, O_RDWR)) < 0) | 709 if ((dbfid = open (dbName, O_RDWR)) < 0) |
669 error("Can't open database file", dbName); | 710 error("Can't open database file", dbName); |
711 get_lock(dbfid, 1); | |
670 | 712 |
671 if(!key) | 713 if(!key) |
672 key=inFile; | 714 key=inFile; |
673 ifstream *filesIn = 0; | 715 ifstream *filesIn = 0; |
674 ifstream *keysIn = 0; | 716 ifstream *keysIn = 0; |