timeout_secs, lock_r);
}
-static int mail_index_lock(struct mail_index *index, int lock_type,
+static int mail_index_lock(struct mail_index *index,
unsigned int timeout_secs, unsigned int *lock_id_r)
{
int ret;
- i_assert(lock_type == F_RDLCK || lock_type == F_WRLCK);
-
- if (lock_type == F_RDLCK && index->lock_type != F_UNLCK) {
+ if (index->lock_type != F_UNLCK) {
+ /* file is already locked */
index->shared_lock_count++;
*lock_id_r = index->lock_id_counter;
- ret = 1;
- } else if (lock_type == F_WRLCK && index->lock_type == F_WRLCK) {
- index->excl_lock_count++;
- *lock_id_r = index->lock_id_counter + 1;
- 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 */
- if (lock_type == F_WRLCK)
- return 0;
-
index->shared_lock_count++;
index->lock_type = F_RDLCK;
*lock_id_r = index->lock_id_counter;
return 1;
}
- if (index->file_lock == NULL) {
- i_assert(index->lock_type == F_UNLCK);
- ret = mail_index_lock_fd(index, index->filepath, index->fd,
- lock_type, timeout_secs,
- &index->file_lock);
- } else {
- i_assert(index->lock_type == F_RDLCK && lock_type == F_WRLCK);
- ret = file_lock_try_update(index->file_lock, lock_type);
- }
+ i_assert(index->lock_type == F_UNLCK);
+ ret = mail_index_lock_fd(index, index->filepath, index->fd,
+ F_RDLCK, timeout_secs,
+ &index->file_lock);
if (ret <= 0)
return ret;
if (index->lock_type == F_UNLCK)
index->lock_id_counter += 2;
- index->lock_type = lock_type;
-
- if (lock_type == F_RDLCK) {
- index->shared_lock_count++;
- *lock_id_r = index->lock_id_counter;
- } else {
- index->excl_lock_count++;
- *lock_id_r = index->lock_id_counter + 1;
- }
+ index->lock_type = F_RDLCK;
+ index->shared_lock_count++;
+ *lock_id_r = index->lock_id_counter;
return 1;
}
timeout_secs = I_MIN(MAIL_INDEX_SHARED_LOCK_TIMEOUT,
index->max_lock_timeout_secs);
- ret = mail_index_lock(index, F_RDLCK, timeout_secs, lock_id_r);
+ ret = mail_index_lock(index, timeout_secs, lock_id_r);
if (ret > 0) {
mail_index_flush_read_cache(index, index->filepath,
index->fd, TRUE);
return -1;
}
-int mail_index_try_lock_exclusive(struct mail_index *index,
- unsigned int *lock_id_r)
-{
- int ret;
-
- if ((ret = mail_index_lock(index, F_WRLCK, 0, lock_id_r)) > 0) {
- mail_index_flush_read_cache(index, index->filepath,
- index->fd, TRUE);
- }
- return ret;
-}
-
void mail_index_unlock(struct mail_index *index, unsigned int *_lock_id)
{
unsigned int lock_id = *_lock_id;
*_lock_id = 0;
- if ((lock_id & 1) == 0) {
- /* shared lock */
- if (!mail_index_is_locked(index, lock_id)) {
- /* unlocking some older generation of the index file.
- we've already closed the file so just ignore this. */
- return;
- }
-
- i_assert(index->shared_lock_count > 0);
- index->shared_lock_count--;
- } else {
- /* exclusive lock */
- i_assert(lock_id == index->lock_id_counter + 1);
- i_assert(index->excl_lock_count > 0);
- i_assert(index->lock_type == F_WRLCK);
- if (--index->excl_lock_count == 0 &&
- index->shared_lock_count > 0) {
- /* drop back to a shared lock. */
- index->lock_type = F_RDLCK;
- (void)file_lock_try_update(index->file_lock, F_RDLCK);
- }
+ /* shared lock */
+ if (!mail_index_is_locked(index, lock_id)) {
+ /* unlocking some older generation of the index file.
+ we've already closed the file so just ignore this. */
+ return;
}
- if (index->shared_lock_count == 0 && index->excl_lock_count == 0) {
+ i_assert(index->shared_lock_count > 0);
+ index->shared_lock_count--;
+
+ if (index->shared_lock_count == 0) {
index->lock_id_counter += 2;
index->lock_type = F_UNLCK;
if (index->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
/* syncing will update this if non-NULL */
struct mail_index_transaction_commit_result *sync_commit_result;
- int lock_type, shared_lock_count, excl_lock_count;
+ int lock_type, shared_lock_count;
unsigned int lock_id_counter;
enum file_lock_method lock_method;
unsigned int max_lock_timeout_secs;
/* Returns 0 = ok, -1 = error. */
int mail_index_lock_shared(struct mail_index *index, unsigned int *lock_id_r);
-/* Returns 1 = ok, 0 = already locked, -1 = error. */
-int mail_index_try_lock_exclusive(struct mail_index *index,
- unsigned int *lock_id_r);
void mail_index_unlock(struct mail_index *index, unsigned int *lock_id);
/* Returns TRUE if given lock_id is valid. */
bool mail_index_is_locked(struct mail_index *index, unsigned int lock_id);