ClRcT _clGmsDbClose(
           CL_IN    ClGmsDbT*  const  gmsDb,
           CL_IN    const ClUint64T   numOfGroups)
{
    ClRcT   rc = CL_OK;
    ClInt32T   i = 0x0;

    if (gmsDb == NULL)
        return CL_ERR_NULL_POINTER;

    for (i=0; i < numOfGroups; i++) 
    {
       rc =_clGmsDbDestroy(&gmsDb[i]); 

       if (rc != CL_OK) return rc;

       rc = clOsalMutexDelete(gmsDb[i].viewMutex);
       if (rc != CL_OK) return rc;

       rc = clOsalMutexDelete(gmsDb[i].trackMutex);
       if (rc != CL_OK) return rc;
    }
        
    clHeapFree((void*)gmsDb);

    return rc;
}
static ClRcT 
clBitmapInfoInitialize(ClBitmapInfoT  *pBitmapInfo, 
                       ClUint32T      bitNum)
{
    ClRcT          rc           = CL_OK;    

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    rc = clOsalMutexCreate(&(pBitmapInfo->bitmapLock));
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexCreate() rc: %x", rc));
        return rc;
    }

    pBitmapInfo->pBitmap  = NULL;
    pBitmapInfo->nBytes   = 0;
    pBitmapInfo->nBits    = 0;
    pBitmapInfo->nBitsSet = 0;
    rc = clBitmapRealloc(pBitmapInfo, bitNum);    
    if( CL_OK != rc )
    {
        clOsalMutexDelete(pBitmapInfo->bitmapLock);
        return rc;
    }
    pBitmapInfo->nBits = bitNum + 1; /* bitNum is 0-based FIXME i felt this is
                                      wrong*/
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
ClRcT
clBitmapDestroy(ClBitmapHandleT  hBitmap)
{
    ClRcT          rc           = CL_OK;
    ClBitmapInfoT  *pBitmapInfo = hBitmap;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    do
    {
        rc = clOsalMutexDelete(pBitmapInfo->bitmapLock);
    }while( CL_OSAL_ERR_MUTEX_EBUSY == rc );

    clHeapFree(pBitmapInfo->pBitmap);
    clHeapFree(pBitmapInfo);

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
/*
 * This api would be called when alarm library is being finalize
 */
ClRcT clAlarmPayloadCntDeleteAll(void)
{
    ClRcT rc = CL_OK;
    clOsalMutexLock(gClAlarmPayloadCntMutex);
    rc = clCntDelete(gPayloadCntHandle);
    clOsalMutexUnlock(gClAlarmPayloadCntMutex);    
    clOsalMutexDelete(gClAlarmPayloadCntMutex);

    if(CL_OK != rc)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("container deletion failed [%x]",rc));
        return rc;
    }
	return rc;
}
static ClRcT
tsActiveTimersQueueCreate (void)
{
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;
    CL_FUNC_ENTER();

    gActiveTimerQueue.pFirstTimer = NULL;
    gActiveTimerQueue.pLastTimer = NULL;


    returnCode = clOsalMutexCreate (&gActiveTimerQueue.timerMutex);
    if (returnCode != CL_OK) {
        CL_FUNC_EXIT();
        return (returnCode);
    }

    gActiveTimerQueue.tickInMilliSec = gTimerConfig.timerResolution;

    /* spawn off the timer-task */
    returnCode = clOsalTaskCreateAttached("TIMER TASK",
                                          CL_OSAL_SCHED_OTHER,
                                          CL_OSAL_THREAD_PRI_NOT_APPLICABLE,  /* gTimerConfig.timerTaskPriority: Normally it would be this, but OTHER scheduling has no priority */
                                          TIMER_TASK_STACK_SIZE,
                                          tsTimerTask,
                                          NULL,
                                          &gActiveTimerQueue.timerTaskId);

    if (returnCode != CL_OK) {
        if(CL_OK != clOsalMutexDelete (gActiveTimerQueue.timerMutex)) {
            CL_FUNC_EXIT();
            return(returnCode);
        }
        CL_FUNC_EXIT();
        return (returnCode);
    }

    gActiveTimerQueue.timerServiceInitialized = 1;

    CL_FUNC_EXIT();
    return (CL_OK);
}
static ClRcT
tsActiveTimersQueueDestroy (void)
{
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;
    TsTimer_t* pUserTimer = NULL;
    TsTimer_t* pNextUserTimer = NULL;

    CL_FUNC_ENTER();
    returnCode = clOsalMutexLock (gActiveTimerQueue.timerMutex);

    if (returnCode != CL_OK) {
        CL_FUNC_EXIT();
        return (returnCode);
    }

    pNextUserTimer = NULL;

    for (pUserTimer = gActiveTimerQueue.pFirstTimer;
            pUserTimer != NULL;
            pUserTimer = pNextUserTimer) {

        pNextUserTimer = pUserTimer->pNextActiveTimer;

        returnCode = clOsalMutexUnlock (gActiveTimerQueue.timerMutex);

        returnCode = clTimerDelete ((ClTimerHandleT*)&pUserTimer);

        returnCode = clOsalMutexLock (gActiveTimerQueue.timerMutex);
    }

    gActiveTimerQueue.timerServiceInitialized = 0;

    returnCode = clOsalMutexUnlock (gActiveTimerQueue.timerMutex);

    returnCode = clOsalMutexDelete (gActiveTimerQueue.timerMutex);

    CL_FUNC_EXIT();
    return (returnCode);
}
ClRcT   ckptShutDown()
{
    /*
     * Uninstall the notification callback function
     */
    if(CL_HANDLE_INVALID_VALUE != gIocCallbackHandle)
    {
        clCpmNotificationCallbackUninstall(&gIocCallbackHandle);
    }
    /*
     * Inform peers about the departure.
     */
    ckptSvrDepartureAnnounce();

    /*
     * Finalize the event library.
     */
    ckptEventSvcFinalize();

    /*
     * Uninstall the native client table.
     */
    clEoClientUninstall (gCkptSvr->eoHdl, CL_EO_NATIVE_COMPONENT_TABLE_ID);

    /*
     * Finalize the gms library.
     */
    clGmsFinalize(gCkptSvr->gmsHdl);

    /*
     * Cleanup the resources acquired bu the ckpt server.
     */
    clOsalMutexLock(&gCkptSvr->ckptClusterSem);
    ckptSvrCbFree(gCkptSvr);
    clOsalMutexUnlock(&gCkptSvr->ckptClusterSem);
    clOsalMutexDelete(gCkptSvr->ckptActiveSem);
    return CL_OK;
}
ClRcT clEvtTestAppFinalize()
{

    ClRcT rc = CL_OK;

    ClEoExecutionObjT *pEoObj;

    /*
     * Delete the list maintaining the mapping b/n Init Name & evtHandle 
     */
    clCntDelete(gEvtTestInitInfo);

    /*
     * Delete the Mutes for the List in Init 
     */
    clOsalMutexDelete(gEvtTestInitMutex);

    rc = clEoMyEoObjectGet(&pEoObj);

    rc = clEoClientUninstall(pEoObj, CL_EO_NATIVE_COMPONENT_TABLE_ID);

    return CL_OK;
}
ClRcT
clAmsSAFinalize(
        CL_IN       ClAmsClientHandleT          amsHandle)
{

    ClRcT                                       rc;
    clAmsClientFinalizeRequestT                 req;
    clAmsClientFinalizeResponseT                *res = NULL;
    struct ams_instance                         *ams_instance = NULL;

    /* 
     * Checkout the client handle 
     */

    if ( (rc = clHandleCheckout(
                    handle_database,
                    amsHandle,
                    (void *)&ams_instance))
            != CL_OK )
        goto error;

    if ( ( rc = clOsalMutexLock(ams_instance->response_mutex)) != CL_OK )
        goto error;

    /*
     * Another thread has already started finalizing
     */

    if (ams_instance->finalize)
    {
        if (( rc = clOsalMutexUnlock(ams_instance->response_mutex)) != CL_OK )
            goto error;
        clHandleCheckin(handle_database, amsHandle);
        rc = CL_ERR_INVALID_HANDLE;
        goto error;
    }

    ams_instance->finalize = 1;

    if ( (rc = clOsalMutexUnlock(ams_instance->response_mutex)) != CL_OK )
        goto error;

    /* 
     * Send the information to the server to finalize the 
     * corresponding handle 
     */

    req.handle = ams_instance->server_handle;
    if ( (rc = cl_ams_client_finalize(
                    &req,
                    &res))
            != CL_OK )
        goto error;

    /*
     * Delete the Mutex
     */

    if ( (rc = clOsalMutexDelete (ams_instance->response_mutex) ) != CL_OK )
        goto error;

    /* 
     * Destroy the handle 
     */

    if ( (rc = clHandleDestroy(
                    handle_database,
                    amsHandle))
            != CL_OK )
        goto error;

    /* 
     * Check-In the handle 
     */

    if ( (rc = clHandleCheckin(
                    handle_database,
                    amsHandle))
            != CL_OK )
        goto error;

    clHeapFree((void*)res);
    res = NULL;
    return CL_OK;

error:

    clHeapFree((void*)res);
    res = NULL;
    return CL_AMS_RC(rc);

}
ClRcT
clInstInitId()
{
    ClRcT           rc          = CL_OK;
    InstrumentedId *tmpPtr      = 0;

    CL_FUNC_ENTER();
    // check whether the id has been initialized
    if (myInstId != 0)
    {
        CL_FUNC_EXIT();
        return CL_OK;
    }

    if (sock == -1)
    {
        rc = initSocket();
        if (rc != CL_OK)
        {
            clOsalPrintf("failed to initialize instrumentation socket\n");
            return rc;
        }
    }


    // Nope, not initialized, so try to initialize it here.
    // First, try to lock a mutex so that only one thread
    // will be creating the id.
    // Then, try to get our component name, our local node name
    // and then allocate the id and set it from the component
    // and local node name.
    // Finally, unlock and delete the mutex.
    if (!id_mutex)
    {
        if ((rc = clOsalMutexCreateAndLock(&id_mutex)) != CL_OK)
        {
            clOsalPrintf("Failed [0x%x] to create and lock mutex\n", rc);
            CL_FUNC_EXIT();
            return rc;
        }
    }
    else
    {
        // Some other thread must have come through here and set
        // the mutex before us.  We're done.
        CL_FUNC_EXIT();
        return CL_OK;
    }

    if ((tmpPtr = clHeapCalloc(1, sizeof (InstrumentedId))) == 0)
    {
        clOsalPrintf("failed to allocate InstrumentedID\n");
        rc = CL_RC(0, CL_ERR_NO_MEMORY);
        goto breakdown;
    }
    memset(&tmpPtr->compName, 0, sizeof tmpPtr->compName);
    memset(&tmpPtr->nodeName, 0, sizeof tmpPtr->nodeName);
    rc = CL_OK;
    myInstId = tmpPtr;
    tmpPtr = 0;

breakdown:
    clOsalMutexUnlock(id_mutex);
    clOsalMutexDelete(id_mutex);
    id_mutex = 0;

    CL_FUNC_EXIT();
    return rc;
}
/* Handle destructor function. */
static void clDispatchHandleDestructor(void* cbArgs)
{
    ClRcT   rc = CL_OK;
    ClDispatchDbEntryT* thisDbEntry = NULL;
    ClUint32T   queueSize = 0;
    ClDispatchCbQueueDataT*   queueData = NULL;

    if (cbArgs == NULL)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Handle destructor is called with NULL pointer"));
        return;
    }

    thisDbEntry = (ClDispatchDbEntryT*)cbArgs;

    /* Lock the queue mutex */
    rc = clOsalMutexLock(thisDbEntry->dispatchMutex);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Unable to lock dispatch Mutex in Handle destructor callback"));
        return;
    }

    /*
     * Before deleting the queue you need to flush the queue.
     * Even though clQueueDelete will flush the node, we need
     * handle it explicitly so that we can invoke
     * ClDispatchQueueDestroyCallbackT registered during Register
     * which will deallocate the memory for the callback arguments.
     */
    rc = clQueueSizeGet(thisDbEntry->cbQueue, &queueSize);
    if (rc != CL_OK)
    {
        goto proceed_other_functions;
    }

    while (queueSize != 0)
    {
        rc = clQueueNodeDelete(thisDbEntry->cbQueue, (ClQueueDataT*)&queueData);
        if (rc != CL_OK)
        {
            goto proceed_other_functions;
        }
        CL_ASSERT(queueData != NULL);

        /* Invoke the queue destroy callback function */
        thisDbEntry->queueDestroyCallback(queueData->callbackType,
                                          queueData->callbackArgs);

        rc = clQueueSizeGet(thisDbEntry->cbQueue, &queueSize);
        if (rc != CL_OK)
        {
            goto proceed_other_functions;
        }
    }

