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);
}
ClRcT
cosSysvSemLock (ClOsalSemIdT semId)
{
    int retCode = 0;

    CL_FUNC_ENTER();
    struct sembuf semLock[] = {{0, -1, SEM_UNDO}};

    /* IPC_NOWAIT is not specified so that we can wait until the resource
     * is available.
     */

    do
    {
        retCode = semop ((int)semId, semLock, 1);

        /* If the semop system calls fails with errno=4, then it it because of
         * the timers interrupt. Call semop again and again.
         */
    }    while((-1 == retCode) && (EINTR == errno));

    sysErrnoChkRet(retCode);

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
cosSysvSemDelete(ClOsalSemIdT semId)
{
    CL_FUNC_ENTER();
    sysErrnoChkRet(semctl ((int)semId, 0, IPC_RMID, 0));
    CL_FUNC_EXIT();

    return (CL_OK);
}
Example #4
0
ClRcT
cosPosixSemDelete(ClOsalSemIdT semId)
{ 
    sem_t *pSem = *(sem_t**)&semId;

    CL_FUNC_ENTER();
    sysErrnoChkRet(sem_close(pSem));
    CL_FUNC_EXIT();

    return (CL_OK);
}
Example #5
0
ClRcT
cosPosixSemUnlock (ClOsalSemIdT semId)
{
    sem_t *pSem = *(sem_t**)&semId;
  
    CL_FUNC_ENTER();
    sysErrnoChkRet(sem_post(pSem));
    CL_FUNC_EXIT();

    return (CL_OK);
}
ClRcT
cosSysvSemUnlock (ClOsalSemIdT semId)
{
    struct sembuf semUnlock[] = {{0, 1, SEM_UNDO}};

    CL_FUNC_ENTER();
    sysErrnoChkRet(semop ((int)semId, semUnlock, 1));
    CL_FUNC_EXIT();

    return (CL_OK);
}
Example #7
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
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);
}
Example #9
0
ClRcT
cosPosixSemLock (ClOsalSemIdT semId)
{
    int retCode = 0;
    sem_t *pSem = *(sem_t**)&semId;
   
    CL_FUNC_ENTER();

retry:
    retCode = sem_wait(pSem);
    if(retCode == -1 && EINTR == errno)
        goto retry;

    sysErrnoChkRet(retCode);

    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);
}