avsync: use atmotic to protect queue item [1/1]

PD#TV-84505

Problem:
thread competition

Solution:
use atmotic to protect queue item

Verify:
Na

Change-Id: Ie7e657b7263ba46fa8ae5f25d82cecb411af23ec
Signed-off-by: sheng.liu <sheng.liu@amlogic.com>
diff --git a/src/Makefile b/src/Makefile
index 596f582..ce16faa 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -14,7 +14,7 @@
 all: $(TARGET)
 endif
 
-CC_FLAG = -Wall
+CC_FLAG += -Wall
 LD_FLAG = -lm -lpthread
 
 ifeq ($(LOG), LOGCAT)
diff --git a/src/queue.c b/src/queue.c
index 0d7bcd5..95754b3 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -19,6 +19,7 @@
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdatomic.h>
 #include "aml_avsync.h"
 #include "aml_queue.h"
 
@@ -26,7 +27,7 @@
     int max_len;
     int ri; //read index
     int wi; //write index
-    void **items;
+    atomic_intptr_t *items;
 };
 
 void* create_q(int max_len)
@@ -44,7 +45,7 @@
         printf("%s %d OOM\n", __func__, __LINE__);
         return NULL;
     }
-    q->items = (void **)calloc(max_len, sizeof(void *));
+    q->items = (atomic_intptr_t *)calloc(max_len, sizeof(void *));
     if (!q->items) {
         printf("%s %d OOM\n", __func__, __LINE__);
         free(q);
@@ -78,8 +79,9 @@
     if (fullness >= q->max_len - 1)
         return -1; // not enough space
 
-    q->items[q->wi] = item;
-    if (q->wi == q->max_len - 1)
+    atomic_store(&q->items[q->wi], (atomic_intptr_t)item);
+
+    if (q->wi >= q->max_len - 1)
         q->wi = 0;
     else
         q->wi++;
@@ -103,9 +105,13 @@
     index += cnt;
     if (index >= q->max_len)
         index -= q->max_len;
-    *p_item = q->items[index];
 
-    return 0;
+    *p_item = (void *)atomic_load(&q->items[index]);
+
+    if (*p_item != NULL)
+        return 0;
+    else
+        return -1;
 }
 
 int dqueue_item(void *queue, void** p_item)
@@ -119,8 +125,11 @@
     if (fullness < 0) fullness += q->max_len;
     if (fullness == 0)
         return -1; //empty
-    *p_item = q->items[q->ri];
-    if (q->ri == q->max_len - 1)
+
+    *p_item = (void *)atomic_load(&q->items[q->ri]);
+    q->items[q->ri] = 0;
+
+    if (q->ri >= q->max_len - 1)
         q->ri = 0;
     else
         q->ri++;