From: Junyan He <junyan...@intel.com>

Also fix a bug in get ownership.

Signed-off-by: Junyan He <junyan...@intel.com>
---
 src/cl_base_object.c | 27 +++++++++++++++++++++++++++
 src/cl_base_object.h | 17 +++++++++++------
 2 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/src/cl_base_object.c b/src/cl_base_object.c
index 4661977..00c4b35 100644
--- a/src/cl_base_object.c
+++ b/src/cl_base_object.c
@@ -29,6 +29,7 @@ cl_object_init_base(cl_base_object obj, cl_ulong magic)
   pthread_mutex_init(&obj->mutex, NULL);
   pthread_cond_init(&obj->cond, NULL);
   obj->owner = invalid_thread_id;
+  list_init(&obj->node);
 }
 
 LOCAL void
@@ -53,6 +54,12 @@ cl_object_destroy_base(cl_base_object obj)
     assert(0);
   }
 
+  if (!list_empty(&obj->node)) {
+    DEBUGP(DL_ERROR, "CL object %p, call destroy while still belong to some 
object %p",
+           obj, obj->node.prev);
+    assert(0);
+  }
+
   obj->magic = CL_OBJECT_INVALID_MAGIC;
   pthread_mutex_destroy(&obj->mutex);
   pthread_cond_destroy(&obj->cond);
@@ -68,6 +75,12 @@ cl_object_take_ownership(cl_base_object obj, cl_int wait)
   self = pthread_self();
 
   pthread_mutex_lock(&obj->mutex);
+
+  if (pthread_equal(obj->owner, self)) { // Already get
+    pthread_mutex_unlock(&obj->mutex);
+    return 1;
+  }
+
   if (pthread_equal(obj->owner, invalid_thread_id)) {
     obj->owner = self;
     pthread_mutex_unlock(&obj->mutex);
@@ -100,3 +113,17 @@ cl_object_release_ownership(cl_base_object obj)
 
   pthread_mutex_unlock(&obj->mutex);
 }
+
+LOCAL void
+cl_object_wait_on_cond(cl_base_object obj)
+{
+  assert(CL_OBJECT_IS_VALID(obj));
+  pthread_cond_wait(&obj->cond, &obj->mutex);
+}
+
+LOCAL void
+cl_object_notify_cond(cl_base_object obj)
+{
+  assert(CL_OBJECT_IS_VALID(obj));
+  pthread_cond_broadcast(&obj->cond);
+}
diff --git a/src/cl_base_object.h b/src/cl_base_object.h
index 77f2c2f..4e643df 100644
--- a/src/cl_base_object.h
+++ b/src/cl_base_object.h
@@ -44,12 +44,13 @@
 *************************************************************************/
 
 typedef struct _cl_base_object {
-  DEFINE_ICD(dispatch);         /* Dispatch function table for icd */
-  cl_ulong magic;               /* Magic number for each CL object */
-  atomic_t ref;                 /* Reference for each CL object */
-  pthread_mutex_t mutex;        /* THe mutex to protect this object MT safe */
-  pthread_cond_t cond;          /* Condition to wait for getting the object */
-  pthread_t owner;              /* The thread which own this object */
+  DEFINE_ICD(dispatch);  /* Dispatch function table for icd */
+  cl_ulong magic;        /* Magic number for each CL object */
+  atomic_t ref;          /* Reference for each CL object */
+  list_head node;        /* CL object node belong to some container */
+  pthread_mutex_t mutex; /* THe mutex to protect this object MT safe */
+  pthread_cond_t cond;   /* Condition to wait for getting the object */
+  pthread_t owner;       /* The thread which own this object */
 } _cl_base_object;
 
 typedef struct _cl_base_object *cl_base_object;
@@ -68,10 +69,14 @@ extern void cl_object_init_base(cl_base_object obj, 
cl_ulong magic);
 extern void cl_object_destroy_base(cl_base_object obj);
 extern cl_int cl_object_take_ownership(cl_base_object obj, cl_int wait);
 extern void cl_object_release_ownership(cl_base_object obj);
+extern void cl_object_wait_on_cond(cl_base_object obj);
+extern void cl_object_notify_cond(cl_base_object obj);
 
 #define CL_OBJECT_INIT_BASE(obj, magic) 
(cl_object_init_base((cl_base_object)obj, magic))
 #define CL_OBJECT_DESTROY_BASE(obj) 
(cl_object_destroy_base((cl_base_object)obj))
 #define CL_OBJECT_TAKE_OWNERSHIP(obj, wait) 
(cl_object_take_ownership((cl_base_object)obj, wait))
 #define CL_OBJECT_RELEASE_OWNERSHIP(obj) 
(cl_object_release_ownership((cl_base_object)obj))
+#define CL_OBJECT_WAIT_ON_COND(obj) 
(cl_object_wait_on_cond((cl_base_object)obj))
+#define CL_OBJECT_NOTIFY_COND(obj) (cl_object_notify_cond((cl_base_object)obj))
 
 #endif /* __CL_BASE_OBJECT_H__ */
-- 
2.7.4



_______________________________________________
Beignet mailing list
Beignet@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/beignet

Reply via email to