OSDN Git Service

GKI cleanup - Moved functions GKI_disable() and GKI_enable() to OSI
authorPavlin Radoslavov <pavlin@google.com>
Fri, 25 Sep 2015 05:07:53 +0000 (22:07 -0700)
committerScott James Remnant <keybuk@google.com>
Mon, 19 Oct 2015 17:36:26 +0000 (10:36 -0700)
Added new functions mutex_global_lock() and mutex_global_unlock()
within the OSI module, and replaced GKI_disable() and GKI_enable()
with those functions.

Also, minor cleanup in the gki.h header file.

Change-Id: I5f410e3174541224fcf30f37e1524ef099c25193

13 files changed:
btcore/src/osi_module.c
btif/co/bta_av_co.c
btif/src/btif_media_task.c
gki/common/gki.h
gki/common/gki_buffer.c
gki/ulinux/gki_int.h
gki/ulinux/gki_ulinux.c
include/bt_target.h
osi/Android.mk
osi/BUILD.gn
osi/include/mutex.h [new file with mode: 0644]
osi/src/mutex.c [new file with mode: 0644]
stack/gap/gap_conn.c

index 9ba9685..96783bc 100644 (file)
 #include "osi/include/alarm.h"
 #include "osi/include/future.h"
 #include "osi/include/log.h"
+#include "osi/include/mutex.h"
 #include "osi/include/osi.h"
 
+future_t *osi_init(void) {
+  mutex_init();
+  return future_new_immediate(FUTURE_SUCCESS);
+}
+
 future_t *osi_clean_up(void) {
   alarm_cleanup();
-  return NULL;
+  mutex_cleanup();
+  return future_new_immediate(FUTURE_SUCCESS);
 }
 
 const module_t osi_module = {
   .name = OSI_MODULE,
-  .init = NULL,
+  .init = osi_init,
   .start_up = NULL,
   .shut_down = NULL,
   .clean_up = osi_clean_up,
index e410f14..9955963 100644 (file)
@@ -36,6 +36,7 @@
 #include "sbc_encoder.h"
 #include "btif_av_co.h"
 #include "btif_util.h"
+#include "osi/include/mutex.h"
 
 
 /*****************************************************************************
@@ -528,7 +529,7 @@ UINT8 bta_av_audio_sink_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
         APPL_TRACE_DEBUG("bta_av_audio_sink_getconfig last SRC reached");
 
         /* Protect access to bta_av_co_cb.codec_cfg */
-        GKI_disable();
+        mutex_global_lock();
 
         /* Find a src that matches the codec config */
         if (bta_av_co_audio_peer_src_supports_codec(p_peer, &index))
@@ -561,7 +562,7 @@ UINT8 bta_av_audio_sink_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
             }
         }
         /* Protect access to bta_av_co_cb.codec_cfg */
-        GKI_enable();
+        mutex_global_unlock();
     }
     return result;
 }
@@ -658,7 +659,7 @@ UINT8 bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
         APPL_TRACE_DEBUG("bta_av_co_audio_getconfig last sink reached");
 
         /* Protect access to bta_av_co_cb.codec_cfg */
-        GKI_disable();
+        mutex_global_lock();
 
         /* Find a sink that matches the codec config */
         if (bta_av_co_audio_peer_supports_codec(p_peer, &index))
@@ -720,7 +721,7 @@ UINT8 bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
             }
         }
         /* Protect access to bta_av_co_cb.codec_cfg */
-        GKI_enable();
+        mutex_global_unlock();
     }
     return result;
 }
@@ -813,7 +814,7 @@ void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
         {
 
             /* Protect access to bta_av_co_cb.codec_cfg */
-            GKI_disable();
+            mutex_global_lock();
 
             /* Check if the configuration matches the current codec config */
             switch (bta_av_co_cb.codec_cfg.id)
@@ -852,7 +853,7 @@ void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, tBTA_AV_CODEC codec_type,
                 break;
             }
             /* Protect access to bta_av_co_cb.codec_cfg */
