All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chunming Zhou <David1.Zhou-5C7GfCeVMHo@public.gmane.org>
To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org
Cc: Chunming Zhou <David1.Zhou-5C7GfCeVMHo@public.gmane.org>,
	David.Mao-5C7GfCeVMHo@public.gmane.org
Subject: [PATCH 3/4] amdgpu: add mutex for across process reason
Date: Thu, 18 Aug 2016 15:55:58 +0800	[thread overview]
Message-ID: <1471506959-905-4-git-send-email-David1.Zhou@amd.com> (raw)
In-Reply-To: <1471506959-905-1-git-send-email-David1.Zhou-5C7GfCeVMHo@public.gmane.org>

Change-Id: I69b5c8d86f9e1ed32bb20e899b74ad4146c0e988
Signed-off-by: Chunming Zhou <David1.Zhou@amd.com>
---
 amdgpu/amdgpu_cs.c       | 36 +++++++++++++++++++++++++++++-------
 amdgpu/amdgpu_internal.h |  2 ++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/amdgpu/amdgpu_cs.c b/amdgpu/amdgpu_cs.c
index a3ff34e..ecbd4d7 100644
--- a/amdgpu/amdgpu_cs.c
+++ b/amdgpu/amdgpu_cs.c
@@ -541,6 +541,12 @@ int amdgpu_cs_create_semaphore_object(amdgpu_device_handle device_handle,
 	}
 	(*sem)->buf_handle = buf_handle;
 	atomic_set(&(*sem)->refcount, 1);
+	r = sem_init(&(*sem)->mutex, 1, 1);
+	if (r) {
+		amdgpu_bo_cpu_unmap(buf_handle);
+		amdgpu_bo_free(buf_handle);
+		return r;
+	}
 	(*sem)->version = 2;
 
 	return 0;
