comparison src/lib/file-dotlock.c @ 5938:805f2527a982 HEAD

If use_io_notify dotlock setting is enabled, use I/O notify loop for waiting dotlock deletion instead of just sleeping randomly.
author Timo Sirainen <tss@iki.fi>
date Mon, 09 Jul 2007 07:01:08 +0300
parents b796701a6927
children 29770d8a013b
comparison
equal deleted inserted replaced
5937:772f4e8bd2a9 5938:805f2527a982
1 /* Copyright (C) 2003 Timo Sirainen */ 1 /* Copyright (C) 2003 Timo Sirainen */
2 2
3 #include "lib.h" 3 #include "lib.h"
4 #include "ioloop.h"
4 #include "str.h" 5 #include "str.h"
5 #include "hex-binary.h" 6 #include "hex-binary.h"
6 #include "hostpid.h" 7 #include "hostpid.h"
7 #include "randgen.h" 8 #include "randgen.h"
8 #include "write-full.h" 9 #include "write-full.h"
56 int fd; 57 int fd;
57 58
58 struct file_change_info lock_info; 59 struct file_change_info lock_info;
59 struct file_change_info file_info; 60 struct file_change_info file_info;
60 61
61 bool have_pid; 62 bool have_pid, use_io_notify;
62 time_t last_pid_check; 63 time_t last_pid_check;
63 time_t last_change; 64 time_t last_change;
64 }; 65 };
65 66
66 static struct dotlock * 67 static struct dotlock *
361 362
362 lock_info->fd = fd; 363 lock_info->fd = fd;
363 return 1; 364 return 1;
364 } 365 }
365 366
367 static void dotlock_wait_end(struct ioloop *ioloop)
368 {
369 io_loop_stop(ioloop);
370 }
371
372 static void dotlock_wait(struct lock_info *lock_info)
373 {
374 struct ioloop *ioloop;
375 struct io *io;
376 struct timeout *to;
377
378 if (!lock_info->use_io_notify) {
379 usleep(LOCK_RANDOM_USLEEP_TIME);
380 return;
381 }
382
383 ioloop = io_loop_create();
384 switch (io_add_notify(lock_info->lock_path, dotlock_wait_end,
385 ioloop, &io)) {
386 case IO_NOTIFY_ADDED:
387 break;
388 case IO_NOTIFY_NOTFOUND:
389 /* the lock file doesn't exist anymore, don't sleep */
390 io_loop_destroy(&ioloop);
391 return;
392 case IO_NOTIFY_DISABLED:
393 /* listening for files not supported */
394 io_loop_destroy(&ioloop);
395 lock_info->use_io_notify = FALSE;
396 usleep(LOCK_RANDOM_USLEEP_TIME);
397 return;
398 }
399 to = timeout_add(LOCK_RANDOM_USLEEP_TIME/1000,
400 dotlock_wait_end, ioloop);
401 io_loop_run(ioloop);
402 io_remove(&io);
403 timeout_remove(&to);
404 io_loop_destroy(&ioloop);
405 }
406
366 static int dotlock_create(const char *path, struct dotlock *dotlock, 407 static int dotlock_create(const char *path, struct dotlock *dotlock,
367 enum dotlock_create_flags flags, bool write_pid) 408 enum dotlock_create_flags flags, bool write_pid)
368 { 409 {
369 const struct dotlock_settings *set = &dotlock->settings; 410 const struct dotlock_settings *set = &dotlock->settings;
370 const char *lock_path; 411 const char *lock_path;
388 memset(&lock_info, 0, sizeof(lock_info)); 429 memset(&lock_info, 0, sizeof(lock_info));
389 lock_info.path = path; 430 lock_info.path = path;
390 lock_info.set = set; 431 lock_info.set = set;
391 lock_info.lock_path = lock_path; 432 lock_info.lock_path = lock_path;
392 lock_info.fd = -1; 433 lock_info.fd = -1;
393 434 lock_info.use_io_notify = set->use_io_notify;
435 ;
394 last_notify = 0; do_wait = FALSE; 436 last_notify = 0; do_wait = FALSE;
395 437
396 do { 438 do {
397 if (do_wait) { 439 if (do_wait) {
398 usleep(LOCK_RANDOM_USLEEP_TIME); 440 dotlock_wait(&lock_info);
399 do_wait = FALSE; 441 do_wait = FALSE;
400 } 442 }
401 443
402 ret = check_lock(now, &lock_info); 444 ret = check_lock(now, &lock_info);
403 if (ret < 0) 445 if (ret < 0)