Reduce time in critical sections

Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/wilink_6_1/Txn/TxnQueue.c b/wilink_6_1/Txn/TxnQueue.c
index 39b863f..d455e3d 100644
--- a/wilink_6_1/Txn/TxnQueue.c
+++ b/wilink_6_1/Txn/TxnQueue.c
@@ -361,11 +361,11 @@
         }
     }
 
+    context_LeaveCriticalSection (pTxnQ->hContext);
+
     /* Clear the calling function's queues (call function CB with status=RECOVERY) */
     txnQ_ClearQueues (pTxnQ, uFuncId);
 
-    context_LeaveCriticalSection (pTxnQ->hContext);
-
     /* Return COMPLETE to indicate that the restart was completed */
     return TXN_STATUS_COMPLETE;
 }
@@ -486,9 +486,7 @@
         TRACE0(pTxnQ->hReport, REPORT_SEVERITY_INFORMATION, "txnQ_TxnDoneCb(): Handling restart\n");
 
         /* First, Clear the restarted function queues  */
-        context_EnterCriticalSection (pTxnQ->hContext);
         txnQ_ClearQueues (pTxnQ, uFuncId);
-        context_LeaveCriticalSection (pTxnQ->hContext);
 
         /* Call function CB for current Txn with restart indication */
         TXN_PARAM_SET_STATUS(pTxn, TXN_PARAM_STATUS_RECOVERY);
@@ -769,22 +767,18 @@
     /* For all function priorities */
     for (uPrio = 0; uPrio < pTxnQ->aFuncInfo[uFuncId].uNumPrios; uPrio++)
     {
-        while (1) 
+        do
         {
+            context_EnterCriticalSection (pTxnQ->hContext);
             /* Dequeue Txn from current priority queue */
             pTxn = (TTxnStruct *) que_Dequeue (pTxnQ->aTxnQueues[uFuncId][uPrio]);
-
-            /* If NULL Txn (queue empty), exit while loop */
-            if (pTxn == NULL)
-            {
-                break;
-            }
+            context_LeaveCriticalSection (pTxnQ->hContext);
 
             /* 
              * Drop on Restart 
              * do not call fTxnQueueDoneCb (hCbHandle, pTxn) callback 
              */
-        }
+        } while (pTxn != NULL);
     }
 
     /* Clear state - for restart (doesn't call txnQ_Open) */
@@ -802,5 +796,3 @@
     que_Print(pTxnQ->aTxnQueues[TXN_FUNC_ID_WLAN][TXN_HIGH_PRIORITY]);
 }
 #endif /* TI_DBG */
-
-
diff --git a/wilink_6_1/stad/src/Ctrl_Interface/CmdHndlr.c b/wilink_6_1/stad/src/Ctrl_Interface/CmdHndlr.c
index be737c6..b60fa34 100644
--- a/wilink_6_1/stad/src/Ctrl_Interface/CmdHndlr.c
+++ b/wilink_6_1/stad/src/Ctrl_Interface/CmdHndlr.c
@@ -274,14 +274,14 @@
     /* Enter critical section to protect queue access */
     context_EnterCriticalSection (pCmdHndlr->hContext);
 
-	/* Enqueue the command (if failed, release memory and return NOK) */
+    /* Enqueue the command (if failed, release memory and return NOK) */
     eStatus = que_Enqueue (pCmdHndlr->hCmdQueue, (TI_HANDLE)pNewCmd);
     if (eStatus != TI_OK) 