@@ -549,6 +555,7 @@ int amdgpu_cs_create_semaphore_object(amdgpu_device_handle device_handle,
 int amdgpu_cs_create_semaphore(amdgpu_semaphore_handle *sem)
 {
 	struct amdgpu_semaphore *gpu_semaphore;
+	int r;
 
 	if (NULL == sem)
 		return -EINVAL;
@@ -559,6 +566,11 @@ int amdgpu_cs_create_semaphore(amdgpu_semaphore_handle *sem)
 
 	gpu_semaphore->version = 1;
 	atomic_set(&gpu_semaphore->refcount, 1);
+	r = sem_init(&gpu_semaphore->mutex, 1, 1);
+	if (r) {
+		free(gpu_semaphore);
+		return r;
+	}
 	*sem = gpu_semaphore;
 
 	return 0;
@@ -581,6 +593,7 @@ int amdgpu_cs_signal_semaphore(amdgpu_context_handle ctx,
 	/* sem has been signaled */
 	if (sem->signal_fence.ctx_id)
 		return -EINVAL;
+	sem_wait(&sem->mutex);
 	pthread_mutex_lock(&ctx->sequence_mutex);
 	sem->signal_fence.ctx_id = ctx->id;
 	sem->signal_fence.ip_type = ip_type;
@@ -589,6 +602,7 @@ int amdgpu_cs_signal_semaphore(amdgpu_context_handle ctx,
 	sem->signal_fence.seq_no = ctx->last_seq[ip_type][ip_instance][ring];
 	update_references(NULL, &sem->refcount);
 	pthread_mutex_unlock(&ctx->sequence_mutex);
+	sem_post(&sem->mutex);
 	return 0;
 }
 
@@ -609,10 +623,11 @@ int amdgpu_cs_wait_semaphore(amdgpu_context_handle ctx,
 	/* must signal first */
 	if (0 == sem->signal_fence.ctx_id)
 		return -EINVAL;
-
+	sem_wait(&sem->mutex);
 	pthread_mutex_lock(&ctx->sequence_mutex);
 	list_add(&sem->list, &ctx->sem_list[ip_type][ip_instance][ring]);
 	pthread_mutex_unlock(&ctx->sequence_mutex);
+	sem_post(&sem->mutex);
 	return 0;
 }
 
@@ -622,13 +637,13 @@ static int amdgpu_cs_reset_sem(amdgpu_semaphore_handle sem)
 		return -EINVAL;
 	if (0 == sem->signal_fence.ctx_id)
 		return -EINVAL;
-
+	sem_wait(&sem->mutex);
 	sem->signal_fence.ctx_id = 0;
 	sem->signal_fence.ip_type = 0;
 	sem->signal_fence.ip_instance = 0;
 	sem->signal_fence.ring = 0;
 	sem->signal_fence.seq_no = 0;
-
+	sem_post(&sem->mutex);
 	return 0;
 }
 
@@ -636,8 +651,9 @@ static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem)
 {
 	if (NULL == sem)
 		return -EINVAL;
-
+	sem_wait(&sem->mutex);
 	if (update_references(&sem->refcount, NULL)) {
+		sem_post(&sem->mutex);
 		if (sem->version == 1)
 			free(sem);
 		else if (sem->version == 2) {
@@ -645,7 +661,9 @@ static int amdgpu_cs_unreference_sem(amdgpu_semaphore_handle sem)
 			amdgpu_bo_cpu_unmap(buf_handle);
 			amdgpu_bo_free(buf_handle);
 		}
+		return 0;
 	}
+	sem_post(&sem->mutex);
 	return 0;
 }
 
@@ -657,10 +675,14 @@ int amdgpu_cs_destroy_semaphore(amdgpu_semaphore_handle sem)
 int amdgpu_cs_export_semaphore(amdgpu_semaphore_handle sem,
 			       uint32_t *shared_handle)
 {
-	return amdgpu_bo_export(sem->buf_handle,
-				amdgpu_bo_handle_type_dma_buf_fd,
-				shared_handle);
+	int r;
 
+	sem_wait(&sem->mutex);
+	r = amdgpu_bo_export(sem->buf_handle,
+			     amdgpu_bo_handle_type_dma_buf_fd,
+			     shared_handle);
+	sem_post(&sem->mutex);
+	return r;
 }
 
 int amdgpu_cs_import_semaphore(amdgpu_semaphore_handle *sem,
diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h
index 7c422da..f035786 100644
--- a/amdgpu/amdgpu_internal.h
+++ b/amdgpu/amdgpu_internal.h
@@ -31,6 +31,7 @@
 
 #include <assert.h>
 #include <pthread.h>
+#include <semaphore.h>
 
 #include "libdrm_macros.h"
 #include "xf86atomic.h"
@@ -135,6 +136,7 @@ struct amdgpu_semaphore {
 	struct list_head list;
 	struct drm_amdgpu_fence signal_fence;
 	amdgpu_bo_handle buf_handle;
+	sem_t mutex;
 	uint32_t version;
 };
 
-- 
1.9.1

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

  parent reply	other threads:[~2016-08-18  7:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-18  7:55 [PATCH 0/4] share semaphore across process Chunming Zhou
     [not found] ` <1471506959-905-1-git-send-email-David1.Zhou-5C7GfCeVMHo@public.gmane.org>
2016-08-18  7:55   ` [PATCH 1/4] amdgpu: use drm_amdgpu_fence instead of amdgpu_cs_fence in semaphore structure Chunming Zhou
2016-08-18  7:55   ` [PATCH 2/4] amdgpu: add export/import semaphore apis Chunming Zhou
     [not found]     ` <1471506959-905-3-git-send-email-David1.Zhou-5C7GfCeVMHo@public.gmane.org>
2016-08-21  6:23       ` Edward O'Callaghan
     [not found]         ` <cf458d97-d40a-d363-b5c4-2ae45dde9179-dczkZgxz+BNUPWh3PAxdjQ@public.gmane.org>
2016-08-22  2:42           ` zhoucm1
     [not found]             ` <57BA6689.2010700-5C7GfCeVMHo@public.gmane.org>
2016-08-22 12:41               ` Edward O'Callaghan
     [not found]                 ` <414bf811-0e72-6f79-350f-8995a2b5167c-dczkZgxz+BNUPWh3PAxdjQ@public.gmane.org>
2016-08-22 12:44                   ` Edward O'Callaghan
2016-08-18  7:55   ` Chunming Zhou [this message]
2016-08-18  7:55   ` [PATCH 4/4] tests/amdgpu: add semaphore across process test Chunming Zhou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1471506959-905-4-git-send-email-David1.Zhou@amd.com \
    --to=david1.zhou-5c7gfcevmho@public.gmane.org \
    --cc=David.Mao-5C7GfCeVMHo@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.