[Beignet] [PATCH] Runtime: Add CL base object for all cl objects.

2016-07-14 Thread junyan . he
From: Junyan He 

The runtime code is a little verbose in CL object handle.
Every CL objects should have a reference, a lock to protect itself
and an ICD dispatcher. We can organize them to a struct and place
it at the beginning of each CL object.
This base object is also used to protect the CL objects MT safe.
CL_OBJECT_LOCK/CL_OBJECT_UNLOCK macro will lock/unlock objects,
but we should use them within one function call, and the critical
region should be short.
We add CL_OBJECT_TAKE_OWNERSHIP/CL_OBJECT_RELEASE_OWNERSHIP macro
to own the object for a long time. CL_OBJECT_TAKE_OWNERSHIP will
not hold the lock and so will not cause deadlock problems.
For example, when we call NDRange on some memobj, we should take
the ownship of the memobj. If another thread call NDRange on the
same memobj, we should return some error like CL_OUT_OF_RESOURCE
to users and protect the memobj from accessing simultaneously.
---
 src/CMakeLists.txt   |1 +
 src/cl_base_object.c |   98 ++
 src/cl_base_object.h |   59 ++
 3 files changed, 158 insertions(+)
 create mode 100644 src/cl_base_object.c
 create mode 100644 src/cl_base_object.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a002865..5e8f000 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -84,6 +84,7 @@ set(OPENCL_SRC
 cl_command_queue.h
 cl_command_queue_gen7.c
 cl_thread.c
+cl_base_object.c
 cl_driver.h
 cl_driver.cpp
 cl_driver_defs.c
diff --git a/src/cl_base_object.c b/src/cl_base_object.c
new file mode 100644
index 000..bfbc790
--- /dev/null
+++ b/src/cl_base_object.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see .
+ *
+ */
+#include 
+#include "cl_base_object.h"
+
+static pthread_t invalid_thread_id = -1;
+
+LOCAL void cl_object_init_base(cl_base_object obj)
+{
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  obj->ref = 1;
+  SET_ICD(obj->dispatch)
+  pthread_mutex_init(&obj->mutex, NULL);
+  pthread_cond_init(&obj->cond, NULL);
+  obj->owner = invalid_thread_id;
+}
+
+LOCAL void cl_object_destroy_base(cl_base_object obj)
+{
+  int ref = CL_OBJECT_GET_REF(obj);
+  if (ref != 0) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy with a reference %d", obj,
+   ref);
+assert(0);
+  }
+
+  if (!CL_OBJECT_IS_VALID(obj)) {
+DEBUGP(DL_ERROR,
+   "CL object %p, call destroy while it is already a dead object", 
obj);
+assert(0);
+  }
+
+  if (obj->owner != invalid_thread_id) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy while still has a owener %d",
+   obj, (int)obj->owner);
+assert(0);
+  }
+
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  pthread_mutex_destroy(&obj->mutex);
+  pthread_cond_destroy(&obj->cond);
+}
+
+LOCAL cl_int cl_object_take_ownership(cl_base_object obj, cl_int wait)
+{
+  pthread_t self;
+
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  self = pthread_self();
+
+  pthread_mutex_lock(&obj->mutex);
+  if (pthread_equal(obj->owner, invalid_thread_id)) {
+obj->owner = self;
+pthread_mutex_unlock(&obj->mutex);
+return 1;
+  }
+
+  if (wait == 0) {
+pthread_mutex_unlock(&obj->mutex);
+return 0;
+  }
+
+  while (!pthread_equal(obj->owner, invalid_thread_id)) {
+pthread_cond_wait(&obj->cond, &obj->mutex);
+  }
+
+  obj->owner = self;
+  pthread_mutex_unlock(&obj->mutex);
+  return 1;
+}
+
+LOCAL void cl_object_release_ownership(cl_base_object obj)
+{
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  pthread_mutex_lock(&obj->mutex);
+  assert(pthread_equal(pthread_self(), obj->owner));
+  obj->owner = invalid_thread_id;
+  pthread_cond_broadcast(&obj->cond);
+
+  pthread_mutex_unlock(&obj->mutex);
+}
diff --git a/src/cl_base_object.h b/src/cl_base_object.h
new file mode 100644
index 000..82370d5
--- /dev/null
+++ b/src/cl_base_object.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *

[Beignet] [PATCH] Runtime: Add CL base object for all cl objects.

2016-07-14 Thread junyan . he
From: Junyan He 

