- /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "coverage.h"
#include "hash.h"
#include "hmap.h"
+#include "ovs-thread.h"
#include "timeval.h"
#include "util.h"
#include "vlog.h"
* once. */
static struct hmap lock_table = HMAP_INITIALIZER(&lock_table);
+/* Protects 'lock_table'. */
+static pthread_mutex_t lock_table_mutex = PTHREAD_MUTEX_INITIALIZER;
+
static void lockfile_unhash(struct lockfile *);
static int lockfile_try_lock(const char *name, pid_t *pidp,
struct lockfile **lockfilep);
lock_name = lockfile_name(file);
+ xpthread_mutex_lock(&lock_table_mutex);
error = lockfile_try_lock(lock_name, &pid, lockfilep);
+ xpthread_mutex_unlock(&lock_table_mutex);
if (error) {
COVERAGE_INC(lockfile_error);
"pid %ld", lock_name, (long int) pid);
} else {
VLOG_WARN("%s: failed to lock file: %s",
- lock_name, strerror(error));
+ lock_name, ovs_strerror(error));
}
}
lockfile_unlock(struct lockfile *lockfile)
{
if (lockfile) {
- COVERAGE_INC(lockfile_unlock);
+ xpthread_mutex_lock(&lock_table_mutex);
lockfile_unhash(lockfile);
+ xpthread_mutex_unlock(&lock_table_mutex);
+
+ COVERAGE_INC(lockfile_unlock);
free(lockfile->name);
free(lockfile);
}
}
} else if (errno != ENOENT) {
VLOG_WARN("%s: failed to stat lock file: %s",
- name, strerror(errno));
+ name, ovs_strerror(errno));
return errno;
}
fd = open(name, O_RDWR | O_CREAT, 0600);
if (fd < 0) {
VLOG_WARN("%s: failed to open lock file: %s",
- name, strerror(errno));
+ name, ovs_strerror(errno));
return errno;
}
/* Get the inode and device number for the lock table. */
if (fstat(fd, &s)) {
- VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno));
+ VLOG_ERR("%s: failed to fstat lock file: %s",
+ name, ovs_strerror(errno));
close(fd);
return errno;
}
l.l_start = 0;
l.l_len = 0;
- time_disable_restart();
error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
- time_enable_restart();
if (!error) {
*lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);