Blame Esm/ib/src/cs/cs_sema.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT5 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015-2017, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
      documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
 * ** END_ICS_COPYRIGHT5   ****************************************/
Packit 857059
Packit 857059
/***********************************************************************
Packit 857059
* 
Packit 857059
* FILE NAME
Packit 857059
*      cs_sema.c
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*      This file contains the implementation of the Counting Semaphores
Packit 857059
*      services.
Packit 857059
*
Packit 857059
* DATA STRUCTURES
Packit 857059
*
Packit 857059
* FUNCTIONS
Packit 857059
*      cs_sema_create
Packit 857059
*      cs_sema_delete
Packit 857059
*      cs_vsema
Packit 857059
*      cs_psema
Packit 857059
*
Packit 857059
* DEPENDENCIES
Packit 857059
*      cs_g.h
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
* NAME      DATE        REMARKS
Packit 857059
* MGR       03/14/02    Initial creation of file.
Packit 857059
* MGR       03/26/02    Corrected lint errors.
Packit 857059
* MGR       04/18/02    PR1728.  vs_psema should return VSTATUS_AGAIN
Packit 857059
*                       error code, if thread has been killed.
Packit 857059
* MGR       04/23/02    PR1742.  Added fix for blocking of cs_psema;
Packit 857059
*                       cs_vsema should release lock if count goes to 1.
Packit 857059
*
Packit 857059
***********************************************************************/
Packit 857059
#include "ib_types.h"
Packit 857059
#include "ib_status.h"
Packit 857059
#include "cs_g.h"
Packit 857059
#include "vs_g.h"
Packit 857059
#include "cs_log.h"
Packit 857059
#define function __FUNCTION__
Packit 857059
#ifdef __LINUX__
Packit 857059
#include <semaphore.h>
Packit 857059
#elif defined(__VXWORKS__) 
Packit 857059
#include <drv/timer/timerDev.h>
Packit 857059
#endif
Packit 857059
#ifdef LOCAL_MOD_ID
Packit 857059
#undef LOCAL_MOD_ID
Packit 857059
#endif
Packit 857059
#define LOCAL_MOD_ID VIEO_CS_MOD_ID
Packit 857059
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_sema_create
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    Initialize a counting semaphore object for use.
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object to be filled in by
Packit 857059
*               the routine.
Packit 857059
*    count    - The initial value for the semaphore.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    Status_t - On success VSTATUS_OK is returned, otherwise
Packit 857059
*    the cause of the error.
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   MGR     03/14/02    Initial creation of function.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t
Packit 857059
cs_sema_create (Sema_t *handle,
Packit 857059
                    uint32_t count)
