netdev-dummy: Make counter thread-safe.
authorBen Pfaff <blp@nicira.com>
Fri, 26 Apr 2013 19:58:14 +0000 (12:58 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 23 Jul 2013 18:38:08 +0000 (11:38 -0700)
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
lib/netdev-dummy.c

index a940df8..82473a1 100644 (file)
@@ -241,8 +241,15 @@ static int
 netdev_dummy_create(const struct netdev_class *class, const char *name,
                     struct netdev **netdevp)
 {
-    static unsigned int n = 0xaa550000;
+    static unsigned int next_n = 0xaa550000;
+    static pthread_mutex_t mutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER;
+
     struct netdev_dummy *netdev;
+    unsigned int n;
+
+    xpthread_mutex_lock(&mutex);
+    n = next_n++;
+    xpthread_mutex_unlock(&mutex);
 
     netdev = xzalloc(sizeof *netdev);
     netdev_init(&netdev->up, name, class);
@@ -265,8 +272,6 @@ netdev_dummy_create(const struct netdev_class *class, const char *name,
 
     shash_add(&dummy_netdevs, name, netdev);
 
-    n++;
-
     *netdevp = &netdev->up;
 
     return 0;