[openib-general] [PATCH] libibverbs: protect device list initialization

Michael S. Tsirkin mst at mellanox.co.il
Wed Nov 9 06:15:58 PST 2005


Hello, Roland!

The following patch solves a problem I'm seeing when multiple
threads try to call ibv_get_devices at the same time.

Which brings me to another issue: our code examples call non-reentrant
dlist_for_each variants of dlist scanning routines, which will
create strange problems for multi-threaded users who might copy this.

I propose returning const struct dlist instead of struct dlist
from ibv_get_devices, which I think will trigger a warning
on such code, and converting all users to the reentrant dlist_for_each_nomark.
Comments?

---

Make ibv_get_devices reentrant.

Signed-off-by: Michael S. Tsirkin <mst at mellanox.co.il>

Index: src/userspace/libibverbs/src/device.c
===================================================================
--- src/userspace/libibverbs/src/device.c	(revision 3914)
+++ src/userspace/libibverbs/src/device.c	(working copy)
@@ -48,13 +48,17 @@
 
 #include "ibverbs.h"
 
+static pthread_mutex_t device_list_lock = PTHREAD_MUTEX_INITIALIZER;
 static struct dlist *device_list;
 
 struct dlist *ibv_get_devices(void)
 {
+	struct dlist *l;
+	pthread_mutex_lock(&device_list_lock);
 	if (!device_list)
-		device_list = ibverbs_init();
-	return device_list;
+		l = device_list = ibverbs_init();
+	pthread_mutex_unlock(&device_list_lock);
+	return l;
 }
 
 const char *ibv_get_device_name(struct ibv_device *device)

-- 
MST



More information about the general mailing list