-            GKI_enable();
+            mutex_global_unlock();
         }
         else
         {
@@ -1553,7 +1554,7 @@ BOOLEAN bta_av_co_audio_codec_supported(tBTIF_STATUS *p_status)
  *******************************************************************************/
 void bta_av_co_audio_codec_reset(void)
 {
-    GKI_disable();
+    mutex_global_lock();
     FUNC_TRACE();
 
     /* Reset the current configuration to SBC */
@@ -1564,7 +1565,7 @@ void bta_av_co_audio_codec_reset(void)
         APPL_TRACE_ERROR("bta_av_co_audio_codec_reset A2D_BldSbcInfo failed");
     }
 
-    GKI_enable();
+    mutex_global_unlock();
 }
 
 /*******************************************************************************
@@ -1676,7 +1677,7 @@ BOOLEAN bta_av_co_audio_get_sbc_config(tA2D_SBC_CIE *p_sbc_config, UINT16 *p_min
     /* Minimum MTU is by default very large */
     *p_minmtu = 0xFFFF;
 
-    GKI_disable();
+    mutex_global_lock();
     if (bta_av_co_cb.codec_cfg.id == BTIF_AV_CODEC_SBC)
     {
         if (A2D_ParsSbcInfo(p_sbc_config, bta_av_co_cb.codec_cfg.info, FALSE) == A2D_SUCCESS)
@@ -1718,7 +1719,7 @@ BOOLEAN bta_av_co_audio_get_sbc_config(tA2D_SBC_CIE *p_sbc_config, UINT16 *p_min
         /* Not SBC, still return the default values */
         *p_sbc_config = btif_av_sbc_default_config;
     }
-    GKI_enable();
+    mutex_global_unlock();
 
     return result;
 }
index 7861a0a..57021bc 100644 (file)
@@ -65,6 +65,7 @@
 #include "osi/include/alarm.h"
 #include "osi/include/fixed_queue.h"
 #include "osi/include/log.h"
+#include "osi/include/mutex.h"
 #include "osi/include/thread.h"
 
 #if (BTA_AV_INCLUDED == TRUE)
@@ -794,7 +795,7 @@ void btif_a2dp_setup_codec(void)
 
     APPL_TRACE_EVENT("## A2DP SETUP CODEC ##");
 
-    GKI_disable();
+    mutex_global_lock();
 
     /* for now hardcode 44.1 khz 16 bit stereo PCM format */
     media_feeding.cfg.pcm.sampling_freq = 44100;
@@ -816,7 +817,7 @@ void btif_a2dp_setup_codec(void)
         btif_media_task_audio_feeding_init_req(&mfeed);
     }
 
-    GKI_enable();
+    mutex_global_unlock();
 }
 
 
index 9ee118f..1515dc9 100644 (file)
@@ -33,8 +33,6 @@ typedef void (TIMER_CBACK)(void *p_tle);
 */
 typedef struct _tle
 {
-    struct _tle  *p_next;
-    struct _tle  *p_prev;
     TIMER_CBACK  *p_cback;
     INT32         ticks;
     INT32         ticks_initial;
@@ -54,9 +52,6 @@ typedef struct
     UINT16   _count;
 } BUFFER_Q;
 
-#define GKI_PUBLIC_POOL         0       /* General pool accessible to GKI_getbuf() */
-#define GKI_RESTRICTED_POOL     1       /* Inaccessible pool to GKI_getbuf() */
-
 /***********************************************************************
 ** Function prototypes
 */
@@ -79,8 +74,3 @@ void    GKI_init_q (BUFFER_Q *);
 UINT16  GKI_queue_length(BUFFER_Q *);
 BOOLEAN GKI_queue_is_empty(BUFFER_Q *);
 void   *GKI_remove_from_queue (BUFFER_Q *, void *);
-
-/* Disable Interrupts, Enable Interrupts
-*/
-void    GKI_enable(void);
-void    GKI_disable(void);
index 4bfa762..9f649ce 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 
 #include "osi/include/allocator.h"
+#include "osi/include/mutex.h"
 #include "gki_int.h"
 
 #if (GKI_NUM_TOTAL_BUF_POOLS > 16)
@@ -190,10 +191,6 @@ void GKI_init_q (BUFFER_Q *p_q)
 ** Description      Called by an application to get a free buffer which
 **                  is of size greater or equal to the requested size.
 **
-**                  Note: This routine only takes buffers from public pools.
-**                        It will not use any buffers from pools
-**                        marked GKI_RESTRICTED_POOL.
-**
 ** Parameters       size - (input) number of bytes needed.
 **
 ** Returns          A pointer to the buffer, or NULL if none available