proceed_other_functions:
    /* 
     * Delete the queue. This will also flush the queue. So all the
     * pending callbacks will be flushed.
     */
    rc = clQueueDelete(&thisDbEntry->cbQueue);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Unable to delete the queue. Rc = 0x%x",rc));
    }
        
    /* Delete the pipe */
    errno = 0;
    if ((close(thisDbEntry->readFd)) < 0)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Unable to close write fd of the pipe:%s",strerror(errno)));
    }

    errno = 0;
    if ((close(thisDbEntry->writeFd)) < 0)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Unable to close read fd of the pipe:%s",strerror(errno)));
    }


    /* Delete the mutex */
    rc = clOsalMutexUnlock(thisDbEntry->dispatchMutex);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Failed to unlock the dispatch mutex"));
    }
    rc = clOsalMutexDelete(thisDbEntry->dispatchMutex);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Failed to delete the dispatch mutex"));
    }


    return;
}
ClRcT clSnmpMutexFini(void)
{
    return clOsalMutexDelete(gOper.mtx);
}
Exemplo n.º 13
0
ClRcT clRmdObjClose(ClRmdObjHandleT p)
{
#if 0 // Don't know why safplus_amf process hang at clOsalMutexLock(pRmdObject->semaForRecvHashTable)
    ClRcT retCode = CL_OK;
    ClRmdObjT *pRmdObject = NULL;

    CL_FUNC_ENTER();

    pRmdObject = (ClRmdObjT *) p;

    if (NULL == pRmdObject)
    {
        RMD_DBG1((" RMD  Invalid rmd handle in objclose\n"));
        CL_FUNC_EXIT();
        return ((CL_RMD_RC(CL_ERR_INVALID_PARAMETER)));
    }

    /*
     * Avoid deleting the mutexes so its still valid in case any context is abusing the terminate
     * and initiating or not quitting the rmd sends.
     */

    retCode = clOsalMutexDelete(pRmdObject->semaForRecvHashTable);

    if (CL_OK != retCode)
    {
        RMD_DBG1((" RMD Recv mutex delete failed\n"));
    }
    
    retCode = clOsalMutexDelete(pRmdObject->semaForSendHashTable);

    if (CL_OK != retCode)
    {
        RMD_DBG1((" RMD Send mutex delete failed\n"));
    }

    clOsalMutexLock(pRmdObject->semaForRecvHashTable);
    retCode = clCntDelete(pRmdObject->rcvRecContainerHandle);
    if (CL_OK != retCode)
    {
        RMD_DBG1((" RMD rcv hash table destroy failed\n"));
    }
    pRmdObject->rcvRecContainerHandle = 0;
    clOsalMutexUnlock(pRmdObject->semaForRecvHashTable);

    clOsalMutexLock(pRmdObject->semaForSendHashTable);
    retCode = clCntDelete(pRmdObject->sndRecContainerHandle);
    if (CL_OK != retCode)
    {
        RMD_DBG1((" RMD snd hash table destroy failed\n"));

    }
    pRmdObject->sndRecContainerHandle = 0;
    clOsalMutexUnlock(pRmdObject->semaForSendHashTable);

    retCode = clHandleDatabaseDestroy(pRmdObject->responseCntxtDbHdl);
    if (CL_OK != retCode)
    {
        RMD_DBG1((" RMD snd hash table destroy failed\n"));

    }
    pRmdObject->responseCntxtDbHdl = 0;

    /*
     * Don't release the rmd context to respect abusers or initiators of rmd send outside
     * terminate callback contexts.
     */
    clHeapFree(pRmdObject);
#endif

    CL_FUNC_EXIT();
    return (CL_OK);
}
Exemplo n.º 14
0
ClRcT clRmdObjInit(ClRmdObjHandleT *p)
{
    ClRcT retCode = CL_OK;
    ClRmdObjT *pRmdObject = NULL;
    //unsigned long timeStamp = 0;
    struct timeval tm1;

#ifdef DEBUG
    static ClUint8T rmdAddedTodbgComp = CL_FALSE;

    if (CL_FALSE == rmdAddedTodbgComp)
    {
        retCode = dbgAddComponent(COMP_PREFIX, COMP_NAME, COMP_DEBUG_VAR_PTR);
        rmdAddedTodbgComp = CL_TRUE;
        if (CL_OK != retCode)
        {
            clLogError("OBG","INI","dbgAddComponent FAILED ");
            CL_FUNC_EXIT();
            return retCode;
        }
    }
#endif
    CL_FUNC_ENTER();

    if (NULL == p)
    {
        RMD_DBG1((" RMD Invalid Object handle passed\n"));
        CL_FUNC_EXIT();
        return ((CL_RMD_RC(CL_ERR_INVALID_PARAMETER)));
    }

    retCode = clOsalInitialize(NULL);

    retCode = clTimerInitialize(NULL);
    pRmdObject = (ClRmdObjT *) clHeapAllocate(sizeof(ClRmdObjT));

    if (NULL == pRmdObject)
    {
        RMD_DBG1((" RMD No Memory\n"));
        CL_FUNC_EXIT();
        return ((CL_RMD_RC(CL_ERR_NO_MEMORY)));
    }

    gettimeofday(&tm1, NULL);
    //timeStamp = tm1.tv_sec * 1000000 + tm1.tv_usec;
    pRmdObject->msgId = 1;
    retCode = clOsalMutexCreate(&pRmdObject->semaForSendHashTable);

    if (CL_OK != CL_GET_ERROR_CODE(retCode))
    {
        RMD_DBG1((" RMD send Mutex creation failed\n"));
        CL_FUNC_EXIT();
        return (retCode);
    }
    retCode = clOsalMutexCreate(&pRmdObject->semaForRecvHashTable);

    if (CL_OK != CL_GET_ERROR_CODE(retCode))
    {
        RMD_DBG1((" RMD  recv Mutex creation failed\n"));

        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForSendHashTable));
        CL_FUNC_EXIT();
        return (retCode);
    }

    retCode =
        clCntHashtblCreate(NUMBER_OF_RECV_BUCKETS, recvKeyCompare,
                           recvHashFunction, recvHashDeleteCallBack,
                           recvHashDeleteCallBack, CL_CNT_UNIQUE_KEY,
                           &pRmdObject->rcvRecContainerHandle);
    if (CL_OK != CL_GET_ERROR_CODE(retCode))
    {
        RMD_DBG1((" RMD  send Hash table creation failed\n"));

        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForRecvHashTable));
        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForSendHashTable));
        CL_FUNC_EXIT();
        return (retCode);
    }
    retCode =
        clCntHashtblCreate(NUMBER_OF_SEND_BUCKETS, sendKeyCompare,
                           sendHashFunction, sendHashDeleteCallBack,
                           sendHashDestroyCallBack, CL_CNT_UNIQUE_KEY,
                           &pRmdObject->sndRecContainerHandle);
    if (CL_OK != CL_GET_ERROR_CODE(retCode))
    {
        RMD_DBG1((" RMD  recv Hash table creation failed\n"));

        IGNORE_RETURN(clCntDelete(pRmdObject->rcvRecContainerHandle));
        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForRecvHashTable));
        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForSendHashTable));
        CL_FUNC_EXIT();
        return (retCode);
    }

    pRmdObject->responseCntxtDbHdl = 0;
    retCode = clHandleDatabaseCreate(NULL, &pRmdObject->responseCntxtDbHdl);
    if (retCode != CL_OK)
    {
        RMD_DBG1((" RMD  Sync Handle Database create failed\n"));
        IGNORE_RETURN(clCntDelete(pRmdObject->sndRecContainerHandle));
        IGNORE_RETURN(clCntDelete(pRmdObject->rcvRecContainerHandle));
        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForRecvHashTable));
        IGNORE_RETURN(clOsalMutexDelete(pRmdObject->semaForSendHashTable));

        CL_FUNC_EXIT();
        return retCode;
    }

    pRmdObject->numAtmostOnceEntry = 0;
    pRmdObject->lastAtmostOnceCleanupTime = clOsalStopWatchTimeGet();
    pRmdObject->rmdStats.nRmdCalls = 0;
    pRmdObject->rmdStats.nFailedCalls = 0;
    pRmdObject->rmdStats.nResendRequests = 0;
    pRmdObject->rmdStats.nRmdReplies = 0;
    pRmdObject->rmdStats.nBadReplies = 0;
    pRmdObject->rmdStats.nRmdRequests = 0;
    pRmdObject->rmdStats.nBadRequests = 0;
    pRmdObject->rmdStats.nCallTimeouts = 0;
    pRmdObject->rmdStats.nDupRequests = 0;
    pRmdObject->rmdStats.nAtmostOnceCalls = 0;
    pRmdObject->rmdStats.nResendReplies = 0;
    pRmdObject->rmdStats.nReplySend = 0;
    pRmdObject->rmdStats.nRmdCallOptimized = 0;
    *p = (ClRmdObjHandleT) pRmdObject;
    CL_FUNC_EXIT();
    return (CL_OK);
}
/**
 *  Creates a new Extended State machine Instance.
 *
 *  This API creates a new Extended State macine Instance of given
 *  state machine type.  The extended state machine shall include
 *  all the regular state machine instance functionalities, plus
 *  additional event queue, history, and lock capabilities.
 *                                                                        
 *  @param sm       State machine type 
 *  @param instance [out] newly created extended state machine instance
 *
 *  @returns 
 *    CL_OK on CL_OK <br/>
 *    CL_SM_RC(CL_ERR_NO_MEMORY) on memory allocation FAILURE <br/>
 *    CL_SM_RC(CL_ERR_NULL_POINTER) on invalid/null sm / instance <br/>
 *
 *  @see #clEsmInstanceDelete
 */
