summaryrefslogtreecommitdiff
path: root/lib/lockfile.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2013-04-15 15:05:19 -0700
committerBen Pfaff <blp@nicira.com>2013-07-22 10:53:08 -0700
commit5947bbe6094a68814f8da8045d170ca03c88fe26 (patch)
tree70c0fb5be803c30f471d217ee67216eabe901759 /lib/lockfile.c
parentd7eea710a02193d6384174fa3f3e7d10d5248344 (diff)
downloadopenvswitch-5947bbe6094a68814f8da8045d170ca03c88fe26.tar.gz
lockfile: Make thread-safe.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib/lockfile.c')
-rw-r--r--lib/lockfile.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/lockfile.c b/lib/lockfile.c
index 14e553d29..50a4e0c07 100644
--- a/lib/lockfile.c
+++ b/lib/lockfile.c
@@ -27,6 +27,7 @@
#include "coverage.h"
#include "hash.h"
#include "hmap.h"
+#include "ovs-thread.h"
#include "timeval.h"
#include "util.h"
#include "vlog.h"
@@ -54,6 +55,9 @@ struct lockfile {
* 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);
@@ -106,7 +110,9 @@ lockfile_lock(const char *file, 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);
@@ -132,8 +138,11 @@ void
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);
}