[ovs-dev] [threads 04/23] lockfile: Make thread-safe.

Alex Wang alexw at nicira.com
Mon Jul 22 16:17:37 UTC 2013


Looks good to me,

Also, for confirmation, we do not need to guarantee the thread safety
of coverage counters, since they are not meant to be perfectly accurate,
right?

Thanks,


On Thu, Jul 18, 2013 at 4:15 PM, Ben Pfaff <blp at nicira.com> wrote:

> Signed-off-by: Ben Pfaff <blp at nicira.com>
> ---
>  lib/lockfile.c |   11 ++++++++++-
>  1 files changed, 10 insertions(+), 1 deletions(-)
>
> diff --git a/lib/lockfile.c b/lib/lockfile.c
> index 14e553d..50a4e0c 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);
>      }
> --
> 1.7.2.5
>
> _______________________________________________
> dev mailing list
> dev at openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openvswitch.org/pipermail/ovs-dev/attachments/20130722/27b541d3/attachment-0003.html>


More information about the dev mailing list