ClRcT
clEsmInstanceCreate(ClSmTemplatePtrT sm, 
                  ClExSmInstancePtrT* instance
                  )
{
  ClRcT ret = CL_OK;

  CL_FUNC_ENTER();
  CL_ASSERT(instance);  
  CL_ASSERT(sm);  

  clLogTrace(ESM_LOG_AREA,ESM_LOG_CTX_CREATE,"Create Extended State Machine Instance");

  if(sm && instance) 
    {
      /* allocate the instance space */
      *instance = (ClExSmInstancePtrT) mALLOC(sizeof(ClExSmInstanceT));
      if(*instance!=0) 
        {
          memset(*instance, 0, sizeof(ClExSmInstanceT));
          /* call sm create here */
          ret = clSmInstanceCreate(sm, &(*instance)->fsm);
          if(ret == CL_OK)
            {
              ret = clOsalMutexCreate(&(*instance)->lock);
              if (CL_OK != ret)
              {
                  clSmInstanceDelete((*instance)->fsm);
                  mFREE(*instance);
                  ret = SM_ERR_NO_SEMA;
              }
              else
              {
              /* create queue and init */
              ret = SMQ_CREATE((*instance)->q);
              if(ret == CL_OK)
                {
                  /* init log buffer */
                  ESM_LOG_INIT((*instance)->log, ESM_LOG_ENTRIES);
                }
              if(!(*instance)->log.buffer || ret != CL_OK)
                {
                  /* delete the instance */
                  ret = clSmInstanceDelete((*instance)->fsm);
                  /* delete the mutex */
                  clOsalMutexDelete((*instance)->lock);
                  /* check if q init succeeded */
                  if(ret == CL_OK)
                    {
                      /* delete the queue */
                      clQueueDelete(&((*instance)->q));
                    }
                  /* free the instance */
                  mFREE(*instance);
                  ret = CL_SM_RC(CL_ERR_NO_MEMORY);
                }
              }
            }
          
        } else 
          {
            ret = CL_SM_RC(CL_ERR_NO_MEMORY);
          }
    } else 
      {
        ret = CL_SM_RC(CL_ERR_NULL_POINTER);
      }
  
  CL_FUNC_EXIT();
  return ret;
}
/** 
 *  Delete Extended State machine Instance.
 *  
 *  API to delete a previously created State macine Instance. Also
 *  frees up the events that are in the Q.
 *                                                                        
 *  @param smThis Extended State machine Instance to be deleted
 *
 *  @returns 
 *    CL_OK on CL_OK <br/>
 *    CL_SM_RC(CL_ERR_NULL_POINTER) on invalid/null instance handle <br/>
 *
 *  @see #clEsmInstanceCreate
 *
 */