-	{
-		os_printf("cmdPerform: Failed to enqueue new command\n");
-		os_SignalObjectFree (pCmdHndlr->hOs, pNewCmd->pSignalObject);
-		os_memoryFree (pCmdHndlr->hOs, pNewCmd, sizeof (TConfigCommand));
+    {
         context_LeaveCriticalSection (pCmdHndlr->hContext);  /* Leave critical section */
+        os_printf("cmdPerform: Failed to enqueue new command\n");
+        os_SignalObjectFree (pCmdHndlr->hOs, pNewCmd->pSignalObject);
+        os_memoryFree (pCmdHndlr->hOs, pNewCmd, sizeof (TConfigCommand));
         return TI_NOK;
     }
 
diff --git a/wilink_6_1/utils/timer.c b/wilink_6_1/utils/timer.c
index 39ca459..0fbd726 100644
--- a/wilink_6_1/utils/timer.c
+++ b/wilink_6_1/utils/timer.c
@@ -52,9 +52,9 @@
 /* The timer module structure (common to all timers) */
 typedef struct 
 {
-	TI_HANDLE   hOs;
-	TI_HANDLE   hReport;
-	TI_HANDLE   hContext;
+    TI_HANDLE   hOs;
+    TI_HANDLE   hReport;
+    TI_HANDLE   hContext;
     TI_UINT32   uContextId;     /* The ID allocated to this module on registration to context module */
     TI_HANDLE   hInitQueue;     /* Handle of the Init-Queue */
     TI_HANDLE   hOperQueue;     /* Handle of the Operational-Queue */
@@ -93,20 +93,20 @@
  */ 
 TI_HANDLE tmr_Create (TI_HANDLE hOs)
 {
-	TI_HANDLE hTimerModule;
+    TI_HANDLE hTimerModule;
 
-	/* allocate module object */
-	hTimerModule = os_memoryAlloc (hOs, sizeof(TTimerModule));
+    /* allocate module object */
+    hTimerModule = os_memoryAlloc (hOs, sizeof(TTimerModule));
 	
-	if (!hTimerModule)
-	{
-		WLAN_OS_REPORT (("tmr_Create():  Allocation failed!!\n"));
-		return NULL;
-	}
+    if (!hTimerModule)
+    {
+        WLAN_OS_REPORT (("tmr_Create():  Allocation failed!!\n"));
+        return NULL;
+    }
 	
     os_memoryZero (hOs, hTimerModule, (sizeof(TTimerModule)));
 
-	return (hTimerModule);
+    return (hTimerModule);
 }
 
 
@@ -128,7 +128,7 @@
     /* Alert if there are still timers that were not destroyed */
     if (pTimerModule->uTimersCount)
     {
-		WLAN_OS_REPORT (("tmr_Destroy():  ERROR - Destroying Timer module but not all timers were destroyed!!\n"));
+        WLAN_OS_REPORT (("tmr_Destroy():  ERROR - Destroying Timer module but not all timers were destroyed!!\n"));
     }
 
     /* Clear queues */
@@ -140,7 +140,7 @@
     que_Destroy (pTimerModule->hOperQueue);
 
     /* free module object */
-	os_memoryFree (pTimerModule->hOs, pTimerModule, sizeof(TTimerModule));
+    os_memoryFree (pTimerModule->hOs, pTimerModule, sizeof(TTimerModule));
 	
     return TI_OK;
 }
@@ -159,7 +159,7 @@
     TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
 
     /* free module object */
-	os_memoryFree (pTimerModule->hOs, pTimerModule, sizeof(TTimerModule));
+    os_memoryFree (pTimerModule->hOs, pTimerModule, sizeof(TTimerModule));
 	
     return TI_OK;
 }
@@ -179,19 +179,25 @@
 void tmr_ClearInitQueue (TI_HANDLE hTimerModule)
 {
     TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
+    TTimerInfo   *pTimerInfo;      /* The timer handle */
 
-    context_EnterCriticalSection (pTimerModule->hContext);
-    while (que_Dequeue (pTimerModule->hInitQueue) != NULL) {}
-    context_LeaveCriticalSection (pTimerModule->hContext);
+    do {
+        context_EnterCriticalSection (pTimerModule->hContext);
+        pTimerInfo = que_Dequeue (pTimerModule->hInitQueue);
+        context_LeaveCriticalSection (pTimerModule->hContext);
+    } while (pTimerInfo != NULL);
 }
 
 void tmr_ClearOperQueue (TI_HANDLE hTimerModule)
 {
     TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
+    TTimerInfo   *pTimerInfo;      /* The timer handle */
 
-    context_EnterCriticalSection (pTimerModule->hContext);
-    while (que_Dequeue (pTimerModule->hOperQueue) != NULL) {}
-    context_LeaveCriticalSection (pTimerModule->hContext);
+    do {
+        context_EnterCriticalSection (pTimerModule->hContext);
+        pTimerInfo = que_Dequeue (pTimerModule->hOperQueue);
+        context_LeaveCriticalSection (pTimerModule->hContext);
+    } while (pTimerInfo != NULL);
 }
 
 
