mail-index-lock.c revision cc833a7a4e2258afdc834ace4bfe6579820a1df3
/* Copyright (C) 2003-2004 Timo Sirainen */
/*
Locking should never fail or timeout. Exclusive locks must be kept as short
time as possible. Shared locks can be long living, so if we can't get
exclusive lock directly, we'll recreate the index. That means the shared
lock holders can keep using the old file.
lock_id is used to figure out if acquired lock is still valid. When index
file is reopened, the lock_id can become invalid. It doesn't matter however,
as no-one's going to modify the old file anymore.
lock_id also tells us if we're referring to a shared or an exclusive lock.
This allows us to drop back to shared locking once all exclusive locks
are dropped. Shared locks have even numbers, exclusive locks have odd numbers.
The number is increased by two every time the lock is dropped or index file
is reopened.
*/
#include "lib.h"
#include "nfs-workarounds.h"
#include "mail-index-private.h"
#define MAIL_INDEX_SHARED_LOCK_TIMEOUT 120
int lock_type, unsigned int timeout_secs,
{
if (fd == -1) {
return 1;
}
}
unsigned int timeout_secs, unsigned int *lock_id_r)
{
int ret;
ret = 1;
index->excl_lock_count++;
ret = 1;
} else {
ret = 0;
}
if (ret > 0) {
/* file is already locked */
return 1;
}
/* FIXME: exclusive locking will rewrite the index file every
time. shouldn't really be needed.. reading doesn't require
locks then, though */
return 0;
return 1;
}
} else {
/* this is kind of kludgy. we wish to avoid deadlocks while
trying to lock transaction log, but it can happen if our
process is holding transaction log lock and waiting for
index write lock, while the other process is holding index
read lock and waiting for transaction log lock.
we don't have a problem with grabbing read index lock
because the only way for it to block is if it's
write-locked, which isn't allowed unless transaction log
is also locked.
so, the workaround for this problem is that we simply try
locking once. if it doesn't work, just rewrite the file.
hopefully there won't be any other deadlocking issues. :) */
} else {
}
}
if (ret <= 0)
return ret;
} else {
index->excl_lock_count++;
}
return 1;
}
{
return;
/* Assume flock() is independent of fcntl() locks. This isn't true
with Linux 2.6 NFS, but with it there's no point in using flock() */
} else {
}
}
{
int ret;
if (ret > 0) {
return 0;
}
if (ret < 0)
return -1;
"Timeout while waiting for shared lock for index file %s",
return -1;
}
unsigned int *lock_id_r)
{
int ret;
return ret;
}
{
*_lock_id = 0;
if ((lock_id & 1) == 0) {
/* shared lock */
/* unlocking some older generation of the index file.
we've already closed the file so just ignore this. */
return;
}
} else {
/* exclusive lock */
if (--index->excl_lock_count == 0 &&
index->shared_lock_count > 0) {
/* drop back to a shared lock. */
}
}
if (!MAIL_INDEX_IS_IN_MEMORY(index))
}
}
}
{
return TRUE;
}
return FALSE;
}