@@ -261,7 +258,7 @@ void GKI_enqueue (BUFFER_Q *p_q, void *p_buf)
     BUFFER_HDR_T *p_hdr = (BUFFER_HDR_T *) ((UINT8 *) p_buf - BUFFER_HDR_SIZE);
     assert(p_hdr->status == BUF_STATUS_UNLINKED);
 
-    GKI_disable();
+    mutex_global_lock();
 
     /* Since the queue is exposed (C vs C++), keep the pointers in exposed format */
     if (p_q->_p_last)
@@ -278,7 +275,7 @@ void GKI_enqueue (BUFFER_Q *p_q, void *p_buf)
     p_hdr->p_next = NULL;
     p_hdr->status = BUF_STATUS_QUEUED;
 
-    GKI_enable();
+    mutex_global_unlock();
 }
 
 /*******************************************************************************
@@ -296,11 +293,11 @@ void *GKI_dequeue (BUFFER_Q *p_q)
 {
     BUFFER_HDR_T    *p_hdr;
 
-    GKI_disable();
+    mutex_global_lock();
 
     if (!p_q || !p_q->_count)
     {
-        GKI_enable();
+        mutex_global_unlock();
         return (NULL);
     }
 
@@ -321,7 +318,7 @@ void *GKI_dequeue (BUFFER_Q *p_q)
     p_hdr->p_next = NULL;
     p_hdr->status = BUF_STATUS_UNLINKED;
 
-    GKI_enable();
+    mutex_global_unlock();
 
     return ((UINT8 *)p_hdr + BUFFER_HDR_SIZE);
 }
@@ -343,11 +340,11 @@ void *GKI_remove_from_queue (BUFFER_Q *p_q, void *p_buf)
     BUFFER_HDR_T    *p_prev;
     BUFFER_HDR_T    *p_buf_hdr;
 
-    GKI_disable();
+    mutex_global_lock();
 
     if (p_buf == p_q->_p_first)
     {
-        GKI_enable();
+        mutex_global_unlock();
         return (GKI_dequeue (p_q));
     }
 
@@ -372,12 +369,12 @@ void *GKI_remove_from_queue (BUFFER_Q *p_q, void *p_buf)
             p_buf_hdr->p_next = NULL;
             p_buf_hdr->status = BUF_STATUS_UNLINKED;
 
-            GKI_enable();
+            mutex_global_unlock();
             return (p_buf);
         }
     }
 
-    GKI_enable();
+    mutex_global_unlock();
     return (NULL);
 }
 
index 6e05a85..2a69d35 100644 (file)
 
 #pragma once
 
-#include <pthread.h>
-
 #include "gki_common.h"
 
 typedef struct
 {
-    pthread_mutex_t lock;
     tGKI_COM_CB com;
 } tGKI_CB;
 
index acaaacf..bc4bbe2 100644 (file)
@@ -19,7 +19,6 @@
 #define LOG_TAG "bt_gki"
 
 #include <assert.h>
-#include <pthread.h>
 #include <string.h>
 
 #include "btcore/include/module.h"
@@ -32,11 +31,6 @@ tGKI_CB gki_cb;
 static future_t *init(void) {
   memset(&gki_cb, 0, sizeof(gki_cb));
 
-  pthread_mutexattr_t attr;
-  pthread_mutexattr_init(&attr);
-  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
-  pthread_mutex_init(&gki_cb.lock, &attr);
-
   gki_buffer_init();
   return NULL;
 }
@@ -44,7 +38,6 @@ static future_t *init(void) {
 static future_t *clean_up(void) {
   gki_buffer_cleanup();
 
-  pthread_mutex_destroy(&gki_cb.lock);
   return NULL;
 }
 
@@ -59,11 +52,3 @@ EXPORT_SYMBOL const module_t gki_module = {
     NULL
   }
 };
-
-void GKI_enable(void) {
-  pthread_mutex_unlock(&gki_cb.lock);
-}
-
-void GKI_disable(void) {
-  pthread_mutex_lock(&gki_cb.lock);
-}
index 0a32109..72d49e9 100644 (file)
 #define PORT_CREDIT_RX_LOW          8
 #endif
 
-/* if application like BTA, Java or script test engine is running on other than BTU thread, */
-/* PORT_SCHEDULE_LOCK shall be defined as GKI_sched_lock() or GKI_disable() */
+/*
+ * If an application like BTA, Java or script test engine is running on other
+ * than BTU thread, PORT_SCHEDULE_LOCK shall be defined as mutex_global_lock()
+ */
 #ifndef PORT_SCHEDULE_LOCK
