ClRcT
clCntNextNodeGet(ClCntHandleT     containerHandle,
                 ClCntNodeHandleT currentNodeHandle,
                 ClCntNodeHandleT *pNextNodeHandle)
{
  CclContainer_t  *pContainer = NULL;
  
  nullChkRet(pNextNodeHandle);
  *pNextNodeHandle = CL_HANDLE_INVALID_VALUE;

  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  
  if(pContainer->validContainer != CONTAINER_ID)
  {
      returnCntError(CL_ERR_INVALID_HANDLE, "Passed handle is invalid");
  }

  if (pContainer->fpFunctionContainerNextNodeGet == NULL) 
  {
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
    /* can't be right! DEBUG MESSAGE */
  }

  return (pContainer->fpFunctionContainerNextNodeGet (containerHandle,
  					              currentNodeHandle, pNextNodeHandle));
}
ClRcT
clCntPreviousNodeGet(ClCntHandleT containerHandle,
                     ClCntNodeHandleT currentNodeHandle,
                     ClCntNodeHandleT* pPreviousNodeHandle)
{
  CclContainer_t  *pContainer = NULL;
  
  nullChkRet(pPreviousNodeHandle);
  *pPreviousNodeHandle = CL_HANDLE_INVALID_VALUE;

  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }
  
  if (pContainer->fpFunctionContainerPreviousNodeGet == NULL) 
  {
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

  return (pContainer->fpFunctionContainerPreviousNodeGet (containerHandle,
							  currentNodeHandle, pPreviousNodeHandle));
}
ClRcT
clCntNodeUserDataGet(ClCntHandleT      containerHandle,
                     ClCntNodeHandleT  nodeHandle, 
                     ClCntDataHandleT  *pUserDataHandle)
{
    void             *pNode         = NULL;
    CclContainer_t   *pContainer    = NULL;
    ClRcT            errorCode      = CL_OK;
    ClCntDataHandleT tempDataHandle = CL_HANDLE_INVALID_VALUE;

    nullChkRet(pUserDataHandle);
    *pUserDataHandle = CL_HANDLE_INVALID_VALUE;

    pContainer = (CclContainer_t *) containerHandle;
    nullChkRet(pContainer);

    if(pContainer->validContainer != CONTAINER_ID)
    {
        returnCntError(CL_ERR_INVALID_HANDLE, 
                "Passed container handle is invalid");
    }
    pNode = (void*) nodeHandle;
    nullChkRet(pNode);

    errorCode = (pContainer->fpFunctionContainerUserDataGet(containerHandle, nodeHandle,
                &tempDataHandle));
    if( CL_OK != errorCode )
    {
        return errorCode;
    }
    *pUserDataHandle = tempDataHandle;
    return (CL_OK);
}
ClRcT
clCntNodeUserKeyGet(ClCntHandleT     containerHandle,
                    ClCntNodeHandleT nodeHandle, 
                    ClCntKeyHandleT* pUserKey)
{
    void            *pNode      = NULL;
    CclContainer_t  *pContainer = NULL;
    ClRcT           errorCode   = CL_OK;
    ClCntKeyHandleT tempKey     = CL_HANDLE_INVALID_VALUE;

    /* User key validations */
    nullChkRet(pUserKey);
    *pUserKey = CL_HANDLE_INVALID_VALUE;

    /*Container handle validations */
    pContainer = (CclContainer_t *) containerHandle;
    nullChkRet(pContainer);
    if(pContainer->validContainer != CONTAINER_ID)
    {
        returnCntError(CL_ERR_INVALID_HANDLE, "Passed handle is invalid");
    }
    /* Node Handle validations */
    pNode = (void*) nodeHandle;
    nullChkRet(pNode);

    errorCode = (pContainer->fpFunctionContainerUserKeyGet(containerHandle, 
                nodeHandle,
                &tempKey));
    if( CL_OK != errorCode )
    {
        return errorCode;
    }
    *pUserKey = tempKey;
    return CL_OK;
}
ClRcT
cosSysvSemCreate (ClUint8T* pName, ClUint32T count, ClOsalSemIdT* pSemId)
{
    ClInt32T retCode = CL_OK;
    CosSemCtl_t semArg = {0};
    ClUint32T len = 0;
    ClUint32T key = 0;
    ClInt32T semId = -1;

    nullChkRet(pSemId);
    nullChkRet(pName);

    CL_FUNC_ENTER();
    if ((count == 0) || count > CL_SEM_MAX_VALUE)
    {
        retCode = CL_OSAL_RC(CL_ERR_INVALID_PARAMETER);
        clDbgCodeError(retCode, ("Number of semaphores to create (count) [%d] must be between [1] and [%d]", count,  CL_SEM_MAX_VALUE));
        CL_FUNC_EXIT();
        return(retCode);
    }

    len = (ClUint32T)strlen ((ClCharT*)pName);

#if 0 /* Stone: why this limitation? */
    if(len > 20)
        if(len > 256)
        {
            CL_DEBUG_PRINT (CL_DEBUG_INFO,("Sanity check, semaphore name length is suspiciously long"));
            retCode = CL_OSAL_RC(CL_OSAL_ERR_NAME_TOO_LONG);
            CL_FUNC_EXIT();
            return(retCode);
        }
#endif

    if(len > 256)
    {
        CL_DEBUG_PRINT (CL_DEBUG_INFO,("Sanity check, semaphore name [%s] is suspiciously long",pName));
    }

    retCode = (ClInt32T)clCrc32bitCompute (pName, len, &key, NULL);
    CL_ASSERT(retCode == CL_OK); /* There is no possible error except for pName == NULL, which I've already checked, so don't check the retCode */


    sysErrnoChkRet(semId = semget ((key_t)key, (int)count, IPC_CREAT|0666));

    semArg.val = (int)count;

    /* Initialize all the semaphores to 0.  This should never fail, because I just created the semaphores */
    sysErrnoChkRet(semctl (semId, 0, SETVAL, semArg));

    *pSemId = (ClOsalSemIdT)semId;

    CL_FUNC_EXIT();
    return (CL_OK);
}
Пример #6
0
ClRcT
cosPosixSemCreate (ClUint8T* pName, ClUint32T count, ClOsalSemIdT* pSemId)
{
    ClInt32T rc = CL_OK;
    sem_t *pSem = NULL;
    ClInt32T semValue = 0;
    ClInt32T i = 0;
    
    nullChkRet(pSemId);
    nullChkRet(pName);

    CL_FUNC_ENTER();
    if ((count == 0) || count > CL_SEM_MAX_VALUE)
    {
        rc = CL_OSAL_RC(CL_ERR_INVALID_PARAMETER);
        clDbgCodeError(rc, ("Number of semaphores to create (count) [%d] must be between [1] and [%d]", count,  CL_SEM_MAX_VALUE));
        CL_FUNC_EXIT();
        return(rc);
    }

    pSem = sem_open((ClCharT*)pName, O_CREAT, 0777, count);
    if(pSem == SEM_FAILED)
    {
        rc = CL_OSAL_RC(CL_ERR_LIBRARY); 
        clDbgCodeError(rc, ("Failed at sem_open. system error code %d.\n", errno));
        return rc;
    }

    sem_getvalue(pSem, &semValue);

    if (semValue < (ClInt32T)count)
    {
        for (i = semValue; i < (ClInt32T)count; ++i)
        {
            sem_post(pSem);
        }
    }
    else
    {
        for (i = count; i < semValue; ++i)
        {
            sem_wait(pSem);
        }
    }

    *pSemId = *(ClOsalSemIdT*)&pSem;

    CL_FUNC_EXIT();
    return (rc);
}
Пример #7
0
ClRcT 
__cosPosixMutexLock (ClOsalMutexIdT mutexId, ClBoolT verbose)
{
    ClRcT rc = CL_OK;
    ClOsalMutexT *pMutex = (ClOsalMutexT*)mutexId;
    ClInt32T err=0;

    if(verbose)
    {
        nullChkRet(pMutex);
    }
    else if(!pMutex)
    {
        return CL_OSAL_RC(CL_ERR_NULL_POINTER);
    }

    CL_FUNC_ENTER();   
retry:
    err = sem_wait(&pMutex->shared_lock.sem.posSem);
    if(err < 0 )
    {
        if(errno == EINTR)
            goto retry;
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
        if(verbose)
        {
            clDbgCodeError(rc,("sem_wait returned [%s]\n",strerror(errno)));
        }
    }

    CL_FUNC_EXIT();
    return (rc);
}
Пример #8
0
ClRcT cosPosixProcessSharedSemInit(ClOsalMutexT *pMutex, ClUint8T *pKey, ClUint32T keyLen, ClInt32T value)
{
    ClRcT rc = CL_OK;
    int err;

    nullChkRet(pKey);
    if(keyLen == 0)
    {
        clDbgCodeError(CL_ERR_INVALID_PARAMETER,("Invalid keylen [%d]\n",keyLen));
        return CL_OSAL_RC(CL_ERR_INVALID_PARAMETER);
    }

    pthread_mutex_lock(&gClSemAccessLock);
    err = sem_init(&pMutex->shared_lock.sem.posSem,1, value);
    if(err < 0)
    {
        pthread_mutex_unlock(&gClSemAccessLock);
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
        goto out;
    }
    pthread_mutex_unlock(&gClSemAccessLock);
    
    pMutex->shared_lock.sem.numSems = 1;
    rc = CL_OK;

out:
    return rc;
}
ClRcT
clCntDelete(ClCntHandleT containerHandle)
{
  CclContainer_t  *pContainer = NULL;
  
  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  
  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }
  if (pContainer->fpFunctionContainerDestroy == NULL) 
  {
    /* can't be right! DEBUG MESSAGE */
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

#if HA_ENABLED
  /* HA-aware container Modifications Start: Hari*/
  /* The container is getting destroyed. So de-register it with CPS*/
  if (pContainer->haEnabled)
  {
       errorCode = clCPContainerDeReg(pContainer->cpsHdl,
                          pContainer->domainId,
                          pContainer->containerId);
  }
#endif
  /* HA-aware container Modifications End: Hari*/
  return (pContainer->fpFunctionContainerDestroy (containerHandle));
}
ClRcT
clHandleWithAddressCreate (
                ClHandleDatabaseHandleT databaseHandle,
                ClInt32T instance_size,
                ClIocPhysicalAddressT compAddr,
                ClHandleT *handle_out)
{
  ClWordT        idx       = 0;
  void           *instance    = NULL;
  ClRcT          rc           = CL_OK;
  ClHdlDatabaseT *hdbp        = (ClHdlDatabaseT*) databaseHandle;

  nullChkRet(handle_out);
  hdlDbValidityChk(hdbp);

  instance = clHeapCalloc (1, instance_size);
  if (instance == NULL)
    {
      rc = CL_HANDLE_RC(CL_ERR_NO_MEMORY);
      return rc;
    }

  rc =  clHandleAdd (databaseHandle, instance, &compAddr, handle_out);
  idx = CL_HDL_IDX(*handle_out)-1;

  hdbp->handles[idx].flags = HANDLE_ALLOC_FLAG;

  if (rc != CL_OK)
  {
      clHeapFree(instance);
  }
  return rc;
}
Пример #11
0
ClRcT
cosPosixSemIdGet(ClUint8T* pName, ClOsalSemIdT* pSemId)
{
    ClUint32T count = 0;
    sem_t *pSem;

    nullChkRet(pSemId);
    nullChkRet(pName);

    CL_FUNC_ENTER();

    pSem = sem_open((ClCharT*)pName, O_CREAT, 0666, count);

    *pSemId = *(ClOsalSemIdT*)&pSem;

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
cosSysvSemIdGet(ClUint8T* pName, ClOsalSemIdT* pSemId)
{
    ClUint32T key = 0;
    ClInt32T semId = -1;
    ClUint32T len = 0;
    ClUint32T count = 0;
    ClUint32T retCode = CL_OK;
    int       err;

    nullChkRet(pSemId);
    nullChkRet(pName);

    CL_FUNC_ENTER();

    len = (ClUint32T)strlen ((ClCharT*)pName);

    if(len > 256)
    {
        CL_DEBUG_PRINT (CL_DEBUG_INFO,("Sanity check, semaphore name [%s] is suspiciously long",pName));
    }

    retCode = clCrc32bitCompute (pName, len, &key, NULL);
    CL_ASSERT(retCode == CL_OK); /* There is no possible error except for pName == NULL, which I've already checked, so don't check the retCode */

    semId = semget ((key_t)key, (int)count, 0660);
    err   = errno;

    if(semId == -1)
    {
        if (err == ENOENT)
        {
            CL_DEBUG_PRINT (CL_DEBUG_INFO,("Semaphore [%s], id [%u] accessed but not created.  Creating it now",pName,key));
            semId = semget ((key_t)key, (int)count, IPC_CREAT|0666);
        }
    }

    sysErrnoChkRet(semId);

    *pSemId = (ClOsalSemIdT)semId;

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT cosSysvProcessSharedSemInit(ClOsalMutexT *pMutex, ClUint8T *pKey, ClUint32T keyLen, ClInt32T value)
{
    ClInt32T semId = 0;
    ClUint32T semKey = 0;
    ClRcT rc = CL_OK;
    ClUint32T flags = 0666;
    ClInt32T err = 0;

    nullChkRet(pKey);
    if(keyLen == 0)
    {
        clDbgCodeError(CL_ERR_INVALID_PARAMETER,("Invalid keylen [%d]\n",keyLen));
        return CL_OSAL_RC(CL_ERR_INVALID_PARAMETER);
    }

    rc = clCrc32bitCompute(pKey, keyLen, &semKey, NULL);
    CL_ASSERT(rc == CL_OK && semKey );

    pthread_mutex_lock(&gClSemAccessLock);
retry:
    semId = semget(semKey, 1, flags);
    if(semId < 0 )
    {
        if(errno == EINTR)
            goto retry;
        if(errno == ENOENT)
        {
            flags |= IPC_CREAT;
            goto retry;
        }
        pthread_mutex_unlock(&gClSemAccessLock);
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
        goto out;
    }
    pthread_mutex_unlock(&gClSemAccessLock);
    if( (flags & IPC_CREAT) )
    {
        CosSemCtl_t arg = {0};
        arg.val = value;
retry1:
        err = semctl(semId,0,SETVAL,arg);
        if(err < 0 )
        {
            if(errno == EINTR)
                goto retry1;
            rc = CL_OSAL_RC(CL_ERR_LIBRARY);
            goto out;
        }
    }
    pMutex->shared_lock.sem.semId = semId;
    pMutex->shared_lock.sem.numSems = 1;
    rc = CL_OK;

out:
    return rc;
}
ClRcT
clCntNodeFind(ClCntHandleT      containerHandle,
              ClCntKeyHandleT   userKey,
              ClCntNodeHandleT  *pNodeHandle)
{
  CclContainer_t        *pContainer    = NULL;
  ClCntNodeHandleT      tempNodeHandle = CL_HANDLE_INVALID_VALUE;
  ClRcT                 errorCode      = CL_OK;
  BaseLinkedListNode_t  *pBLlNode      = NULL;

  nullChkRet(pNodeHandle);
  *pNodeHandle = CL_HANDLE_INVALID_VALUE;

  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);

  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }

  if (pContainer->fpFunctionContainerNodeFind == NULL) 
  {
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

  errorCode = (pContainer->fpFunctionContainerNodeFind(containerHandle,
						   userKey, &tempNodeHandle));
  
  if(errorCode != CL_OK )
  {
    return (errorCode);
  }
  else
  {
    pBLlNode     = (BaseLinkedListNode_t *) tempNodeHandle;
    *pNodeHandle = tempNodeHandle;
    return (CL_OK);
  }
  
}
ClRcT
clCntDataForKeyGet(ClCntHandleT      containerHandle,
                   ClCntKeyHandleT   userKey,
                   ClCntDataHandleT  *pUserData)
{
  ClCntNodeHandleT  containerNode = CL_HANDLE_INVALID_VALUE;
  CclContainer_t    *pContainer   = NULL;
  ClRcT             errorCode     = CL_OK;

  nullChkRet(pUserData);
  *pUserData = CL_HANDLE_INVALID_VALUE;

  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }

  if(pContainer->isUniqueFlag == 0) 
  {
      returnCntError(CL_ERR_NOT_IMPLEMENTED,
              "This feature is not applicaple for non unique container");
  }

  errorCode = clCntNodeFind(containerHandle, userKey, &containerNode);
  if(errorCode != CL_OK)
  {
      returnCntError(CL_ERR_NOT_EXIST, "Key doesn't exist");
  }

  errorCode = clCntNodeUserDataGet(containerHandle, containerNode, pUserData);
  if(errorCode != CL_OK)
  {
    return(errorCode);
  }

  return(CL_OK);
}
Пример #16
0
ClRcT
cosPosixSemValueGet(ClOsalSemIdT semId, ClUint32T* pSemValue)
{
    sem_t *pSem = *(sem_t**)&semId;
   
    nullChkRet(pSemValue);

    CL_FUNC_ENTER();
    
    sysErrnoChkRet(sem_getvalue(pSem, (ClInt32T*)pSemValue));

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
clCntNodeDelete(ClCntHandleT      containerHandle,
                ClCntNodeHandleT  nodeHandle)
{
  CclContainer_t  *pContainer = NULL;
  
  pContainer = (CclContainer_t *) containerHandle;

  nullChkRet(pContainer);
  
  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }

  if (pContainer->fpFunctionContainerNodeDelete == NULL) 
  {
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

#if HA_ENABLED
  /* HA-aware container Modifications Start: Hari*/
  /* This element is going to be deleted. Let the redundancy peer know it*/
  if(pContainer->haEnabled)
  {
      ClCntKeyHandleT  userKey;

      if (CL_OK != clCntNodeUserKeyGet(containerHandle,
                                  nodeHandle,
                                  &userKey))
      {
         errorCode =CL_CNT_RC(CL_ERR_INVALID_STATE);
         CL_DEBUG_PRINT (CL_DEBUG_TRACE,("\nInvalid state"));
         return (errorCode);
      }

      /* Update the standby component */
      errorCode = clCPContainerElemSynch(pContainer->cpsHdl,
                             pContainer->domainId,
                             pContainer->containerId,
                             userKey,
                             0x0000);  /* ELEMENTDEL =  0x00000*/
  }
#endif 
  /* HA-aware container Modifications End: Hari*/

  return (pContainer->fpFunctionContainerNodeDelete (containerHandle,
						     nodeHandle));
}
ClRcT
clCntKeySizeGet(ClCntHandleT    containerHandle,
                ClCntKeyHandleT userKey,
                ClUint32T       *pSize)
{
  CclContainer_t  *pContainer = NULL;

  nullChkRet(pSize);
  *pSize = 0;
  
  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);

  if(pContainer->validContainer != CONTAINER_ID)
  {
    returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
  }
  if (pContainer->fpFunctionContainerKeySizeGet == NULL) 
  {
    returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

  return (pContainer->fpFunctionContainerKeySizeGet (containerHandle, userKey, pSize));
}
ClRcT
cosSysvSemValueGet(ClOsalSemIdT semId, ClUint32T* pSemValue)
{
    ClInt32T count = -1;

    nullChkRet(pSemValue);

    CL_FUNC_ENTER();

    sysErrnoChkRet(count = semctl ((int)semId, 0, GETVAL, 0));

    *pSemValue = (ClUint32T)count;

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
clCntNodeAdd(ClCntHandleT     containerHandle,
             ClCntKeyHandleT  userKey,
             ClCntDataHandleT userData,
             ClRuleExprT      *pExp)
{
  CclContainer_t  *pContainer = NULL;
  ClRcT           errorCode   = CL_OK;
  
  pContainer = (CclContainer_t *) containerHandle;

  nullChkRet(pContainer);
  if( CONTAINER_ID != pContainer->validContainer)
  {
      returnCntError(CL_ERR_INVALID_HANDLE, 
                     "Passed container handle is invalid");
  }

  if( NULL == pContainer->fpFunctionContainerNodeAdd )
  {
      returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }
 
  errorCode = pContainer->fpFunctionContainerNodeAdd (containerHandle,
                                                  userKey,
                                                  userData,
                                                  pExp);
  /* HA-aware Container Modifications Start: Hari*/

#if HA_ENABLED
  /* Addition is succesfull and the container is ha enabled*/
  if((pContainer->haEnabled)&& (errorCode == CL_OK))
  {
    /* Update the standby component */
    errorCode = clCPContainerElemSynch(pContainer->cpsHdl,
                           pContainer->domainId,
                           pContainer->containerId,
                           userKey,
                           0xFFFF);  /* ELEMENTADD =  0xFFFFF */
  }
#endif /*HA_ENABLED*/
  /* HA-aware Container Modifications End: Hari*/
  return (errorCode);

}
ClRcT
clCntAllNodesDelete(ClCntHandleT containerHandle)
{
  CclContainer_t   *pContainer       = NULL;
  ClCntNodeHandleT containerNode     = CL_HANDLE_INVALID_VALUE;
  ClCntNodeHandleT nextContainerNode = CL_HANDLE_INVALID_VALUE;
  ClRcT            errorCode         = CL_OK;
  
  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  if(pContainer->validContainer != CONTAINER_ID)
  {
      returnCntError(CL_ERR_INVALID_HANDLE, 
              "Passed container handle is invalid");
  }

  errorCode = clCntFirstNodeGet(containerHandle, &containerNode);
  if(CL_OK != errorCode) 
  {
      /*
       * Check if container is empty.
       */
      if(CL_GET_ERROR_CODE(errorCode) == CL_ERR_NOT_EXIST)
          return CL_OK;
      return errorCode;
  }

  while(containerNode)
  {
      if(clCntNextNodeGet(containerHandle, containerNode, &nextContainerNode) != CL_OK)
      {
          nextContainerNode = 0;
      }
  
      errorCode = clCntNodeDelete(containerHandle, containerNode);
	  if(errorCode != CL_OK)
	  {
		  return(errorCode);
	  }
      containerNode = nextContainerNode;
  }

  return (CL_OK);
}
Пример #22
0
ClRcT
cosPosixMutexValueGet(ClOsalMutexIdT mutexId, ClInt32T *pValue)
{
    ClOsalMutexT *pMutex = (ClOsalMutexT*) mutexId;
    ClRcT rc = CL_OK;
    ClInt32T val = 0;
    
    nullChkRet(pMutex);
    CL_FUNC_ENTER();

    val = sem_getvalue(&pMutex->shared_lock.sem.posSem, pValue);
    if(val < 0 )
    {
        clLogError(CL_LOG_AREA_UNSPECIFIED, CL_LOG_CONTEXT_UNSPECIFIED,"Mutex value get- sem_getvalue() failed with [%s]\n", strerror(errno));
        rc = CL_OSAL_RC(CL_ERR_LIBRARY);
    }

    CL_FUNC_EXIT();

    return rc;
}
/*
 * Creates a new handle database.  User is responsible to cleanup and free
 * database.
 */
ClRcT
clHandleDatabaseCreate(
    void                    (*destructor)(void*),
    ClHandleDatabaseHandleT  *databaseHandle)
{
    ClHdlDatabaseT *hdbp = NULL;
    
    nullChkRet(databaseHandle);

    hdbp = (ClHdlDatabaseT*) clHeapCalloc(1, sizeof(ClHdlDatabaseT));
    if (NULL == hdbp)
    {
        clLogError(CL_HDL_AREA, CL_HDL_CTX_DBCREATE, 
                   "Memory allocation failed");
        return CL_HANDLE_RC(CL_ERR_NO_MEMORY);
    }
    
    (void)pthread_mutex_init(&hdbp->mutex, NULL); /* This always returns 0 */
    if (destructor != NULL)
    {
        hdbp->handle_instance_destructor = destructor;
    }
    hdbp->pValidDb = (void *) CL_HDL_VALID_DB;

    hdbp->id = handleDbId++;
    
    /*
     * Database handle is obtained from memory address here. This is OK,
     * since (1) handle type is larger or same size as address, (2) the
     * use of handle is limited to one process.
     */
    *databaseHandle = hdbp;
#if 0
    clLogTrace(CL_HDL_AREA, CL_HDL_CTX_DBCREATE, 
             "Database [%p] has been created", (ClPtrT) hdbp);
#endif
    clDbgResourceNotify(clDbgHandleGroupResource, clDbgAllocate, 0, hdbp, ("Handle database %p allocated", (ClPtrT) hdbp));
    
    return CL_OK;
}
ClRcT
clCntAllNodesForKeyDelete(ClCntHandleT    containerHandle,
                          ClCntKeyHandleT userKey)
{
  CclContainer_t  *pContainer = NULL;
  
  pContainer = (CclContainer_t *) containerHandle;
  nullChkRet(pContainer);
  
  if(pContainer->validContainer != CONTAINER_ID)
  {
      returnCntError(CL_ERR_INVALID_HANDLE, 
              "Passed container handle is invalid");
  }

  if (pContainer->fpFunctionContainerKeyDelete == NULL) 
  {
      returnCntError(CL_ERR_INVALID_STATE, "Container got corrupted");
  }

#if HA_ENABLED
  /* HA-aware container Modifications Start: Hari*/
  /* This element is going to be deleted. Let the redundancy peer know it*/
  if(pContainer->haEnabled)
  {
      /* Update the standby component */
      errorCode = clCPContainerElemSynch(pContainer->cpsHdl,
                             pContainer->domainId,
                             pContainer->containerId,
                             userKey,
                             0x0000);  /* ELEMENTDEL =  0x00000*/
  }
  /* HA-aware container Modifications End: Hari*/
#endif /*HA_ENABLED*/

  return (pContainer->fpFunctionContainerKeyDelete (containerHandle,
					       userKey));
}
ClRcT clCntNodeAddAndNodeGet(ClCntHandleT containerHandle,
           ClCntKeyHandleT userKey,
           ClCntDataHandleT userData,
           ClRuleExprT* pExp, ClCntNodeHandleT* pNodeHandle)
{
    ClRcT rc = CL_OK;

    nullChkRet(pNodeHandle);

    rc = clCntNodeAdd(containerHandle, userKey, userData, pExp);
    if(CL_OK != rc)
    {
        return rc;
    }
    rc = clCntNodeFind (containerHandle, userKey, pNodeHandle);
    CL_ASSERT(rc == CL_OK); /* I just added it, so I should be able to find it! */
    
    if(CL_OK != rc)
    {
        return rc;
    }
        
    return CL_OK;
}
ClRcT
clHandleAdd (ClHandleDatabaseHandleT databaseHandle,  void* instance, ClIocPhysicalAddressT *compAddr, ClHandleT* handle_out)
{
  ClHandleT      handle       = 0;
  ClHdlEntryT    *new_handles = NULL;
  ClBoolT        found        = CL_FALSE;
  ClRcT          rc           = CL_OK;
  ClHdlDatabaseT *hdbp        = (ClHdlDatabaseT*) databaseHandle;

  nullChkRet(handle_out);
  hdlDbValidityChk(hdbp);

  rc = pthread_mutex_lock (&hdbp->mutex);
  if (rc != 0)
    {
      return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR);
    }

  for (handle = 0; handle < hdbp->n_handles; handle++) 
    {
      if (hdbp->handles[handle].state == HANDLE_STATE_EMPTY) 
        {
          found = 1;
          break;
        }
    }

  if (found == 0) 
    {
      new_handles = (ClHdlEntryT *) realloc ( hdbp->handles, 
                                    sizeof (ClHdlEntryT) *
                              (hdbp->n_handles + CL_HDL_NUM_HDLS_BUNCH));
      if (new_handles == NULL)
        {
          rc = pthread_mutex_unlock (&hdbp->mutex);
          if (rc != 0)
            {
              return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be very bad */
            }
          return CL_HANDLE_RC(CL_ERR_NO_MEMORY);
        }
      memset(&new_handles[hdbp->n_handles], '\0', 
             sizeof(ClHdlEntryT) * CL_HDL_NUM_HDLS_BUNCH);
      hdbp->n_handles += CL_HDL_NUM_HDLS_BUNCH;
      hdbp->handles    = new_handles;
    }

  hdbp->handles[handle].state     = HANDLE_STATE_USED;
  hdbp->handles[handle].instance  = instance;
  hdbp->handles[handle].ref_count = 1;
  hdbp->handles[handle].flags     = 0;
    
  hdbp->n_handles_used++;

  /*
   * Adding 1 to handle to ensure the non-zero handle interface.
   */
  if (compAddr == NULL)
      *handle_out = CL_HDL_MAKE_ADDR(ASP_NODEADDR,gEOIocPort,hdbp->id, handle + 1);
  else
      *handle_out = CL_HDL_MAKE_ADDR(compAddr->nodeAddress,compAddr->portId, hdbp->id, handle + 1);

  hdbp->handles[handle].handle     = *handle_out;

  // Log uses handle so handle cant' use log
  // clDbgResourceNotify(clDbgHandleResource, clDbgAllocate, hdbp, handle+1, ("Handle [%p:%#llX] allocated", (ClPtrT)hdbp, handle+1));
  
  rc = pthread_mutex_unlock (&hdbp->mutex);
  if (rc != 0)
    {
      return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be devastating */
    }
#if 0
  clLogTrace(CL_HDL_AREA, CL_HDL_CTX_CREATE, 
             "Handle [%p:%#llX] has been created",
             (ClPtrT) hdbp, (handle + 1));
#endif
  return rc;
}
static ClRcT
__clCntWalk(ClCntHandleT        containerHandle,
            ClCntWalkCallbackT  fpUserWalkCallback,
            ClCntArgHandleT     userDataArg,
            ClInt32T            dataLength,
            ClBoolT failSafe)
{
    CclContainer_t       *pContainer   = NULL;
    ClCntNodeHandleT     containerNode = CL_HANDLE_INVALID_VALUE;
    ClCntNodeHandleT     nextContainerNode = CL_HANDLE_INVALID_VALUE;
    ClCntDataHandleT     userData      = CL_HANDLE_INVALID_VALUE;
    ClCntKeyHandleT      userKey       = CL_HANDLE_INVALID_VALUE;
    BaseLinkedListNode_t *pTemp        = NULL;
    ClRcT                errorCode     = CL_OK;
 
    nullChkRet(fpUserWalkCallback);

    pContainer = (CclContainer_t *) containerHandle;
    nullChkRet(pContainer);

    if(pContainer->validContainer != CONTAINER_ID)
    {
        returnCntError(CL_ERR_INVALID_HANDLE, "Passed container handle is invalid");
    }
  
    errorCode = clCntFirstNodeGet (containerHandle, &containerNode);
    if(errorCode != CL_OK)
    { 
        if((CL_GET_ERROR_CODE(errorCode)) == CL_ERR_NOT_EXIST) 
        {
            /* If the container is empty it means we dont have to walk anymore.
             * Hence return OK.
             */
            return (CL_OK);
        }
        return(errorCode);	  
    }

    while (containerNode)
    {
        pTemp = (BaseLinkedListNode_t*) containerNode;
        if(clCntNextNodeGet(containerHandle, containerNode, &nextContainerNode) != CL_OK)
        {
            nextContainerNode = 0;
        }
        if ((pTemp->pRbeExpression == NULL) || (clRuleExprEvaluate(pTemp->pRbeExpression, (ClUint32T*) userDataArg, dataLength)))
        {
            errorCode = clCntNodeUserKeyGet (containerHandle, containerNode, &userKey);
            errorCode = clCntNodeUserDataGet (containerHandle, containerNode, &userData);
  
            errorCode = fpUserWalkCallback(userKey, userData, userDataArg,dataLength);
            if(!failSafe && (CL_OK != errorCode))
            {
                return (errorCode);
            }
        }
        containerNode = nextContainerNode;
    }

    return(CL_OK);
}
ClRcT
clHandleCheckout(
                 ClHandleDatabaseHandleT databaseHandle,
                 ClHandleT               handleArg,
                 void                    **instance)
{ 
	ClRcT           rc    = CL_OK;
    ClHdlDatabaseT  *hdbp = (ClHdlDatabaseT*)databaseHandle;
    ClHdlStateT     state = HANDLE_STATE_EMPTY;
    ClRcT           ec    = CL_OK;
    ClHandleT       handle;
    hdlDbValidityChk(hdbp);
    /* sometimes people want to create the same handle across multiple nodes hdlValidityChk(handle,hdbp); */
    handle = CL_HDL_IDX(handleArg); /* once we've verified it, we only care about the index */
    nullChkRet(instance);
    /*
     * Decrementing handle to ensure the non-zero handle interface.
     */
    if (CL_HANDLE_INVALID_VALUE == handle--)
    {
        clDbgCodeError(CL_HANDLE_RC(CL_ERR_INVALID_HANDLE), ("Passed Invalid Handle [0x0]"));
        return CL_HANDLE_RC(CL_ERR_INVALID_HANDLE); /* 0 no longer allowed */
    }

    ec = pthread_mutex_lock (&hdbp->mutex);
    if (ec != 0)
    {
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR);
    }

    if (handle >= (ClHandleT)hdbp->n_handles)
    {
        rc = CL_HANDLE_RC(CL_ERR_INVALID_HANDLE);
        pthread_mutex_unlock(&hdbp->mutex);
        clDbgCodeError(rc, ("Passed Invalid Handle [%p:%#llx]", (ClPtrT) hdbp, handleArg));
        return rc;
    }
    
    if ( ( state = hdbp->handles[handle].state ) != HANDLE_STATE_USED)
    {
        pthread_mutex_unlock(&hdbp->mutex);
        if (state == HANDLE_STATE_EMPTY)
        {
            /* In some of our ASP components the assumption made,
             * like checkout handle returns CL_ERR_INVALID_HANDLE 
             * to verify the handle does exist or not.
             * so removing the debug pause
             */
#if 0
            clDbgCodeError(rc, ("Handle [%p:%#llX] is not allocated", (ClPtrT) hdbp, (handle+1)));
#endif
        }
        else if (state == HANDLE_STATE_PENDINGREMOVAL)
        {
            clDbgCodeError(rc, ("Handle [%p:%#llX] is being removed", (ClPtrT) hdbp, handleArg));
        }
        else
        {
            clDbgCodeError(rc, ("Handle [%p:%#llX] invalid state %d", (ClPtrT) hdbp, handleArg, state));
        }

        rc = CL_HANDLE_RC(CL_ERR_INVALID_HANDLE);
        clDbgCodeError(rc, ("Handle [%p:%#llX] is invalid", (ClPtrT) hdbp, handleArg));
        return rc;
    }

    *instance = hdbp->handles[handle].instance;

    hdbp->handles[handle].ref_count += 1;

    ec = pthread_mutex_unlock (&hdbp->mutex);
    if (ec != 0)
    {
        clDbgCodeError(CL_HANDLE_RC(CL_ERR_MUTEX_ERROR), ("Mutex unlock failed errno %d", errno));
        return CL_HANDLE_RC(CL_ERR_MUTEX_ERROR); /* This can be devastating */
    }
#if 0
    clLogTrace(CL_HDL_AREA, CL_HDL_CTX_CHECKOUT, 
               "Checked out handle [%p:%#llX]", (ClPtrT) hdbp, handleArg);
#endif
    return rc;
}