The runtime code is a little verbose in CL object handle.
Every CL objects should have a reference, a lock to protect itself
and an ICD dispatcher. We can organize them to a struct and place
it at the beginning of each CL object.
This base object is also used to protect the CL objects MT safe.
CL_OBJECT_LOCK/CL_OBJECT_UNLOCK macro will lock/unlock objects,
but we should use them within one function call, and the critical
region should be short.
We add CL_OBJECT_TAKE_OWNERSHIP/CL_OBJECT_RELEASE_OWNERSHIP macro
to own the object for a long time. CL_OBJECT_TAKE_OWNERSHIP will
not hold the lock and so will not cause deadlock problems.
For example, when we call NDRange on some memobj, we should take
the ownship of the memobj. If another thread call NDRange on the
same memobj, we should return some error like CL_OUT_OF_RESOURCE
to users and protect the memobj from accessing simultaneously.
---
 src/CMakeLists.txt   |1 +
 src/cl_base_object.c |   98 ++
 src/cl_base_object.h |   59 ++
 3 files changed, 158 insertions(+)
 create mode 100644 src/cl_base_object.c
 create mode 100644 src/cl_base_object.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a002865..5e8f000 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -84,6 +84,7 @@ set(OPENCL_SRC
 cl_command_queue.h
 cl_command_queue_gen7.c
 cl_thread.c
+cl_base_object.c
 cl_driver.h
 cl_driver.cpp
 cl_driver_defs.c
diff --git a/src/cl_base_object.c b/src/cl_base_object.c
new file mode 100644
index 000..bfbc790
--- /dev/null
+++ b/src/cl_base_object.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see .
+ *
+ */
+#include 
+#include "cl_base_object.h"
+
+static pthread_t invalid_thread_id = -1;
+
+LOCAL void cl_object_init_base(cl_base_object obj)
+{
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  obj->ref = 1;
+  SET_ICD(obj->dispatch)
+  pthread_mutex_init(&obj->mutex, NULL);
+  pthread_cond_init(&obj->cond, NULL);
+  obj->owner = invalid_thread_id;
+}
+
+LOCAL void cl_object_destroy_base(cl_base_object obj)
+{
+  int ref = CL_OBJECT_GET_REF(obj);
+  if (ref != 0) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy with a reference %d", obj,
+   ref);
+assert(0);
+  }
+
+  if (!CL_OBJECT_IS_VALID(obj)) {
+DEBUGP(DL_ERROR,
+   "CL object %p, call destroy while it is already a dead object", 
obj);
+assert(0);
+  }
+
+  if (obj->owner != invalid_thread_id) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy while still has a owener %d",
+   obj, (int)obj->owner);
+assert(0);
+  }
+
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  pthread_mutex_destroy(&obj->mutex);
+  pthread_cond_destroy(&obj->cond);
+}
+
+LOCAL cl_int cl_object_take_ownership(cl_base_object obj, cl_int wait)
+{
+  pthread_t self;
+
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  self = pthread_self();
+
+  pthread_mutex_lock(&obj->mutex);
+  if (pthread_equal(obj->owner, invalid_thread_id)) {
+obj->owner = self;
+pthread_mutex_unlock(&obj->mutex);
+return 1;
+  }
+
+  if (wait == 0) {
+pthread_mutex_unlock(&obj->mutex);
+return 0;
+  }
+
+  while (!pthread_equal(obj->owner, invalid_thread_id)) {
+pthread_cond_wait(&obj->cond, &obj->mutex);
+  }
+
+  obj->owner = self;
+  pthread_mutex_unlock(&obj->mutex);
+  return 1;
+}
+
+LOCAL void cl_object_release_ownership(cl_base_object obj)
+{
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  pthread_mutex_lock(&obj->mutex);
+  assert(pthread_equal(pthread_self(), obj->owner));
+  obj->owner = invalid_thread_id;
+  pthread_cond_broadcast(&obj->cond);
+
+  pthread_mutex_unlock(&obj->mutex);
+}
diff --git a/src/cl_base_object.h b/src/cl_base_object.h
new file mode 100644
index 000..82370d5
--- /dev/null
+++ b/src/cl_base_object.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *

[Beignet] [PATCH] Runtime: Add CL base object for all cl objects.

2016-07-14 Thread junyan . he
From: Junyan He 

