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

Michael S. Tsirkin mst at mellanox.co.il
Wed Nov 9 07:36:30 PST 2005


Quoting Michael S. Tsirkin <mst at mellanox.co.il>:
> 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?

Oops, the patch I posted previously is broken.
Here's a correct version. Sorry.

---

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,19 @@
 
 #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;
+	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