Home | History | Annotate | Download | only in ulinux
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 #include <stdio.h>
     19 #include <stdarg.h>
     20 #include <errno.h>
     21 
     22 #define GKI_DEBUG   FALSE
     23 
     24 #include <pthread.h>  /* must be 1st header defined  */
     25 #include <time.h>
     26 #include "gki_int.h"
     27 #include "gki_target.h"
     28 
     29 /* Temp android logging...move to android tgt config file */
     30 
     31 #ifndef LINUX_NATIVE
     32 #include <cutils/log.h>
     33 #else
     34 #define LOGV(format, ...)  fprintf (stdout, LOG_TAG format, ## __VA_ARGS__)
     35 #define LOGE(format, ...)  fprintf (stderr, LOG_TAG format, ## __VA_ARGS__)
     36 #define LOGI(format, ...)  fprintf (stdout, LOG_TAG format, ## __VA_ARGS__)
     37 
     38 #define SCHED_NORMAL 0
     39 #define SCHED_FIFO 1
     40 #define SCHED_RR 2
     41 #define SCHED_BATCH 3
     42 
     43 #define pthread_cond_timedwait_monotonic pthread_cond_timedwait
     44 
     45 #endif
     46 
     47 /* Define the structure that holds the GKI variables
     48 */
     49 #if GKI_DYNAMIC_MEMORY == FALSE
     50 tGKI_CB   gki_cb;
     51 #endif
     52 
     53 #define NANOSEC_PER_MILLISEC (1000000)
     54 #define NSEC_PER_SEC (1000*NANOSEC_PER_MILLISEC)
     55 
     56 /* works only for 1ms to 1000ms heart beat ranges */
     57 #define LINUX_SEC (1000/TICKS_PER_SEC)
     58 // #define GKI_TICK_TIMER_DEBUG
     59 
     60 #define LOCK(m)  pthread_mutex_lock(&m)
     61 #define UNLOCK(m) pthread_mutex_unlock(&m)
     62 #define INIT(m) pthread_mutex_init(&m, NULL)
     63 
     64 
     65 /* this kind of mutex go into tGKI_OS control block!!!! */
     66 /* static pthread_mutex_t GKI_sched_mutex; */
     67 /*static pthread_mutex_t thread_delay_mutex;
     68 static pthread_cond_t thread_delay_cond;
     69 static pthread_mutex_t gki_timer_update_mutex;
     70 static pthread_cond_t   gki_timer_update_cond;
     71 */
     72 #ifdef NO_GKI_RUN_RETURN
     73 static pthread_t            timer_thread_id = 0;
     74 #endif
     75 
     76 
     77 /* For Android */
     78 
     79 #ifndef GKI_SHUTDOWN_EVT
     80 #define GKI_SHUTDOWN_EVT    APPL_EVT_7
     81 #endif
     82 
     83 typedef struct
     84 {
     85     UINT8 task_id;          /* GKI task id */
     86     TASKPTR task_entry;     /* Task entry function*/
     87     UINT32 params;          /* Extra params to pass to task entry function */
     88     pthread_cond_t* pCond;	/* for android*/
     89     pthread_mutex_t* pMutex;  /* for android*/
     90 } gki_pthread_info_t;
     91 gki_pthread_info_t gki_pthread_info[GKI_MAX_TASKS];
     92 
     93 /*******************************************************************************
     94 **
     95 ** Function         gki_task_entry
     96 **
     97 ** Description      entry point of GKI created tasks
     98 **
     99 ** Returns          void
    100 **
    101 *******************************************************************************/
    102 void gki_task_entry(UINT32 params)
    103 {
    104     pthread_t thread_id = pthread_self();
    105     gki_pthread_info_t *p_pthread_info = (gki_pthread_info_t *)params;
    106     GKI_TRACE_5("gki_task_entry task_id=%i, thread_id=%x/%x, pCond/pMutex=%x/%x", p_pthread_info->task_id,
    107                 gki_cb.os.thread_id[p_pthread_info->task_id], pthread_self(),
    108                 p_pthread_info->pCond, p_pthread_info->pMutex);
    109 
    110     gki_cb.os.thread_id[p_pthread_info->task_id] = thread_id;
    111     /* Call the actual thread entry point */
    112     (p_pthread_info->task_entry)(p_pthread_info->params);
    113 
    114     GKI_TRACE_1("gki_task task_id=%i terminating", p_pthread_info->task_id);
    115     gki_cb.os.thread_id[p_pthread_info->task_id] = 0;
    116 
    117     pthread_exit(0);    /* GKI tasks have no return value */
    118 }
    119 /* end android */
    120 
    121 #ifndef ANDROID
    122 void GKI_TRACE(char *fmt, ...)
    123 {
    124     LOCK(gki_cb.os.GKI_trace_mutex);
    125     va_list ap;
    126 
    127     va_start(ap, fmt);
    128     vfprintf(stderr, fmt, ap);
    129     fprintf(stderr, "\n");
    130 
    131     va_end(ap);
    132     UNLOCK(gki_cb.os.GKI_trace_mutex);
    133 }
    134 #endif
    135 
    136 /*******************************************************************************
    137 **
    138 ** Function         GKI_init
    139 **
    140 ** Description      This function is called once at startup to initialize
    141 **                  all the timer structures.
    142 **
    143 ** Returns          void
    144 **
    145 *******************************************************************************/
    146 
    147 void GKI_init(void)
    148 {
    149     pthread_mutexattr_t attr;
    150     tGKI_OS             *p_os;
    151 
    152     memset (&gki_cb, 0, sizeof (gki_cb));
    153 
    154     gki_buffer_init();
    155     gki_timers_init();
    156     gki_cb.com.OSTicks = (UINT32) times(0);
    157 
    158     pthread_mutexattr_init(&attr);
    159 
    160 #ifndef __CYGWIN__
    161     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
    162 #endif
    163     p_os = &gki_cb.os;
    164     pthread_mutex_init(&p_os->GKI_mutex, &attr);
    165     /* pthread_mutex_init(&GKI_sched_mutex, NULL); */
    166 #if (GKI_DEBUG == TRUE)
    167     pthread_mutex_init(&p_os->GKI_trace_mutex, NULL);
    168 #endif
    169     /* pthread_mutex_init(&thread_delay_mutex, NULL); */  /* used in GKI_delay */
    170     /* pthread_cond_init (&thread_delay_cond, NULL); */
    171 
    172     /* Initialiase GKI_timer_update suspend variables & mutexes to be in running state.
    173      * this works too even if GKI_NO_TICK_STOP is defined in btld.txt */
    174     p_os->no_timer_suspend = GKI_TIMER_TICK_RUN_COND;
    175     pthread_mutex_init(&p_os->gki_timer_mutex, NULL);
    176     pthread_cond_init(&p_os->gki_timer_cond, NULL);
    177 }
    178 
    179 
    180 /*******************************************************************************
    181 **
    182 ** Function         GKI_get_os_tick_count
    183 **
    184 ** Description      This function is called to retrieve the native OS system tick.
    185 **
    186 ** Returns          Tick count of native OS.
    187 **
    188 *******************************************************************************/
    189 UINT32 GKI_get_os_tick_count(void)
    190 {
    191 
    192     /* TODO - add any OS specific code here
    193     **/
    194     return (gki_cb.com.OSTicks);
    195 }
    196 
    197 /*******************************************************************************
    198 **
    199 ** Function         GKI_create_task
    200 **
    201 ** Description      This function is called to create a new OSS task.
    202 **
    203 ** Parameters:      task_entry  - (input) pointer to the entry function of the task
    204 **                  task_id     - (input) Task id is mapped to priority
    205 **                  taskname    - (input) name given to the task
    206 **                  stack       - (input) pointer to the top of the stack (highest memory location)
    207 **                  stacksize   - (input) size of the stack allocated for the task
    208 **
    209 ** Returns          GKI_SUCCESS if all OK, GKI_FAILURE if any problem
    210 **
    211 ** NOTE             This function take some parameters that may not be needed
    212 **                  by your particular OS. They are here for compatability
    213 **                  of the function prototype.
    214 **
    215 *******************************************************************************/
    216 UINT8 GKI_create_task (TASKPTR task_entry, UINT8 task_id, INT8 *taskname, UINT16 *stack, UINT16 stacksize, void* pCondVar, void* pMutex)
    217 {
    218     UINT16  i;
    219     UINT8   *p;
    220     struct sched_param param;
    221     int policy, ret = 0;
    222     pthread_attr_t attr1;
    223 
    224     GKI_TRACE_5 ("GKI_create_task func=0x%x  id=%d  name=%s  stack=0x%x  stackSize=%d", task_entry, task_id, taskname, stack, stacksize);
    225 
    226     if (task_id >= GKI_MAX_TASKS)
    227     {
    228         GKI_TRACE_0("Error! task ID > max task allowed");
    229         return (GKI_FAILURE);
    230     }
    231 
    232 
    233     gki_cb.com.OSRdyTbl[task_id]    = TASK_READY;
    234     gki_cb.com.OSTName[task_id]     = taskname;
    235     gki_cb.com.OSWaitTmr[task_id]   = 0;
    236     gki_cb.com.OSWaitEvt[task_id]   = 0;
    237 
    238     /* Initialize mutex and condition variable objects for events and timeouts */
    239     pthread_mutex_init(&gki_cb.os.thread_evt_mutex[task_id], NULL);
    240     pthread_cond_init (&gki_cb.os.thread_evt_cond[task_id], NULL);
    241     pthread_mutex_init(&gki_cb.os.thread_timeout_mutex[task_id], NULL);
    242     pthread_cond_init (&gki_cb.os.thread_timeout_cond[task_id], NULL);
    243 
    244     pthread_attr_init(&attr1);
    245     /* by default, pthread creates a joinable thread */
    246 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    247     pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);
    248 
    249     GKI_TRACE_3("GKI creating task %i, pCond/pMutex=%x/%x", task_id, pCondVar, pMutex);
    250 #else
    251     GKI_TRACE_1("GKI creating JOINABLE task %i", task_id);
    252 #endif
    253 
    254     /* On Android, the new tasks starts running before 'gki_cb.os.thread_id[task_id]' is initialized */
    255     /* Pass task_id to new task so it can initialize gki_cb.os.thread_id[task_id] for it calls GKI_wait */
    256     gki_pthread_info[task_id].task_id = task_id;
    257     gki_pthread_info[task_id].task_entry = task_entry;
    258     gki_pthread_info[task_id].params = 0;
    259     gki_pthread_info[task_id].pCond = (pthread_cond_t*)pCondVar;
    260     gki_pthread_info[task_id].pMutex = (pthread_mutex_t*)pMutex;
    261 
    262     ret = pthread_create( &gki_cb.os.thread_id[task_id],
    263               &attr1,
    264               (void *)gki_task_entry,
    265               &gki_pthread_info[task_id]);
    266 
    267     if (ret != 0)
    268     {
    269          GKI_TRACE_2("pthread_create failed(%d), %s!", ret, taskname);
    270          return GKI_FAILURE;
    271     }
    272 
    273     if(pthread_getschedparam(gki_cb.os.thread_id[task_id], &policy, &param)==0)
    274     {
    275 #if defined(PBS_SQL_TASK)
    276          if (task_id == PBS_SQL_TASK)
    277          {
    278              GKI_TRACE_0("PBS SQL lowest priority task");
    279              policy = SCHED_NORMAL;
    280          }
    281          else
    282 #endif
    283          {
    284              policy = SCHED_RR;
    285              param.sched_priority = 30 - task_id - 2;
    286          }
    287          pthread_setschedparam(gki_cb.os.thread_id[task_id], policy, &param);
    288      }
    289 
    290     GKI_TRACE_6( "Leaving GKI_create_task %x %d %x %s %x %d",
    291               task_entry,
    292               task_id,
    293               gki_cb.os.thread_id[task_id],
    294               taskname,
    295               stack,
    296               stacksize);
    297 
    298     return (GKI_SUCCESS);
    299 }
    300 
    301 /*******************************************************************************
    302 **
    303 ** Function         GKI_shutdown
    304 **
    305 ** Description      shutdowns the GKI tasks/threads in from max task id to 0 and frees
    306 **                  pthread resources!
    307 **                  IMPORTANT: in case of join method, GKI_shutdown must be called outside
    308 **                  a GKI thread context!
    309 **
    310 ** Returns          void
    311 **
    312 *******************************************************************************/
    313 #define WAKE_LOCK_ID "brcm_nfca"
    314 #define PARTIAL_WAKE_LOCK 1
    315 extern int acquire_wake_lock(int lock, const char* id);
    316 extern int release_wake_lock(const char* id);
    317 
    318 void GKI_shutdown(void)
    319 {
    320     UINT8 task_id;
    321     volatile int    *p_run_cond = &gki_cb.os.no_timer_suspend;
    322     int     oldCOnd = 0;
    323 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    324     int i = 0;
    325 #else
    326     int result;
    327 #endif
    328 
    329     /* release threads and set as TASK_DEAD. going from low to high priority fixes
    330      * GKI_exception problem due to btu->hci sleep request events  */
    331     for (task_id = GKI_MAX_TASKS; task_id > 0; task_id--)
    332     {
    333         if (gki_cb.com.OSRdyTbl[task_id - 1] != TASK_DEAD)
    334         {
    335             gki_cb.com.OSRdyTbl[task_id - 1] = TASK_DEAD;
    336 
    337             /* paranoi settings, make sure that we do not execute any mailbox events */
    338             gki_cb.com.OSWaitEvt[task_id-1] &= ~(TASK_MBOX_0_EVT_MASK|TASK_MBOX_1_EVT_MASK|
    339                                                 TASK_MBOX_2_EVT_MASK|TASK_MBOX_3_EVT_MASK);
    340             GKI_send_event(task_id - 1, EVENT_MASK(GKI_SHUTDOWN_EVT));
    341 
    342 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    343             i = 0;
    344 
    345             while ((gki_cb.com.OSWaitEvt[task_id - 1] != 0) && (++i < 10))
    346                 usleep(100 * 1000);
    347 #else
    348             /* wait for proper Arnold Schwarzenegger task state */
    349             result = pthread_join( gki_cb.os.thread_id[task_id-1], NULL );
    350             if ( result < 0 )
    351             {
    352                 GKI_TRACE_1( "pthread_join() FAILED: result: %d", result );
    353             }
    354 #endif
    355             GKI_TRACE_1( "GKI_shutdown(): task %s dead", gki_cb.com.OSTName[task_id]);
    356             GKI_exit_task(task_id - 1);
    357         }
    358     }
    359 
    360     /* Destroy mutex and condition variable objects */
    361     pthread_mutex_destroy(&gki_cb.os.GKI_mutex);
    362     /*    pthread_mutex_destroy(&GKI_sched_mutex); */
    363 #if (GKI_DEBUG == TRUE)
    364     pthread_mutex_destroy(&gki_cb.os.GKI_trace_mutex);
    365 #endif
    366     /*    pthread_mutex_destroy(&thread_delay_mutex);
    367      pthread_cond_destroy (&thread_delay_cond); */
    368 #if ( FALSE == GKI_PTHREAD_JOINABLE )
    369     i = 0;
    370 #endif
    371 
    372 #ifdef NO_GKI_RUN_RETURN
    373     shutdown_timer = 1;
    374 #endif
    375     if (gki_cb.os.gki_timer_wake_lock_on)
    376     {
    377         GKI_TRACE_0("GKI_shutdown :  release_wake_lock(brcm_btld)");
    378         release_wake_lock(WAKE_LOCK_ID);
    379         gki_cb.os.gki_timer_wake_lock_on = 0;
    380     }
    381     oldCOnd = *p_run_cond;
    382     *p_run_cond = GKI_TIMER_TICK_EXIT_COND;
    383     if (oldCOnd == GKI_TIMER_TICK_STOP_COND)
    384         pthread_cond_signal( &gki_cb.os.gki_timer_cond );
    385 
    386 }
    387 
    388 /*******************************************************************************
    389  **
    390  ** Function        GKI_run
    391  **
    392  ** Description     This function runs a task
    393  **
    394  ** Parameters:     start: TRUE start system tick (again), FALSE stop
    395  **
    396  ** Returns         void
    397  **
    398  *********************************************************************************/
    399 void gki_system_tick_start_stop_cback(BOOLEAN start)
    400 {
    401     tGKI_OS         *p_os = &gki_cb.os;
    402     volatile int    *p_run_cond = &p_os->no_timer_suspend;
    403     volatile static int wake_lock_count;
    404     if ( FALSE == start )
    405     {
    406         /* this can lead to a race condition. however as we only read this variable in the timer loop
    407          * we should be fine with this approach. otherwise uncomment below mutexes.
    408          */
    409         /* GKI_disable(); */
    410         *p_run_cond = GKI_TIMER_TICK_STOP_COND;
    411         /* GKI_enable(); */
    412 #ifdef GKI_TICK_TIMER_DEBUG
    413         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> STOP GKI_timer_update(), wake_lock_count:%d", --wake_lock_count);
    414 #endif
    415         release_wake_lock(WAKE_LOCK_ID);
    416         gki_cb.os.gki_timer_wake_lock_on = 0;
    417     }
    418     else
    419     {
    420         /* restart GKI_timer_update() loop */
    421         acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
    422         gki_cb.os.gki_timer_wake_lock_on = 1;
    423         *p_run_cond = GKI_TIMER_TICK_RUN_COND;
    424         pthread_mutex_lock( &p_os->gki_timer_mutex );
    425         pthread_cond_signal( &p_os->gki_timer_cond );
    426         pthread_mutex_unlock( &p_os->gki_timer_mutex );
    427 
    428 #ifdef GKI_TICK_TIMER_DEBUG
    429         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> START GKI_timer_update(), wake_lock_count:%d", ++wake_lock_count );
    430 #endif
    431     }
    432 }
    433 
    434 
    435 /*******************************************************************************
    436 **
    437 ** Function         timer_thread
    438 **
    439 ** Description      Timer thread
    440 **
    441 ** Parameters:      id  - (input) timer ID
    442 **
    443 ** Returns          void
    444 **
    445 *********************************************************************************/
    446 #ifdef NO_GKI_RUN_RETURN
    447 void timer_thread(signed long id)
    448 {
    449     GKI_TRACE_1("%s enter", __func__);
    450     struct timespec delay;
    451     int timeout = 1000;  /* 10  ms per system tick  */
    452     int err;
    453 
    454     while(!shutdown_timer)
    455     {
    456         delay.tv_sec = timeout / 1000;
    457         delay.tv_nsec = 1000 * 1000 * (timeout%1000);
    458 
    459         /* [u]sleep can't be used because it uses SIGALRM */
    460 
    461         do
    462         {
    463             err = nanosleep(&delay, &delay);
    464         } while (err < 0 && errno ==EINTR);
    465 
    466         GKI_timer_update(1);
    467     }
    468     GKI_TRACE_1("%s exit", __func__);
    469     pthread_exit(NULL);
    470 }
    471 #endif
    472 
    473 /*******************************************************************************
    474 **
    475 ** Function         GKI_run
    476 **
    477 ** Description      This function runs a task
    478 **
    479 ** Parameters:      p_task_id  - (input) pointer to task id
    480 **
    481 ** Returns          void
    482 **
    483 ** NOTE             This function is only needed for operating systems where
    484 **                  starting a task is a 2-step process. Most OS's do it in
    485 **                  one step, If your OS does it in one step, this function
    486 **                  should be empty.
    487 *********************************************************************************/
    488 void GKI_run (void *p_task_id)
    489 {
    490     GKI_TRACE_1("%s enter", __func__);
    491     struct timespec delay;
    492     int err = 0;
    493     volatile int * p_run_cond = &gki_cb.os.no_timer_suspend;
    494 
    495 #ifndef GKI_NO_TICK_STOP
    496     /* register start stop function which disable timer loop in GKI_run() when no timers are
    497      * in any GKI/BTA/BTU this should save power when BTLD is idle! */
    498     GKI_timer_queue_register_callback( gki_system_tick_start_stop_cback );
    499     APPL_TRACE_DEBUG0( "GKI_run(): Start/Stop GKI_timer_update_registered!" );
    500 #endif
    501 
    502 #ifdef NO_GKI_RUN_RETURN
    503     GKI_TRACE_0("GKI_run == NO_GKI_RUN_RETURN");
    504     pthread_attr_t timer_attr;
    505 
    506     shutdown_timer = 0;
    507 
    508     pthread_attr_init(&timer_attr);
    509     pthread_attr_setdetachstate(&timer_attr, PTHREAD_CREATE_DETACHED);
    510     if (pthread_create( &timer_thread_id,
    511               &timer_attr,
    512               timer_thread,
    513               NULL) != 0 )
    514     {
    515         GKI_TRACE_0("GKI_run: pthread_create failed to create timer_thread!");
    516         return GKI_FAILURE;
    517     }
    518 #else
    519     GKI_TRACE_2("GKI_run, run_cond(%x)=%d ", p_run_cond, *p_run_cond);
    520     for (;GKI_TIMER_TICK_EXIT_COND != *p_run_cond;)
    521     {
    522         do
    523         {
    524             /* adjust hear bit tick in btld by changning TICKS_PER_SEC!!!!! this formula works only for
    525              * 1-1000ms heart beat units! */
    526             delay.tv_sec = LINUX_SEC / 1000;
    527             delay.tv_nsec = 1000 * 1000 * (LINUX_SEC % 1000);
    528 
    529             /* [u]sleep can't be used because it uses SIGALRM */
    530             do
    531             {
    532                 err = nanosleep(&delay, &delay);
    533             } while (err < 0 && errno == EINTR);
    534 
    535             /* the unit should be alsways 1 (1 tick). only if you vary for some reason heart beat tick
    536              * e.g. power saving you may want to provide more ticks
    537              */
    538             GKI_timer_update( 1 );
    539             /* BT_TRACE_2( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, "update: tv_sec: %d, tv_nsec: %d", delay.tv_sec, delay.tv_nsec ); */
    540         } while ( GKI_TIMER_TICK_RUN_COND == *p_run_cond);
    541 
    542         /* currently on reason to exit above loop is no_timer_suspend == GKI_TIMER_TICK_STOP_COND
    543          * block timer main thread till re-armed by  */
    544 #ifdef GKI_TICK_TIMER_DEBUG
    545         BT_TRACE_0( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> SUSPENDED GKI_timer_update()" );
    546 #endif
    547         if (GKI_TIMER_TICK_EXIT_COND != *p_run_cond) {
    548             GKI_TRACE_1("%s waiting timer mutex", __func__);
    549             pthread_mutex_lock( &gki_cb.os.gki_timer_mutex );
    550             pthread_cond_wait( &gki_cb.os.gki_timer_cond, &gki_cb.os.gki_timer_mutex );
    551             pthread_mutex_unlock( &gki_cb.os.gki_timer_mutex );
    552             GKI_TRACE_1("%s exited timer mutex", __func__);
    553         }
    554         /* potentially we need to adjust os gki_cb.com.OSTicks */
    555 
    556 #ifdef GKI_TICK_TIMER_DEBUG
    557         BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, ">>> RESTARTED GKI_timer_update(): run_cond: %d",
    558                     *p_run_cond );
    559 #endif
    560     } /* for */
    561 #endif
    562     GKI_TRACE_1("%s exit", __func__);
    563     return(0);
    564 }
    565 
    566 
    567 /*******************************************************************************
    568 **
    569 ** Function         GKI_stop
    570 **
    571 ** Description      This function is called to stop
    572 **                  the tasks and timers when the system is being stopped
    573 **
    574 ** Returns          void
    575 **
    576 ** NOTE             This function is NOT called by the Widcomm stack and
    577 **                  profiles. If you want to use it in your own implementation,
    578 **                  put specific code here.
    579 **
    580 *******************************************************************************/
    581 void GKI_stop (void)
    582 {
    583     UINT8 task_id;
    584 
    585     /*  gki_queue_timer_cback(FALSE); */
    586     /* TODO - add code here if needed*/
    587 
    588     for(task_id = 0; task_id<GKI_MAX_TASKS; task_id++)
    589     {
    590         if(gki_cb.com.OSRdyTbl[task_id] != TASK_DEAD)
    591         {
    592             GKI_exit_task(task_id);
    593         }
    594     }
    595 }
    596 
    597 
    598 /*******************************************************************************
    599 **
    600 ** Function         GKI_wait
    601 **
    602 ** Description      This function is called by tasks to wait for a specific
    603 **                  event or set of events. The task may specify the duration
    604 **                  that it wants to wait for, or 0 if infinite.
    605 **
    606 ** Parameters:      flag -    (input) the event or set of events to wait for
    607 **                  timeout - (input) the duration that the task wants to wait
    608 **                                    for the specific events (in system ticks)
    609 **
    610 **
    611 ** Returns          the event mask of received events or zero if timeout
    612 **
    613 *******************************************************************************/
    614 UINT16 GKI_wait (UINT16 flag, UINT32 timeout)
    615 {
    616     UINT16 evt;
    617     UINT8 rtask;
    618     struct timespec abstime = { 0, 0 };
    619     int sec;
    620     int nano_sec;
    621 
    622     rtask = GKI_get_taskid();
    623     GKI_TRACE_3("GKI_wait %d %x %d", rtask, flag, timeout);
    624     if (rtask >= GKI_MAX_TASKS) {
    625         pthread_exit(NULL);
    626         return 0;
    627     }
    628 
    629     gki_pthread_info_t* p_pthread_info = &gki_pthread_info[rtask];
    630     if (p_pthread_info->pCond != NULL && p_pthread_info->pMutex != NULL) {
    631         int ret;
    632         GKI_TRACE_3("GKI_wait task=%i, pCond/pMutex = %x/%x", rtask, p_pthread_info->pCond, p_pthread_info->pMutex);
    633         ret = pthread_mutex_lock(p_pthread_info->pMutex);
    634         ret = pthread_cond_signal(p_pthread_info->pCond);
    635         ret = pthread_mutex_unlock(p_pthread_info->pMutex);
    636         p_pthread_info->pMutex = NULL;
    637         p_pthread_info->pCond = NULL;
    638     }
    639     gki_cb.com.OSWaitForEvt[rtask] = flag;
    640 
    641     /* protect OSWaitEvt[rtask] from modification from an other thread */
    642     pthread_mutex_lock(&gki_cb.os.thread_evt_mutex[rtask]);
    643 
    644 #if 0 /* for clean scheduling we probably should always call pthread_cond_wait() */
    645     /* Check if anything in any of the mailboxes. There is a potential race condition where OSTaskQFirst[rtask]
    646      has been modified. however this should only result in addtional call to  pthread_cond_wait() but as
    647      the cond is met, it will exit immediately (depending on schedulling) */
    648     if (gki_cb.com.OSTaskQFirst[rtask][0])
    649     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_0_EVT_MASK;
    650     if (gki_cb.com.OSTaskQFirst[rtask][1])
    651     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_1_EVT_MASK;
    652     if (gki_cb.com.OSTaskQFirst[rtask][2])
    653     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_2_EVT_MASK;
    654     if (gki_cb.com.OSTaskQFirst[rtask][3])
    655     gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_3_EVT_MASK;
    656 #endif
    657 
    658     if (!(gki_cb.com.OSWaitEvt[rtask] & flag))
    659     {
    660         if (timeout)
    661         {
    662             //            timeout = GKI_MS_TO_TICKS(timeout);     /* convert from milliseconds to ticks */
    663 
    664             /* get current system time */
    665             //            clock_gettime(CLOCK_MONOTONIC, &currSysTime);
    666             //            abstime.tv_sec = currSysTime.time;
    667             //            abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
    668             clock_gettime(CLOCK_MONOTONIC, &abstime);
    669 
    670             /* add timeout */
    671             sec = timeout / 1000;
    672             nano_sec = (timeout % 1000) * NANOSEC_PER_MILLISEC;
    673             abstime.tv_nsec += nano_sec;
    674             if (abstime.tv_nsec > NSEC_PER_SEC)
    675             {
    676                 abstime.tv_sec += (abstime.tv_nsec / NSEC_PER_SEC);
    677                 abstime.tv_nsec = abstime.tv_nsec % NSEC_PER_SEC;
    678             }
    679             abstime.tv_sec += sec;
    680 
    681             pthread_cond_timedwait_monotonic(&gki_cb.os.thread_evt_cond[rtask],
    682                     &gki_cb.os.thread_evt_mutex[rtask], &abstime);
    683 
    684         }
    685         else
    686         {
    687             pthread_cond_wait(&gki_cb.os.thread_evt_cond[rtask], &gki_cb.os.thread_evt_mutex[rtask]);
    688         }
    689 
    690         /* TODO: check, this is probably neither not needed depending on phtread_cond_wait() implmentation,
    691          e.g. it looks like it is implemented as a counter in which case multiple cond_signal
    692          should NOT be lost! */
    693         // we are waking up after waiting for some events, so refresh variables
    694         // no need to call GKI_disable() here as we know that we will have some events as we've been waking up after condition pending or timeout
    695         if (gki_cb.com.OSTaskQFirst[rtask][0])
    696             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_0_EVT_MASK;
    697         if (gki_cb.com.OSTaskQFirst[rtask][1])
    698             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_1_EVT_MASK;
    699         if (gki_cb.com.OSTaskQFirst[rtask][2])
    700             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_2_EVT_MASK;
    701         if (gki_cb.com.OSTaskQFirst[rtask][3])
    702             gki_cb.com.OSWaitEvt[rtask] |= TASK_MBOX_3_EVT_MASK;
    703 
    704         if (gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
    705         {
    706             gki_cb.com.OSWaitEvt[rtask] = 0;
    707             /* unlock thread_evt_mutex as pthread_cond_wait() does auto lock when cond is met */
    708             pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[rtask]);
    709             BT_TRACE_1( TRACE_LAYER_HCI, TRACE_TYPE_DEBUG, "GKI TASK_DEAD received. exit thread %d...", rtask );
    710 
    711             gki_cb.os.thread_id[rtask] = 0;
    712             pthread_exit(NULL);
    713             return (EVENT_MASK(GKI_SHUTDOWN_EVT));
    714         }
    715     }
    716 
    717     /* Clear the wait for event mask */
    718     gki_cb.com.OSWaitForEvt[rtask] = 0;
    719 
    720     /* Return only those bits which user wants... */
    721     evt = gki_cb.com.OSWaitEvt[rtask] & flag;
    722 
    723     /* Clear only those bits which user wants... */
    724     gki_cb.com.OSWaitEvt[rtask] &= ~flag;
    725 
    726     /* unlock thread_evt_mutex as pthread_cond_wait() does auto lock mutex when cond is met */
    727     pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[rtask]);
    728     GKI_TRACE_4("GKI_wait %d %x %d %x resumed", rtask, flag, timeout, evt);
    729 
    730     return (evt);
    731 }
    732 
    733 
    734 /*******************************************************************************
    735 **
    736 ** Function         GKI_delay
    737 **
    738 ** Description      This function is called by tasks to sleep unconditionally
    739 **                  for a specified amount of time. The duration is in milliseconds
    740 **
    741 ** Parameters:      timeout -    (input) the duration in milliseconds
    742 **
    743 ** Returns          void
    744 **
    745 *******************************************************************************/
    746 
    747 void GKI_delay (UINT32 timeout)
    748 {
    749     UINT8 rtask = GKI_get_taskid();
    750     struct timespec delay;
    751     int err;
    752 
    753     GKI_TRACE_2("GKI_delay %d %d", rtask, timeout);
    754 
    755     delay.tv_sec = timeout / 1000;
    756     delay.tv_nsec = 1000 * 1000 * (timeout%1000);
    757 
    758     /* [u]sleep can't be used because it uses SIGALRM */
    759 
    760     do {
    761         err = nanosleep(&delay, &delay);
    762     } while (err < 0 && errno ==EINTR);
    763 
    764     /* Check if task was killed while sleeping */
    765     /* NOTE
    766     **      if you do not implement task killing, you do not
    767     **      need this check.
    768     */
    769     if (rtask && gki_cb.com.OSRdyTbl[rtask] == TASK_DEAD)
    770     {
    771     }
    772 
    773     GKI_TRACE_2("GKI_delay %d %d done", rtask, timeout);
    774     return;
    775 }
    776 
    777 
    778 /*******************************************************************************
    779 **
    780 ** Function         GKI_send_event
    781 **
    782 ** Description      This function is called by tasks to send events to other
    783 **                  tasks. Tasks can also send events to themselves.
    784 **
    785 ** Parameters:      task_id -  (input) The id of the task to which the event has to
    786 **                  be sent
    787 **                  event   -  (input) The event that has to be sent
    788 **
    789 **
    790 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
    791 **
    792 *******************************************************************************/
    793 UINT8 GKI_send_event (UINT8 task_id, UINT16 event)
    794 {
    795     GKI_TRACE_2("GKI_send_event %d %x", task_id, event);
    796 
    797     /* use efficient coding to avoid pipeline stalls */
    798     if (task_id < GKI_MAX_TASKS)
    799     {
    800         /* protect OSWaitEvt[task_id] from manipulation in GKI_wait() */
    801         pthread_mutex_lock(&gki_cb.os.thread_evt_mutex[task_id]);
    802 
    803         /* Set the event bit */
    804         gki_cb.com.OSWaitEvt[task_id] |= event;
    805 
    806         pthread_cond_signal(&gki_cb.os.thread_evt_cond[task_id]);
    807 
    808         pthread_mutex_unlock(&gki_cb.os.thread_evt_mutex[task_id]);
    809 
    810         GKI_TRACE_2("GKI_send_event %d %x done", task_id, event);
    811         return ( GKI_SUCCESS );
    812     }
    813     return (GKI_FAILURE);
    814 }
    815 
    816 
    817 /*******************************************************************************
    818 **
    819 ** Function         GKI_isend_event
    820 **
    821 ** Description      This function is called from ISRs to send events to other
    822 **                  tasks. The only difference between this function and GKI_send_event
    823 **                  is that this function assumes interrupts are already disabled.
    824 **
    825 ** Parameters:      task_id -  (input) The destination task Id for the event.
    826 **                  event   -  (input) The event flag
    827 **
    828 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
    829 **
    830 ** NOTE             This function is NOT called by the Widcomm stack and
    831 **                  profiles. If you want to use it in your own implementation,
    832 **                  put your code here, otherwise you can delete the entire
    833 **                  body of the function.
    834 **
    835 *******************************************************************************/
    836 UINT8 GKI_isend_event (UINT8 task_id, UINT16 event)
    837 {
    838 
    839     GKI_TRACE_2("GKI_isend_event %d %x", task_id, event);
    840     GKI_TRACE_2("GKI_isend_event %d %x done", task_id, event);
    841     return    GKI_send_event(task_id, event);
    842 }
    843 
    844 
    845 /*******************************************************************************
    846 **
    847 ** Function         GKI_get_taskid
    848 **
    849 ** Description      This function gets the currently running task ID.
    850 **
    851 ** Returns          task ID
    852 **
    853 ** NOTE             The Widcomm upper stack and profiles may run as a single task.
    854 **                  If you only have one GKI task, then you can hard-code this
    855 **                  function to return a '1'. Otherwise, you should have some
    856 **                  OS-specific method to determine the current task.
    857 **
    858 *******************************************************************************/
    859 UINT8 GKI_get_taskid (void)
    860 {
    861     int i;
    862 
    863     pthread_t thread_id = pthread_self( );
    864     for (i = 0; i < GKI_MAX_TASKS; i++) {
    865         if (gki_cb.os.thread_id[i] == thread_id) {
    866             GKI_TRACE_2("GKI_get_taskid %x %d done", thread_id, i);
    867             return(i);
    868         }
    869     }
    870 
    871     GKI_TRACE_1("GKI_get_taskid: thread id = %x, task id = -1", thread_id);
    872 
    873     return(-1);
    874 }
    875 
    876 /*******************************************************************************
    877 **
    878 ** Function         GKI_map_taskname
    879 **
    880 ** Description      This function gets the task name of the taskid passed as arg.
    881 **                  If GKI_MAX_TASKS is passed as arg the currently running task
    882 **                  name is returned
    883 **
    884 ** Parameters:      task_id -  (input) The id of the task whose name is being
    885 **                  sought. GKI_MAX_TASKS is passed to get the name of the
    886 **                  currently running task.
    887 **
    888 ** Returns          pointer to task name
    889 **
    890 ** NOTE             this function needs no customization
    891 **
    892 *******************************************************************************/
    893 INT8 *GKI_map_taskname (UINT8 task_id)
    894 {
    895     GKI_TRACE_1("GKI_map_taskname %d", task_id);
    896 
    897     if (task_id < GKI_MAX_TASKS)
    898     {
    899         GKI_TRACE_2("GKI_map_taskname %d %s done", task_id, gki_cb.com.OSTName[task_id]);
    900          return (gki_cb.com.OSTName[task_id]);
    901     }
    902     else if (task_id == GKI_MAX_TASKS )
    903     {
    904         return (gki_cb.com.OSTName[GKI_get_taskid()]);
    905     }
    906     else
    907     {
    908         return "BAD";
    909     }
    910 }
    911 
    912 
    913 /*******************************************************************************
    914 **
    915 ** Function         GKI_enable
    916 **
    917 ** Description      This function enables interrupts.
    918 **
    919 ** Returns          void
    920 **
    921 *******************************************************************************/
    922 void GKI_enable (void)
    923 {
    924     GKI_TRACE_0("GKI_enable");
    925     pthread_mutex_unlock(&gki_cb.os.GKI_mutex);
    926 /* 	pthread_mutex_xx is nesting save, no need for this: already_disabled = 0; */
    927     GKI_TRACE_0("Leaving GKI_enable");
    928     return;
    929 }
    930 
    931 
    932 /*******************************************************************************
    933 **
    934 ** Function         GKI_disable
    935 **
    936 ** Description      This function disables interrupts.
    937 **
    938 ** Returns          void
    939 **
    940 *******************************************************************************/
    941 
    942 void GKI_disable (void)
    943 {
    944     //GKI_TRACE_0("GKI_disable");
    945 
    946 /*	pthread_mutex_xx is nesting save, no need for this: if (!already_disabled) {
    947     already_disabled = 1; */
    948     		pthread_mutex_lock(&gki_cb.os.GKI_mutex);
    949 /*  } */
    950     //GKI_TRACE_0("Leaving GKI_disable");
    951     return;
    952 }
    953 
    954 
    955 /*******************************************************************************
    956 **
    957 ** Function         GKI_exception
    958 **
    959 ** Description      This function throws an exception.
    960 **                  This is normally only called for a nonrecoverable error.
    961 **
    962 ** Parameters:      code    -  (input) The code for the error
    963 **                  msg     -  (input) The message that has to be logged
    964 **
    965 ** Returns          void
    966 **
    967 *******************************************************************************/
    968 
    969 void GKI_exception (UINT16 code, char *msg)
    970 {
    971     UINT8 task_id;
    972     int i = 0;
    973 
    974     GKI_TRACE_ERROR_0( "GKI_exception(): Task State Table");
    975 
    976     for(task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
    977     {
    978         GKI_TRACE_ERROR_3( "TASK ID [%d] task name [%s] state [%d]",
    979                          task_id,
    980                          gki_cb.com.OSTName[task_id],
    981                          gki_cb.com.OSRdyTbl[task_id]);
    982     }
    983 
    984     GKI_TRACE_ERROR_2("GKI_exception %d %s", code, msg);
    985     GKI_TRACE_ERROR_0( "********************************************************************");
    986     GKI_TRACE_ERROR_2( "* GKI_exception(): %d %s", code, msg);
    987     GKI_TRACE_ERROR_0( "********************************************************************");
    988 
    989 #if (GKI_DEBUG == TRUE)
    990     GKI_disable();
    991 
    992     if (gki_cb.com.ExceptionCnt < GKI_MAX_EXCEPTION)
    993     {
    994         EXCEPTION_T *pExp;
    995 
    996         pExp =  &gki_cb.com.Exception[gki_cb.com.ExceptionCnt++];
    997         pExp->type = code;
    998         pExp->taskid = GKI_get_taskid();
    999         strncpy((char *)pExp->msg, msg, GKI_MAX_EXCEPTION_MSGLEN - 1);
   1000     }
   1001 
   1002     GKI_enable();
   1003 #endif
   1004 
   1005     GKI_TRACE_ERROR_2("GKI_exception %d %s done", code, msg);
   1006 
   1007 
   1008     return;
   1009 }
   1010 
   1011 
   1012 /*******************************************************************************
   1013 **
   1014 ** Function         GKI_get_time_stamp
   1015 **
   1016 ** Description      This function formats the time into a user area
   1017 **
   1018 ** Parameters:      tbuf -  (output) the address to the memory containing the
   1019 **                  formatted time
   1020 **
   1021 ** Returns          the address of the user area containing the formatted time
   1022 **                  The format of the time is ????
   1023 **
   1024 ** NOTE             This function is only called by OBEX.
   1025 **
   1026 *******************************************************************************/
   1027 INT8 *GKI_get_time_stamp (INT8 *tbuf)
   1028 {
   1029     UINT32 ms_time;
   1030     UINT32 s_time;
   1031     UINT32 m_time;
   1032     UINT32 h_time;
   1033     INT8   *p_out = tbuf;
   1034 
   1035     gki_cb.com.OSTicks = times(0);
   1036     ms_time = GKI_TICKS_TO_MS(gki_cb.com.OSTicks);
   1037     s_time  = ms_time/100;   /* 100 Ticks per second */
   1038     m_time  = s_time/60;
   1039     h_time  = m_time/60;
   1040 
   1041     ms_time -= s_time*100;
   1042     s_time  -= m_time*60;
   1043     m_time  -= h_time*60;
   1044 
   1045     *p_out++ = (INT8)((h_time / 10) + '0');
   1046     *p_out++ = (INT8)((h_time % 10) + '0');
   1047     *p_out++ = ':';
   1048     *p_out++ = (INT8)((m_time / 10) + '0');
   1049     *p_out++ = (INT8)((m_time % 10) + '0');
   1050     *p_out++ = ':';
   1051     *p_out++ = (INT8)((s_time / 10) + '0');
   1052     *p_out++ = (INT8)((s_time % 10) + '0');
   1053     *p_out++ = ':';
   1054     *p_out++ = (INT8)((ms_time / 10) + '0');
   1055     *p_out++ = (INT8)((ms_time % 10) + '0');
   1056     *p_out++ = ':';
   1057     *p_out   = 0;
   1058 
   1059     return (tbuf);
   1060 }
   1061 
   1062 
   1063 /*******************************************************************************
   1064 **
   1065 ** Function         GKI_register_mempool
   1066 **
   1067 ** Description      This function registers a specific memory pool.
   1068 **
   1069 ** Parameters:      p_mem -  (input) pointer to the memory pool
   1070 **
   1071 ** Returns          void
   1072 **
   1073 ** NOTE             This function is NOT called by the Widcomm stack and
   1074 **                  profiles. If your OS has different memory pools, you
   1075 **                  can tell GKI the pool to use by calling this function.
   1076 **
   1077 *******************************************************************************/
   1078 void GKI_register_mempool (void *p_mem)
   1079 {
   1080     gki_cb.com.p_user_mempool = p_mem;
   1081 
   1082     return;
   1083 }
   1084 
   1085 /*******************************************************************************
   1086 **
   1087 ** Function         GKI_os_malloc
   1088 **
   1089 ** Description      This function allocates memory
   1090 **
   1091 ** Parameters:      size -  (input) The size of the memory that has to be
   1092 **                  allocated
   1093 **
   1094 ** Returns          the address of the memory allocated, or NULL if failed
   1095 **
   1096 ** NOTE             This function is called by the Widcomm stack when
   1097 **                  dynamic memory allocation is used. (see dyn_mem.h)
   1098 **
   1099 *******************************************************************************/
   1100 void *GKI_os_malloc (UINT32 size)
   1101 {
   1102     return (malloc(size));
   1103 }
   1104 
   1105 /*******************************************************************************
   1106 **
   1107 ** Function         GKI_os_free
   1108 **
   1109 ** Description      This function frees memory
   1110 **
   1111 ** Parameters:      size -  (input) The address of the memory that has to be
   1112 **                  freed
   1113 **
   1114 ** Returns          void
   1115 **
   1116 ** NOTE             This function is NOT called by the Widcomm stack and
   1117 **                  profiles. It is only called from within GKI if dynamic
   1118 **
   1119 *******************************************************************************/
   1120 void GKI_os_free (void *p_mem)
   1121 {
   1122     if(p_mem != NULL)
   1123 		free(p_mem);
   1124     return;
   1125 }
   1126 
   1127 
   1128 /*******************************************************************************
   1129 **
   1130 ** Function         GKI_suspend_task()
   1131 **
   1132 ** Description      This function suspends the task specified in the argument.
   1133 **
   1134 ** Parameters:      task_id  - (input) the id of the task that has to suspended
   1135 **
   1136 ** Returns          GKI_SUCCESS if all OK, else GKI_FAILURE
   1137 **
   1138 ** NOTE             This function is NOT called by the Widcomm stack and
   1139 **                  profiles. If you want to implement task suspension capability,
   1140 **                  put specific code here.
   1141 **
   1142 *******************************************************************************/
   1143 UINT8 GKI_suspend_task (UINT8 task_id)
   1144 {
   1145     GKI_TRACE_1("GKI_suspend_task %d - NOT implemented", task_id);
   1146 
   1147 
   1148     GKI_TRACE_1("GKI_suspend_task %d done", task_id);
   1149 
   1150     return (GKI_SUCCESS);
   1151 }
   1152 
   1153 
   1154 /*******************************************************************************
   1155 **
   1156 ** Function         GKI_resume_task()
   1157 **
   1158 ** Description      This function resumes the task specified in the argument.
   1159 **
   1160 ** Parameters:      task_id  - (input) the id of the task that has to resumed
   1161 **
   1162 ** Returns          GKI_SUCCESS if all OK
   1163 **
   1164 ** NOTE             This function is NOT called by the Widcomm stack and
   1165 **                  profiles. If you want to implement task suspension capability,
   1166 **                  put specific code here.
   1167 **
   1168 *******************************************************************************/
   1169 UINT8 GKI_resume_task (UINT8 task_id)
   1170 {
   1171     GKI_TRACE_1("GKI_resume_task %d - NOT implemented", task_id);
   1172 
   1173 
   1174     GKI_TRACE_1("GKI_resume_task %d done", task_id);
   1175 
   1176     return (GKI_SUCCESS);
   1177 }
   1178 
   1179 
   1180 /*******************************************************************************
   1181 **
   1182 ** Function         GKI_exit_task
   1183 **
   1184 ** Description      This function is called to stop a GKI task.
   1185 **
   1186 ** Parameters:      task_id  - (input) the id of the task that has to be stopped
   1187 **
   1188 ** Returns          void
   1189 **
   1190 ** NOTE             This function is NOT called by the Widcomm stack and
   1191 **                  profiles. If you want to use it in your own implementation,
   1192 **                  put specific code here to kill a task.
   1193 **
   1194 *******************************************************************************/
   1195 void GKI_exit_task (UINT8 task_id)
   1196 {
   1197     GKI_disable();
   1198     gki_cb.com.OSRdyTbl[task_id] = TASK_DEAD;
   1199 
   1200     /* Destroy mutex and condition variable objects */
   1201     pthread_mutex_destroy(&gki_cb.os.thread_evt_mutex[task_id]);
   1202     pthread_cond_destroy (&gki_cb.os.thread_evt_cond[task_id]);
   1203     pthread_mutex_destroy(&gki_cb.os.thread_timeout_mutex[task_id]);
   1204     pthread_cond_destroy (&gki_cb.os.thread_timeout_cond[task_id]);
   1205 
   1206     GKI_enable();
   1207 
   1208 	//GKI_send_event(task_id, EVENT_MASK(GKI_SHUTDOWN_EVT));
   1209 
   1210     GKI_TRACE_1("GKI_exit_task %d done", task_id);
   1211     return;
   1212 }
   1213 
   1214 
   1215 /*******************************************************************************
   1216 **
   1217 ** Function         GKI_sched_lock
   1218 **
   1219 ** Description      This function is called by tasks to disable scheduler
   1220 **                  task context switching.
   1221 **
   1222 ** Returns          void
   1223 **
   1224 ** NOTE             This function is NOT called by the Widcomm stack and
   1225 **                  profiles. If you want to use it in your own implementation,
   1226 **                  put code here to tell the OS to disable context switching.
   1227 **
   1228 *******************************************************************************/
   1229 void GKI_sched_lock(void)
   1230 {
   1231     GKI_TRACE_0("GKI_sched_lock");
   1232     return;
   1233 }
   1234 
   1235 
   1236 /*******************************************************************************
   1237 **
   1238 ** Function         GKI_sched_unlock
   1239 **
   1240 ** Description      This function is called by tasks to enable scheduler switching.
   1241 **
   1242 ** Returns          void
   1243 **
   1244 ** NOTE             This function is NOT called by the Widcomm stack and
   1245 **                  profiles. If you want to use it in your own implementation,
   1246 **                  put code here to tell the OS to re-enable context switching.
   1247 **
   1248 *******************************************************************************/
   1249 void GKI_sched_unlock(void)
   1250 {
   1251     GKI_TRACE_0("GKI_sched_unlock");
   1252 }
   1253 
   1254 /*******************************************************************************
   1255 **
   1256 ** Function         GKI_shiftdown
   1257 **
   1258 ** Description      shift memory down (to make space to insert a record)
   1259 **
   1260 *******************************************************************************/
   1261 void GKI_shiftdown (UINT8 *p_mem, UINT32 len, UINT32 shift_amount)
   1262 {
   1263     register UINT8 *ps = p_mem + len - 1;
   1264     register UINT8 *pd = ps + shift_amount;
   1265     register UINT32 xx;
   1266 
   1267     for (xx = 0; xx < len; xx++)
   1268         *pd-- = *ps--;
   1269 }
   1270 
   1271 /*******************************************************************************
   1272 **
   1273 ** Function         GKI_shiftup
   1274 **
   1275 ** Description      shift memory up (to delete a record)
   1276 **
   1277 *******************************************************************************/
   1278 void GKI_shiftup (UINT8 *p_dest, UINT8 *p_src, UINT32 len)
   1279 {
   1280     register UINT8 *ps = p_src;
   1281     register UINT8 *pd = p_dest;
   1282     register UINT32 xx;
   1283 
   1284     for (xx = 0; xx < len; xx++)
   1285         *pd++ = *ps++;
   1286 }
   1287 
   1288 
   1289