The runtime code is a little verbose in CL object handle.
Every CL objects should have a reference, a lock to protect itself
and an ICD dispatcher. We can organize them to a struct and place
it at the beginning of each CL object.
This base object is also used to protect the CL objects MT safe.
CL_OBJECT_LOCK/CL_OBJECT_UNLOCK macro will lock/unlock objects,
but we should use them within one function call, and the critical
region should be short.
We add CL_OBJECT_TAKE_OWNERSHIP/CL_OBJECT_RELEASE_OWNERSHIP macro
to own the object for a long time. CL_OBJECT_TAKE_OWNERSHIP will
not hold the lock and so will not cause deadlock problems.
For example, when we call NDRange on some memobj, we should take
the ownship of the memobj. If another thread call NDRange on the
same memobj, we should return some error like CL_OUT_OF_RESOURCE
to users and protect the memobj from accessing simultaneously.
---
 src/CMakeLists.txt   |1 +
 src/cl_base_object.c |   98 ++
 src/cl_base_object.h |   59 ++
 3 files changed, 158 insertions(+)
 create mode 100644 src/cl_base_object.c
 create mode 100644 src/cl_base_object.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a002865..5e8f000 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -84,6 +84,7 @@ set(OPENCL_SRC
 cl_command_queue.h
 cl_command_queue_gen7.c
 cl_thread.c
+cl_base_object.c
 cl_driver.h
 cl_driver.cpp
 cl_driver_defs.c
diff --git a/src/cl_base_object.c b/src/cl_base_object.c
new file mode 100644
index 000..bfbc790
--- /dev/null
+++ b/src/cl_base_object.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see .
+ *
+ */
+#include 
+#include "cl_base_object.h"
+
+static pthread_t invalid_thread_id = -1;
+
+LOCAL void cl_object_init_base(cl_base_object obj)
+{
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  obj->ref = 1;
+  SET_ICD(obj->dispatch)
+  pthread_mutex_init(&obj->mutex, NULL);
+  pthread_cond_init(&obj->cond, NULL);
+  obj->owner = invalid_thread_id;
+}
+
+LOCAL void cl_object_destroy_base(cl_base_object obj)
+{
+  int ref = CL_OBJECT_GET_REF(obj);
+  if (ref != 0) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy with a reference %d", obj,
+   ref);
+assert(0);
+  }
+
+  if (!CL_OBJECT_IS_VALID(obj)) {
+DEBUGP(DL_ERROR,
+   "CL object %p, call destroy while it is already a dead object", 
obj);
+assert(0);
+  }
+
+  if (obj->owner != invalid_thread_id) {
+DEBUGP(DL_ERROR, "CL object %p, call destroy while still has a owener %d",
+   obj, (int)obj->owner);
+assert(0);
+  }
+
+  obj->magic = CL_OBJECT_INVALID_MAGIC;
+  pthread_mutex_destroy(&obj->mutex);
+  pthread_cond_destroy(&obj->cond);
+}
+
+LOCAL cl_int cl_object_take_ownership(cl_base_object obj, cl_int wait)
+{
+  pthread_t self;
+
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  self = pthread_self();
+
+  pthread_mutex_lock(&obj->mutex);
+  if (pthread_equal(obj->owner, invalid_thread_id)) {
+obj->owner = self;
+pthread_mutex_unlock(&obj->mutex);
+return 1;
+  }
+
+  if (wait == 0) {
+pthread_mutex_unlock(&obj->mutex);
+return 0;
+  }
+
+  while (!pthread_equal(obj->owner, invalid_thread_id)) {
+pthread_cond_wait(&obj->cond, &obj->mutex);
+  }
+
+  obj->owner = self;
+  pthread_mutex_unlock(&obj->mutex);
+  return 1;
+}
+
+LOCAL void cl_object_release_ownership(cl_base_object obj)
+{
+  assert(CL_OBJECT_IS_VALID(obj));
+
+  pthread_mutex_lock(&obj->mutex);
+  assert(pthread_equal(pthread_self(), obj->owner));
+  obj->owner = invalid_thread_id;
+  pthread_cond_broadcast(&obj->cond);
+
+  pthread_mutex_unlock(&obj->mutex);
+}
diff --git a/src/cl_base_object.h b/src/cl_base_object.h
new file mode 100644
index 000..82370d5
--- /dev/null
+++ b/src/cl_base_object.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *

Re: [Beignet] [PATCH] Runtime: Add CL base object for all cl objects.

2016-07-15 Thread Simon Richter
Hi,

On 14.07.2016 10:15, junyan...@inbox.com wrote:

> The runtime code is a little verbose in CL object handle.
> Every CL objects should have a reference, a lock to protect itself
> and an ICD dispatcher. We can organize them to a struct and place
> it at the beginning of each CL object.

Does that mean that only a single call to DEFINE_ICD() and SET_ICD()
remains? If so, can/should these be inlined?

   Simon




signature.asc
Description: OpenPGP digital signature
___
Beignet mailing list
Beignet@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/beignet


Re: [Beignet] [PATCH] Runtime: Add CL base object for all cl objects.

2016-07-19 Thread He Junyan
On Fri, Jul 15, 2016 at 11:50:06AM +0200, Simon Richter wrote:
> Date: Fri, 15 Jul 2016 11:50:06 +0200
> From: Simon Richter 
> To: beignet@lists.freedesktop.org
> Subject: Re: [Beignet] [PATCH] Runtime: Add CL base object for all cl
>  objects.
> 
> Hi,
> 
> On 14.07.2016 10:15, junyan...@inbox.com wrote:
> 
> > The runtime code is a little verbose in CL object handle.
> > Every CL objects should have a reference, a lock to protect itself
> > and an ICD dispatcher. We can organize them to a struct and place
> > it at the beginning of each CL object.
> 
> Does that mean that only a single call to DEFINE_ICD() and SET_ICD()
> remains? If so, can/should these be inlined?
Really it is, it's useless to define a Macro.
Thanks.

> 
>Simon
> 
> 



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


GET FREE 5GB EMAIL - Check out spam free email with many cool features!
Visit http://www.inbox.com/email to find out more!


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