Packit 857059
{
Packit 857059
  Status_t          rc;
Packit 857059
Packit 857059
  IB_ENTER (function,
Packit 857059
            (unint)handle,
Packit 857059
            (uint32_t)count,
Packit 857059
            (uint32_t)0U,
Packit 857059
	    (uint32_t)0U);
Packit 857059
Packit 857059
  /*
Packit 857059
  ** Validate handle
Packit 857059
  */
Packit 857059
  if (handle == (Sema_t *) 0)
Packit 857059
  {
Packit 857059
    IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    IB_EXIT (function, VSTATUS_ILLPARM);
Packit 857059
    return VSTATUS_ILLPARM;
Packit 857059
  }
Packit 857059
Packit 857059
#ifdef __VXWORKS__
Packit 857059
  handle->magic = SEMAPHORE_MAGIC;
Packit 857059
  handle->semId = semCCreate(SEM_Q_FIFO, count);
Packit 857059
  handle->count = count;
Packit 857059
  if (!handle->semId)
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("failed to allocate semaphore");
Packit 857059
      rc = VSTATUS_ILLPARM;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
#else
Packit 857059
  // LINUX USER SEMA
Packit 857059
  handle->magic = SEMAPHORE_MAGIC;
Packit 857059
  if (sem_init((sem_t *)&handle->osdSema, 0, 0) != 0)
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("failed to allocate semaphore");
Packit 857059
      rc = VSTATUS_ILLPARM;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
#endif // vxworks
Packit 857059
Packit 857059
  IB_EXIT (function, VSTATUS_OK);
Packit 857059
  return VSTATUS_OK;
Packit 857059
}
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_sema_delete
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    Delete a counting semaphore object.
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object filled in by the
Packit 857059
*               cs_sema_create() call.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    Status_t - On success VSTATUS_OK is returned, otherwise
Packit 857059
*    the cause of the error.
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   MGR     03/14/02    Initial creation of function.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t
Packit 857059
cs_sema_delete (Sema_t *handle)
Packit 857059
{
Packit 857059
  Status_t          rc;
Packit 857059
Packit 857059
  IB_ENTER (function,
Packit 857059
            (unint)handle,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U);
Packit 857059
Packit 857059
  /*
Packit 857059
  ** Validate handle
Packit 857059
  */
Packit 857059
  if (handle == (Sema_t *) 0)
Packit 857059
  {
Packit 857059
    IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    IB_EXIT (function, VSTATUS_ILLPARM);
Packit 857059
    return VSTATUS_ILLPARM;
Packit 857059
  }
Packit 857059
Packit 857059
  if (handle->magic != SEMAPHORE_MAGIC)
Packit 857059
  {
Packit 857059
    IB_LOG_ERRORX ("Invalid semaphore specified:", handle->magic);
Packit 857059
    IB_EXIT (function, VSTATUS_NXIO);
Packit 857059
    return VSTATUS_NXIO;
Packit 857059
  }
Packit 857059
Packit 857059
#ifdef __VXWORKS__
Packit 857059
  if (!handle->semId)
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("NULL semaphore");
Packit 857059
      rc = VSTATUS_ILLPARM;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
  if (ERROR == semDelete(handle->semId))
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("failed to delete semaphore");
Packit 857059
      rc = VSTATUS_ILLPARM;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
  handle->semId = NULL;
Packit 857059
  handle->count = 0;
Packit 857059
#else
Packit 857059
  // LINUX USER SEMA
Packit 857059
  if (sem_destroy((sem_t *)&handle->osdSema) != 0) {
Packit 857059
      IB_LOG_ERROR0 ("failed to delete semaphore");
Packit 857059
      rc = VSTATUS_ILLPARM;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
  memset((void *)&handle->osdSema, 0, sizeof(sem_t));
Packit 857059
#endif
Packit 857059
Packit 857059
  IB_LOG_INFO ("semaphore deleted", (unint)handle);
Packit 857059
Packit 857059
  IB_EXIT (function, VSTATUS_OK);
Packit 857059
  return VSTATUS_OK;
Packit 857059
}
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_vsema
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    Increment a semaphore (post or give operation)
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object filled in by the
Packit 857059
*               cs_sema_create() call.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    Status_t - On success VSTATUS_OK is returned, otherwise
Packit 857059
*    the cause of the error.
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   MGR     03/14/02    Initial creation of function.
Packit 857059
*   MGR     04/23/02    PR1742.  Added fix for blocking or cs_psema;
Packit 857059
*                       cs_vsema should release lock if count goes to 1.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t
Packit 857059
cs_vsema (Sema_t *handle)
Packit 857059
{
Packit 857059
  Status_t          rc;
Packit 857059
Packit 857059
  IB_ENTER (function,
Packit 857059
            (unint)handle,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U);
Packit 857059
Packit 857059
  /*
Packit 857059
  ** Validate handle
Packit 857059
  */
Packit 857059
  if (handle == (Sema_t *) 0)
Packit 857059
  {
Packit 857059
    IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    IB_EXIT (function, VSTATUS_ILLPARM);
Packit 857059
    return VSTATUS_ILLPARM;
Packit 857059
  }
Packit 857059
Packit 857059
  if (handle->magic != SEMAPHORE_MAGIC)
Packit 857059
  {
Packit 857059
    IB_LOG_ERRORX ("Invalid semaphore specified:", handle->magic);
Packit 857059
    IB_EXIT (function, VSTATUS_NXIO);
Packit 857059
    return VSTATUS_NXIO;
Packit 857059
  }
Packit 857059
Packit 857059
#ifdef __VXWORKS__
Packit 857059
  if (ERROR == semGive(handle->semId))
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("Failed to give semaphore");
Packit 857059
      rc = VSTATUS_NXIO;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
  handle->count++;
Packit 857059
#else
Packit 857059
  // LINUX USER SEMA
Packit 857059
  if (sem_post((sem_t *)&handle->osdSema) != 0) {
Packit 857059
      IB_LOG_ERROR0 ("Failed to give/post semaphore");
Packit 857059
      rc = VSTATUS_NXIO;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
#endif
Packit 857059
Packit 857059
  IB_EXIT (function, VSTATUS_OK);
Packit 857059
  return VSTATUS_OK;
Packit 857059
}
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_psema
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    Decrement a semaphore (wait or take operation)
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object filled in by the
Packit 857059
*               cs_sema_create() call.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    Status_t - On success VSTATUS_OK is returned, otherwise
Packit 857059
*    the cause of the error.
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   MGR     03/14/02    Initial creation of function.
Packit 857059
*   MGR     04/18/02    PR1728.  vs_psema should return VSTATUS_AGAIN
Packit 857059
*                       error code, if thread has been killed.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t
Packit 857059
cs_psema_wait (Sema_t *handle, int timeout)
Packit 857059
{
Packit 857059
	Status_t          rc=VSTATUS_OK;
Packit 857059
Packit 857059
	IB_ENTER (function, (unint)handle, 0U, 0U, 0U);
Packit 857059
Packit 857059
  	/* Validate handle */
Packit 857059
  	if (handle == (Sema_t *) 0)
Packit 857059
  	{
Packit 857059
    	IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    	IB_EXIT (function, VSTATUS_ILLPARM);
Packit 857059
    	return VSTATUS_ILLPARM;
Packit 857059
  	}
Packit 857059
Packit 857059
  	if (handle->magic != SEMAPHORE_MAGIC)
Packit 857059
  	{
Packit 857059
    	IB_LOG_ERRORX ("Invalid semaphore specified:", handle->magic);
Packit 857059
    	IB_EXIT (function, VSTATUS_NXIO);
Packit 857059
    	return VSTATUS_NXIO;
Packit 857059
  	}
Packit 857059
Packit 857059
  	if (timeout == CS_SEMA_WAIT_FOREVER) {
Packit 857059
		return(cs_psema(handle));
Packit 857059
  	} else {
Packit 857059
#ifdef __VXWORKS__
Packit 857059
  		if (ERROR == semTake(handle->semId, timeout*sysClkRateGet()))
Packit 857059
  		{
Packit 857059
      		IB_LOG_WARN0("Timed out trying to take semaphore");
Packit 857059
      		rc = VSTATUS_NXIO;
Packit 857059
  		} else {
Packit 857059
            handle->count--;
Packit 857059
        }
Packit 857059
#elif defined (__LINUX__)
Packit 857059
  		// LINUX USER SEMA
Packit 857059
		int i=0, rc=-1;
Packit 857059
		do {
Packit 857059
  			rc = sem_trywait((sem_t *)&handle->osdSema);
Packit 857059
			++i;
Packit 857059
			(void)vs_thread_sleep(VTIMER_1S);
Packit 857059
		} while (i
Packit 857059
  		if (rc != 0) {
Packit 857059
      		IB_LOG_WARN0("Timed out trying to take semaphore");
Packit 857059
      		rc = VSTATUS_NXIO;
Packit 857059
  		}
Packit 857059
#endif
Packit 857059
	}
Packit 857059
  	IB_EXIT (function, rc);
Packit 857059
    return rc;
Packit 857059
}
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_psema
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    Decrement a semaphore (wait or take operation)
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object filled in by the
Packit 857059
*               cs_sema_create() call.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    Status_t - On success VSTATUS_OK is returned, otherwise
Packit 857059
*    the cause of the error.
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   MGR     03/14/02    Initial creation of function.
Packit 857059
*   MGR     04/18/02    PR1728.  vs_psema should return VSTATUS_AGAIN
Packit 857059
*                       error code, if thread has been killed.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t
Packit 857059
cs_psema (Sema_t *handle)
Packit 857059
{
Packit 857059
  Status_t          rc;
Packit 857059
Packit 857059
  IB_ENTER (function,
Packit 857059
            (unint)handle,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U,
Packit 857059
            (uint32_t)0U);
Packit 857059
Packit 857059
  /*
Packit 857059
  ** Validate handle
Packit 857059
  */
Packit 857059
  if (handle == (Sema_t *) 0)
Packit 857059
  {
Packit 857059
    IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    IB_EXIT (function, VSTATUS_ILLPARM);
Packit 857059
    return VSTATUS_ILLPARM;
Packit 857059
  }
Packit 857059
Packit 857059
  if (handle->magic != SEMAPHORE_MAGIC)
Packit 857059
  {
Packit 857059
    IB_LOG_ERRORX ("Invalid semaphore specified:", handle->magic);
Packit 857059
    IB_EXIT (function, VSTATUS_NXIO);
Packit 857059
    return VSTATUS_NXIO;
Packit 857059
  }
Packit 857059
Packit 857059
#ifdef __VXWORKS__
Packit 857059
  if (ERROR == semTake(handle->semId, WAIT_FOREVER))
Packit 857059
  {
Packit 857059
      IB_LOG_ERROR0 ("Failed to take semaphore");
Packit 857059
      rc = VSTATUS_NXIO;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
  handle->count--;
Packit 857059
#else
Packit 857059
  // LINUX USER SEMA
Packit 857059
  if (sem_wait((sem_t *)&handle->osdSema) != 0) {
Packit 857059
      IB_LOG_ERROR0 ("Failed to take semaphore");
Packit 857059
      rc = VSTATUS_NXIO;
Packit 857059
      IB_EXIT(function, rc);
Packit 857059
      return rc;
Packit 857059
  }
Packit 857059
#endif
Packit 857059
Packit 857059
  IB_EXIT (function, VSTATUS_OK);
Packit 857059
  return VSTATUS_OK;
Packit 857059
}
Packit 857059
Packit 857059
/**********************************************************************
Packit 857059
*
Packit 857059
* FUNCTION
Packit 857059
*    cs_sema_getcount
Packit 857059
*
Packit 857059
* DESCRIPTION
Packit 857059
*    return counting semaphore count
Packit 857059
*
Packit 857059
* INPUTS
Packit 857059
*    handle   - A pointer to a semaphore object filled in by the
Packit 857059
*               cs_sema_create() call.
Packit 857059
*
Packit 857059
* OUTPUTS
Packit 857059
*    int - semaphore count
Packit 857059
*
Packit 857059
*
Packit 857059
* HISTORY
Packit 857059
*
Packit 857059
*   NAME    DATE        REMARKS
Packit 857059
*   JMS     02/20/07    Initial creation of function.
Packit 857059
*
Packit 857059
**********************************************************************/
Packit 857059
Status_t cs_sema_getcount(Sema_t *handle, int *semCount)
Packit 857059
{
Packit 857059
	Status_t rc = VSTATUS_OK;
Packit 857059
Packit 857059
	IB_ENTER (function, (unint)handle, 0U, 0U, 0U);
Packit 857059
Packit 857059
  	/* Validate handle */
Packit 857059
  	if (handle == (Sema_t *) 0) {
Packit 857059
    	IB_LOG_ERROR0 ("NULL handle pointer");
Packit 857059
    	rc = VSTATUS_ILLPARM;
Packit 857059
  	} else if (handle->magic != SEMAPHORE_MAGIC) {
Packit 857059
    	IB_LOG_ERRORX ("Invalid semaphore specified:", handle->magic);
Packit 857059
    	rc = VSTATUS_NXIO;
Packit 857059
  	} else {
Packit 857059
#ifdef __VXWORKS__
Packit 857059
        *semCount = handle->count;
Packit 857059
#elif defined (__LINUX__)
Packit 857059
        if ((sem_getvalue((sem_t *)&handle->osdSema, semCount)) != 0)
Packit 857059
        {
Packit 857059
            IB_LOG_ERROR0 ("Failed to get semaphore count");
Packit 857059
            rc = VSTATUS_NXIO;
Packit 857059
        }
Packit 857059
#endif
Packit 857059
    }
Packit 857059
    IB_EXIT (function, rc);
Packit 857059
    return rc;
Packit 857059
}