[ofa-general] [PATCH - libibverbs] Added reference count to completion event channels

Dotan Barak dotanb at dev.mellanox.co.il
Mon Mar 12 02:38:14 PDT 2007


Added reference count to completion event channels.

Signed-off-by: Dotan Barak <dotanb at mellanox.co.il>

---

Index: gen2_devel_user/src/userspace/libibverbs/include/infiniband/verbs.h
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/include/infiniband/verbs.h	2007-02-26 16:01:56.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/include/infiniband/verbs.h	2007-03-04 10:44:34.696598288 +0200
@@ -546,11 +546,14 @@ struct ibv_qp {
 };
 
 struct ibv_comp_channel {
+	pthread_mutex_t         mutex;
+	int			refcnt;
 	int			fd;
 };
 
 struct ibv_cq {
 	struct ibv_context     *context;
+	struct ibv_comp_channel *channel;
 	void		       *cq_context;
 	uint32_t		handle;
 	int			cqe;
Index: gen2_devel_user/src/userspace/libibverbs/src/verbs.c
===================================================================
--- gen2_devel_user.orig/src/userspace/libibverbs/src/verbs.c	2007-02-26 16:01:56.000000000 +0200
+++ gen2_devel_user/src/userspace/libibverbs/src/verbs.c	2007-03-04 10:42:41.073871568 +0200
@@ -226,7 +226,9 @@ struct ibv_comp_channel *ibv_create_comp
 		return NULL;
 	}
 
-	channel->fd = resp.fd;
+	channel->refcnt = 0;
+	channel->fd     = resp.fd;
+	pthread_mutex_init(&channel->mutex, NULL);
 
 	return channel;
 }
@@ -243,6 +245,12 @@ int ibv_destroy_comp_channel(struct ibv_
 	if (abi_ver <= 2)
 		return ibv_destroy_comp_channel_v2(channel);
 
+	pthread_mutex_lock(&channel->mutex);
+	if (channel->refcnt) {
+		pthread_mutex_unlock(&channel->mutex);
+		return EBUSY;
+	}
+	pthread_mutex_unlock(&channel->mutex);
 	close(channel->fd);
 	free(channel);
 
@@ -260,8 +268,14 @@ struct ibv_cq *__ibv_create_cq(struct ib
 		cq->cq_context 	     	   = cq_context;
 		cq->comp_events_completed  = 0;
 		cq->async_events_completed = 0;
+		cq->channel                = channel;
 		pthread_mutex_init(&cq->mutex, NULL);
 		pthread_cond_init(&cq->cond, NULL);
+		if (channel) {
+			pthread_mutex_lock(&channel->mutex);
+			channel->refcnt ++;
+			pthread_mutex_unlock(&channel->mutex);
+		}
 	}
 
 	return cq;
@@ -279,7 +293,17 @@ default_symver(__ibv_resize_cq, ibv_resi
 
 int __ibv_destroy_cq(struct ibv_cq *cq)
 {
-	return cq->context->ops.destroy_cq(cq);
+	struct ibv_comp_channel *channel = cq->channel;
+	int ret;
+
+	ret = cq->context->ops.destroy_cq(cq);
+	if (!ret && channel) {
+		pthread_mutex_lock(&channel->mutex);
+		channel->refcnt --;
+		pthread_mutex_unlock(&channel->mutex);
+	}
+
+	return ret;
 }
 default_symver(__ibv_destroy_cq, ibv_destroy_cq);
 





More information about the general mailing list