ClRcT 
clEsmInstanceDelete(ClExSmInstancePtrT smThis
                 )
{
  ClRcT ret = CL_OK;

  CL_FUNC_ENTER();
  CL_ASSERT(smThis);  

  clLogTrace(ESM_LOG_AREA,ESM_LOG_CTX_DELETE,"Delete Extended State Machine Instance");

  if(smThis) 
    {
      ClUint32T sz = 0;

      if(ESM_LOCK(smThis)!=CL_OK)
        {
          ret = SM_ERR_LOCKED;
          CL_FUNC_EXIT();
          return ret;
        }

      /* free the fsm first */
      ret = clSmInstanceDelete(smThis->fsm);

      SMQ_SIZE(smThis->q, sz);
      /* Check if the queue is empty, if not, dequeue and delete them */
      if(sz > 0) 
        {
          ClSmQueueItemPtrT item;
          ClRcT rc;

          rc = SMQ_DEQUEUE(smThis->q, item);
          while(rc==CL_OK && item)
            {
              mFREE(item);
              rc = SMQ_DEQUEUE(smThis->q, item);
            }
          clLogInfo(ESM_LOG_AREA,ESM_LOG_CTX_DELETE,"***Delete: Events are present in Q! Dropped to floor!!! ***");
        }

      /* delete the queue */
      clQueueDelete(&smThis->q);

      /* free the history buffer */
      mFREE(smThis->log.buffer);

      /* unlock it before, so we can delete the mutex */
      ESM_UNLOCK(smThis);

      /* delete the mutex */
      clOsalMutexDelete(smThis->lock);

      /* free the object */
      mFREE(smThis);
    } else 
      {
        ret = CL_SM_RC(CL_ERR_NULL_POINTER);
      }

  CL_FUNC_EXIT();
  return ret;
}