#include "coverage.h"
#include "hash.h"
#include "hmap.h"
+#include "ovs-thread.h"
#include "timeval.h"
#include "util.h"
#include "vlog.h"
VLOG_DEFINE_THIS_MODULE(lockfile);
COVERAGE_DEFINE(lockfile_lock);
-COVERAGE_DEFINE(lockfile_timeout);
COVERAGE_DEFINE(lockfile_error);
COVERAGE_DEFINE(lockfile_unlock);
* descriptor for a file on which a process holds a lock drops *all* locks on
* that file. That means that we can't afford to open a lockfile more than
* once. */
-static struct hmap lock_table = HMAP_INITIALIZER(&lock_table);
+static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER;
+static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__);
+static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex)
+ = &lock_table__;
static void lockfile_unhash(struct lockfile *);
static int lockfile_try_lock(const char *name, pid_t *pidp,
lock_name = lockfile_name(file);
+ ovs_mutex_lock(&lock_table_mutex);
error = lockfile_try_lock(lock_name, &pid, lockfilep);
+ ovs_mutex_unlock(&lock_table_mutex);
if (error) {
COVERAGE_INC(lockfile_error);
lockfile_unlock(struct lockfile *lockfile)
{
if (lockfile) {
- COVERAGE_INC(lockfile_unlock);
+ ovs_mutex_lock(&lock_table_mutex);
lockfile_unhash(lockfile);
+ ovs_mutex_unlock(&lock_table_mutex);
+
+ COVERAGE_INC(lockfile_unlock);
free(lockfile->name);
free(lockfile);
}
{
struct lockfile *lockfile;
- HMAP_FOR_EACH (lockfile, hmap_node, &lock_table) {
+ ovs_mutex_lock(&lock_table_mutex);
+ HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
if (lockfile->fd >= 0) {
VLOG_WARN("%s: child does not inherit lock", lockfile->name);
lockfile_unhash(lockfile);
}
}
+ ovs_mutex_unlock(&lock_table_mutex);
}
\f
static uint32_t
}
static struct lockfile *
-lockfile_find(dev_t device, ino_t inode)
+lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex)
{
struct lockfile *lockfile;
HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
- lockfile_hash(device, inode), &lock_table) {
+ lockfile_hash(device, inode), lock_table) {
if (lockfile->device == device && lockfile->inode == inode) {
return lockfile;
}
}
static void
-lockfile_unhash(struct lockfile *lockfile)
+lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex)
{
if (lockfile->fd >= 0) {
close(lockfile->fd);
lockfile->fd = -1;
- hmap_remove(&lock_table, &lockfile->hmap_node);
+ hmap_remove(lock_table, &lockfile->hmap_node);
}
}
static struct lockfile *
lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
+ OVS_REQUIRES(&lock_table_mutex)
{
struct lockfile *lockfile;
lockfile->device = device;
lockfile->inode = inode;
lockfile->fd = fd;
- hmap_insert(&lock_table, &lockfile->hmap_node,
+ hmap_insert(lock_table, &lockfile->hmap_node,
lockfile_hash(device, inode));
return lockfile;
}
static int
lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
+ OVS_REQUIRES(&lock_table_mutex)
{
struct flock l;
struct stat s;