-#define PORT_SCHEDULE_LOCK          GKI_disable()
+#define PORT_SCHEDULE_LOCK          mutex_global_lock()
 #endif
 
-/* if application like BTA, Java or script test engine is running on other than BTU thread, */
-/* PORT_SCHEDULE_LOCK shall be defined as GKI_sched_unlock() or GKI_enable() */
+/*
+ * If an application like BTA, Java or script test engine is running on other 
+ * than BTU thread, PORT_SCHEDULE_UNLOCK shall be defined as
+ * mutex_global_unlock()
+ */
 #ifndef PORT_SCHEDULE_UNLOCK
-#define PORT_SCHEDULE_UNLOCK        GKI_enable()
+#define PORT_SCHEDULE_UNLOCK        mutex_global_unlock()
 #endif
 
 /******************************************************************************
index 464def6..2706ba9 100644 (file)
@@ -39,6 +39,7 @@ btosiCommonSrc := \
     ./src/hash_map.c \
     ./src/hash_map_utils.c \
     ./src/list.c \
+    ./src/mutex.c \
     ./src/non_repeating_timer.c \
     ./src/reactor.c \
     ./src/ringbuffer.c \
index fcfbb45..05d386a 100644 (file)
@@ -31,6 +31,7 @@ static_library("osi") {
     "src/hash_map.c",
     "src/hash_map_utils.c",
     "src/list.c",
+    "src/mutex.c",
     "src/non_repeating_timer.c",
     "src/reactor.c",
     "src/ringbuffer.c",
diff --git a/osi/include/mutex.h b/osi/include/mutex.h
new file mode 100644 (file)
index 0000000..6b6a545
--- /dev/null
@@ -0,0 +1,33 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2015 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#pragma once
+
+#include <stdint.h>
+
+// Mutex-related state init
+void mutex_init(void);
+
+// Mutex-related state cleanup
+void mutex_cleanup(void);
+
+// Lock the global mutex
+void mutex_global_lock(void);
+
+// Unlock the global mutex
+void mutex_global_unlock(void);
diff --git a/osi/src/mutex.c b/osi/src/mutex.c
new file mode 100644 (file)
index 0000000..b70e4eb
--- /dev/null
@@ -0,0 +1,44 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2015 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#define LOG_TAG "bt_osi_mutex"
+
+#include <pthread.h>
+
+#include "osi/include/mutex.h"
+
+static pthread_mutex_t global_lock;
+
+void mutex_init(void) {
+  pthread_mutexattr_t attr;
+  pthread_mutexattr_init(&attr);
+  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+  pthread_mutex_init(&global_lock, &attr);
+}
+
+void mutex_cleanup(void) {
+  pthread_mutex_destroy(&global_lock);
+}
+
+void mutex_global_lock(void) {
+  pthread_mutex_lock(&global_lock);
+}
+
+void mutex_global_unlock(void) {
+  pthread_mutex_unlock(&global_lock);
+}
index 638e718..3d58dd1 100644 (file)
@@ -24,6 +24,7 @@
 #include "l2cdefs.h"
 #include "l2c_int.h"
 #include <string.h>
+#include "osi/include/mutex.h"
 #if GAP_CONN_INCLUDED == TRUE
 #include "btm_int.h"
 
@@ -314,7 +315,7 @@ UINT16 GAP_ConnReadData (UINT16 gap_handle, UINT8 *p_data, UINT16 max_len, UINT1
     if (!p_buf)
         return (GAP_NO_DATA_AVAIL);
 
-    GKI_disable();
+    mutex_global_lock();
 
     while (max_len && p_buf)
     {
@@ -345,7 +346,7 @@ UINT16 GAP_ConnReadData (UINT16 gap_handle, UINT8 *p_data, UINT16 max_len, UINT1
 
     p_ccb->rx_queue_size -= *p_len;
 
-    GKI_enable();
+    mutex_global_unlock();
 
     GAP_TRACE_EVENT ("GAP_ConnReadData - rx_queue_size left=%d, *p_len=%d",
                                        p_ccb->rx_queue_size, *p_len);