@@ -212,7 +218,7 @@
  */ 
 void tmr_Init (TI_HANDLE hTimerModule, TI_HANDLE hOs, TI_HANDLE hReport, TI_HANDLE hContext)
 {
-	TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
+    TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
     TI_UINT32     uNodeHeaderOffset;
 
     pTimerModule->hOs           = hOs;
@@ -263,17 +269,19 @@
  */ 
 void tmr_UpdateDriverState (TI_HANDLE hTimerModule, TI_BOOL bOperState)
 {
-	TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
-
-    if (bOperState == pTimerModule->bOperState) 
-    {
-        TRACE1(pTimerModule->hReport, REPORT_SEVERITY_ERROR, "tmr_UpdateDriverState(): New bOperState (%d) is as current!\n", bOperState);
-        return;
-    }
+    TTimerModule *pTimerModule = (TTimerModule *)hTimerModule;
+    TTimerInfo   *pTimerInfo;      /* The timer handle */
 
     /* Enter critical section */
     context_EnterCriticalSection (pTimerModule->hContext);
 
+    if (bOperState == pTimerModule->bOperState) 
+    {
+        context_LeaveCriticalSection (pTimerModule->hContext);
+        TRACE1(pTimerModule->hReport, REPORT_SEVERITY_ERROR, "tmr_UpdateDriverState(): New bOperState (%d) is as current!\n", bOperState);
+        return;
+    }
+
     /* Save new state (TRUE means operational). */
     pTimerModule->bOperState = bOperState;
 
@@ -282,14 +290,22 @@
     {
         /* Increment the TWD initializations counter (for detecting recovery events). */
         pTimerModule->uTwdInitCount++;
-
-        /* Empty the init queue (obsolete). */
-        while (que_Dequeue (pTimerModule->hInitQueue) != NULL) {}
     }
-	
+
     /* Leave critical section */
     context_LeaveCriticalSection (pTimerModule->hContext);
 
+    /* If new state is operational */
+    if (bOperState) 
+    {
+        /* Empty the init queue (obsolete). */
+        do {
+            context_EnterCriticalSection (pTimerModule->hContext);
+            pTimerInfo = que_Dequeue (pTimerModule->hInitQueue);
+            context_LeaveCriticalSection (pTimerModule->hContext);
+        } while (pTimerInfo != NULL);
+    }
+
     /* If new state is operational, request switch to driver context for handling timer events */
     if (bOperState) 
     {
@@ -299,7 +315,6 @@
 
 
 
-
 /** 
  * \fn     tmr_CreateTimer
  * \brief  Create a new timer
@@ -313,27 +328,27 @@
  */ 
 TI_HANDLE tmr_CreateTimer (TI_HANDLE hTimerModule)
 {
-	TTimerModule *pTimerModule = (TTimerModule *)hTimerModule; /* The timer module handle */
+    TTimerModule *pTimerModule = (TTimerModule *)hTimerModule; /* The timer module handle */
     TTimerInfo   *pTimerInfo;  /* The created timer handle */
 
-	/* Allocate timer object */
-	pTimerInfo = os_memoryAlloc (pTimerModule->hOs, sizeof(TTimerInfo));
-	if (!pTimerInfo)
-	{
-		WLAN_OS_REPORT (("tmr_CreateTimer():  Timer allocation failed!!\n"));
-		return NULL;
-	}
+    /* Allocate timer object */
+    pTimerInfo = os_memoryAlloc (pTimerModule->hOs, sizeof(TTimerInfo));
+    if (!pTimerInfo)
+    {
+        WLAN_OS_REPORT (("tmr_CreateTimer():  Timer allocation failed!!\n"));
+        return NULL;
+    }
     os_memoryZero (pTimerModule->hOs, pTimerInfo, (sizeof(TTimerInfo)));
 
     /* Allocate OS-API timer, providing the common expiry callback with the current timer handle */
     pTimerInfo->hOsTimerObj = os_timerCreate(pTimerModule->hOs, tmr_GetExpiry, (TI_HANDLE)pTimerInfo);
-	if (!pTimerInfo->hOsTimerObj)
-	{
+    if (!pTimerInfo->hOsTimerObj)
+    {
         TRACE0(pTimerModule->hReport, REPORT_SEVERITY_CONSOLE ,"tmr_CreateTimer():  OS-API Timer allocation failed!!\n");
         os_memoryFree (pTimerModule->hOs, pTimerInfo, sizeof(TTimerInfo));
         WLAN_OS_REPORT (("tmr_CreateTimer():  OS-API Timer allocation failed!!\n"));
-		return NULL;
-	}
+        return NULL;
+    }
 
     /* Save the timer module handle in the created timer object (needed for the expiry callback) */
     pTimerInfo->hTimerModule = hTimerModule;
@@ -401,7 +416,7 @@
                      TI_BOOL       bPeriodic)
 {
     TTimerInfo   *pTimerInfo   = (TTimerInfo *)hTimerInfo;                 /* The timer handle */     
-	TTimerModule *pTimerModule = (TTimerModule *)pTimerInfo->hTimerModule; /* The timer module handle */
+    TTimerModule *pTimerModule = (TTimerModule *)pTimerInfo->hTimerModule; /* The timer module handle */
 
     /* Save the timer parameters. */
     pTimerInfo->fExpiryCbFunc            = fExpiryCbFunc;
@@ -432,7 +447,7 @@
 void tmr_StopTimer (TI_HANDLE hTimerInfo)
 {
     TTimerInfo   *pTimerInfo   = (TTimerInfo *)hTimerInfo;                 /* The timer handle */     
-	TTimerModule *pTimerModule = (TTimerModule *)pTimerInfo->hTimerModule; /* The timer module handle */
+    TTimerModule *pTimerModule = (TTimerModule *)pTimerInfo->hTimerModule; /* The timer module handle */
 
     /* Stop OS-API timer running */
     os_timerStop(pTimerModule->hOs, pTimerInfo->hOsTimerObj);
@@ -506,7 +521,7 @@
  */ 
 void tmr_HandleExpiry (TI_HANDLE hTimerModule)
 {
-	TTimerModule *pTimerModule = (TTimerModule *)hTimerModule; /* The timer module handle */
+    TTimerModule *pTimerModule = (TTimerModule *)hTimerModule; /* The timer module handle */
     TTimerInfo   *pTimerInfo;      /* The timer handle */     
     TI_BOOL       bTwdInitOccured; /* Indicates if TWD init occured since timer start */
 
@@ -575,8 +590,8 @@
 
     /* Print module parameters */
     WLAN_OS_REPORT(("tmr_PrintModule(): uContextId=%d, bOperState=%d, uTwdInitCount=%d, uTimersCount=%d\n", 
-        pTimerModule->uContextId, pTimerModule->bOperState, 
-        pTimerModule->uTwdInitCount, pTimerModule->uTimersCount));
+    pTimerModule->uContextId, pTimerModule->bOperState, 
+    pTimerModule->uTwdInitCount, pTimerModule->uTimersCount));
 
     /* Print Init Queue Info */
     WLAN_OS_REPORT(("tmr_PrintModule(): Init-Queue:\n")); 
@@ -593,10 +608,9 @@
     TTimerInfo   *pTimerInfo   = (TTimerInfo *)hTimerInfo;                 /* The timer handle */     
 
     WLAN_OS_REPORT(("tmr_PrintTimer(): uIntervalMs=%d, bPeriodic=%d, bOperStateWhenStarted=%d, uTwdInitCountWhenStarted=%d, hOsTimerObj=0x%x, fExpiryCbFunc=0x%x\n", 
-        pTimerInfo->uIntervalMsec, pTimerInfo->bPeriodic, pTimerInfo->bOperStateWhenStarted, 
-        pTimerInfo->uTwdInitCountWhenStarted, pTimerInfo->hOsTimerObj, pTimerInfo->fExpiryCbFunc));
+    pTimerInfo->uIntervalMsec, pTimerInfo->bPeriodic, pTimerInfo->bOperStateWhenStarted, 
+    pTimerInfo->uTwdInitCountWhenStarted, pTimerInfo->hOsTimerObj, pTimerInfo->fExpiryCbFunc));
 #endif
 }
 
 #endif /* TI_DBG */
-