⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osapi.c

📁 osal for Vxworks,c code
💻 C
📖 第 1 页 / 共 4 页
字号:
}/* end OS_QueueGetIdByName *//*---------------------------------------------------------------------------------------    Name: OS_QueueGetInfo    Purpose: This function will pass back a pointer to structure that contains              all of the relevant info (name and creator) about the specified queue.     Returns: OS_INVALID_POINTER if queue_prop is NULL             OS_ERR_INVALID_ID if the ID given is not  a valid queue             OS_SUCCESS if the info was copied over correctly---------------------------------------------------------------------------------------*/int32 OS_QueueGetInfo (uint32 queue_id, OS_queue_prop_t *queue_prop)  {    /* Check to see that the id given is valid */        if (queue_prop == NULL)        return OS_INVALID_POINTER;        if (queue_id >= OS_MAX_QUEUES || OS_queue_table[queue_id].free == TRUE)        return OS_ERR_INVALID_ID;    /* put the info into the stucture */    semTake(OS_queue_table_sem,WAIT_FOREVER);    queue_prop -> creator =   OS_queue_table[queue_id].creator;    strcpy(queue_prop -> name, OS_queue_table[queue_id].name);    semGive(OS_queue_table_sem);    return OS_SUCCESS;    } /* end OS_QueueGetInfo *//****************************************************************************************                                  SEMAPHORE API****************************************************************************************//*---------------------------------------------------------------------------------------   Name: OS_BinSemCreate   Purpose: Creates a binary semaphore with initial value specified by            sem_initial_value and name specified by sem_name. sem_id will be             returned to the caller               Returns: OS_INVALID_POINTER if sen name or sem_id are NULL            OS_ERR_NAME_TOO_LONG if the name given is too long            OS_ERR_NO_FREE_IDS if all of the semaphore ids are taken            OS_ERR_NAME_TAKEN if this is already the name of a binary semaphore            OS_SEM_FAILURE if the OS call failed            OS_SUCCESS if success               Notes: options is an unused parameter ---------------------------------------------------------------------------------------*/int32 OS_BinSemCreate (uint32 *sem_id, const char *sem_name, uint32 sem_initial_value,                         uint32 options){    /* the current candidate for the new sem id */    uint32 possible_semid;    uint32 i;    if (sem_id == NULL || sem_name == NULL)        return OS_INVALID_POINTER;        /* we don't want to allow names too long*/    /* if truncated, two names might be the same */        if (strlen(sem_name) > OS_MAX_API_NAME)            return OS_ERR_NAME_TOO_LONG;    /* Check Parameters */    semTake(OS_bin_sem_table_sem,WAIT_FOREVER);    for (possible_semid = 0; possible_semid < OS_MAX_SEMAPHORES; possible_semid++)    {        if (OS_bin_sem_table[possible_semid].free == TRUE)                break;    }    semGive(OS_bin_sem_table_sem);    if((possible_semid >= OS_MAX_SEMAPHORES) ||         (OS_bin_sem_table[possible_semid].free != TRUE))        return OS_ERR_NO_FREE_IDS;        /* Check to see if the name is already taken */    semTake(OS_bin_sem_table_sem,WAIT_FOREVER);    for (i = 0; i < OS_MAX_SEMAPHORES; i++)    {        if (strcmp ((char*)sem_name, OS_bin_sem_table[i].name) == 0)        {            semGive(OS_bin_sem_table_sem);            return OS_ERR_NAME_TAKEN;        }    }    semGive(OS_bin_sem_table_sem);    /* Create VxWorks Semaphore */    OS_bin_sem_table[possible_semid].id = semBCreate(SEM_Q_PRIORITY, sem_initial_value);    /* check if semBCreate failed */    if(OS_bin_sem_table[possible_semid].id == NULL)        return OS_SEM_FAILURE;    /* Set the sem_id to the one that we found available */    /* Set the name of the semaphore,creator and free as well */    *sem_id = possible_semid;        semTake(OS_bin_sem_table_sem,WAIT_FOREVER);    OS_bin_sem_table[*sem_id].free = FALSE;    strcpy(OS_bin_sem_table[*sem_id].name , (char*) sem_name);    OS_bin_sem_table[*sem_id].creator = OS_FindCreator();    semGive(OS_bin_sem_table_sem);        return OS_SUCCESS;    }/* end OS_BinSemCreate *//*--------------------------------------------------------------------------------------     Name: OS_BinSemDelete    Purpose: Deletes the specified Binary Semaphore.    Returns: OS_ERR_INVALID_ID if the id passed in is not a valid binary semaphore             OS_ERR_SEM_NOT_FULL if the semahore is taken and cannot be deleted             OS_SEM_FAILURE the OS call failed             OS_SUCCESS if success        Notes: Since we can't delete a semaphore which is currently locked by some task            (as it may ber crucial to completing the task), the semaphore must be full to           allow deletion.---------------------------------------------------------------------------------------*/int32 OS_BinSemDelete (uint32 sem_id){    /* Check to see if this sem_id is valid */    if (sem_id >= OS_MAX_SEMAPHORES || OS_bin_sem_table[sem_id].free == TRUE)        return OS_ERR_INVALID_ID;    /* To make sure it is safe to delete a semaphore, we have to make sure that the     * semaphore is full. Therefore, if OS_BinSemDelete can successfully take the     * semaphore, it must have been free, and we can delete it, otherwise return OS_ERROR    */        if (semTake(OS_bin_sem_table[sem_id].id, NO_WAIT) != OK)        return OS_ERR_SEM_NOT_FULL;        /* now we have successfully taken the semaphore */        if (semDelete(OS_bin_sem_table[sem_id].id) != OK)        return OS_SEM_FAILURE;        /* Remove the Id from the table, and its name, so that it cannot be found again */    semTake(OS_bin_sem_table_sem,WAIT_FOREVER);    OS_bin_sem_table[sem_id].free = TRUE;    strcpy(OS_bin_sem_table[sem_id].name , "");    OS_bin_sem_table[sem_id].creator = UNINITIALIZED;    OS_bin_sem_table[sem_id].id = NULL;    semGive(OS_bin_sem_table_sem);        return OS_SUCCESS;}/* end OS_BinSemDelete *//*---------------------------------------------------------------------------------------    Name: OS_BinSemGive    Purpose: The function  unlocks the semaphore referenced by sem_id by performing             a semaphore unlock operation on that semaphore.If the semaphore value              resulting from this operation is positive, then no threads were blocked             waiting for the semaphore to become unlocked; the semaphore value is             simply incremented for this semaphore.        Returns: OS_SEM_FAILURE the semaphore was not previously  initialized or is not             in the array of semaphores defined by the system             OS_ERR_INVALID_ID if the id passed in is not a binary semaphore             OS_SUCCESS if success                ---------------------------------------------------------------------------------------*/int32 OS_BinSemGive (uint32 sem_id){    /* Check Parameters */    if(sem_id >= OS_MAX_SEMAPHORES || OS_bin_sem_table[sem_id].free == TRUE)        return OS_ERR_INVALID_ID;    /* Give VxWorks Semaphore */    if(semGive(OS_bin_sem_table[sem_id].id) != OK)        return OS_SEM_FAILURE;    return OS_SUCCESS;}/* end OS_BinSemGive *//*---------------------------------------------------------------------------------------    Name:    OS_BinSemTake    Purpose: The locks the semaphore referenced by sem_id by performing a              semaphore lock operation on that semaphore.If the semaphore value              is currently zero, then the calling thread shall not return from              the call until it either locks the semaphore or the call is              interrupted by a signal.    Return:  OS_SEM_FAILURE : the semaphore was not previously initialized             or is not in the array of semaphores defined by the system             OS_ERR_INVALID_ID the Id passed in is not a valid binar semaphore             OS_SEM_FAILURE if the OS call failed             OS_SUCCESS if success             ----------------------------------------------------------------------------------------*/int32 OS_BinSemTake (uint32 sem_id){    /* Check Parameters */    if(sem_id >= OS_MAX_SEMAPHORES  || OS_bin_sem_table[sem_id].free == TRUE)        return OS_ERR_INVALID_ID;    /* Give VxWorks Semaphore */    if(semTake(OS_bin_sem_table[sem_id].id, WAIT_FOREVER) != OK)        return OS_SEM_FAILURE;    return OS_SUCCESS;}/* end OS_BinSemTake *//*---------------------------------------------------------------------------------------    Name: OS_BinSemTimedWait        Purpose: The function locks the semaphore referenced by sem_id . However,             if the semaphore cannot be locked without waiting for another process             or thread to unlock the semaphore , this wait shall be terminated when              the specified timeout ,msecs, expires.    Returns: OS_SEM_TIMEOUT if semaphore was not relinquished in time             OS_SUCCESS if success             OS_SEM_FAILURE the semaphore was not previously initialized or is not             in the array of semaphores defined by the system             OS_ERR_INVALID_ID if the ID passed in is not a valid semaphore ID----------------------------------------------------------------------------------------*/int32 OS_BinSemTimedWait (uint32 sem_id, uint32 msecs){    /* msecs rounded to the closest system tick count */    uint32 sys_ticks;                                                                   sys_ticks = OS_Milli2Ticks(msecs);    /* Check Parameters */    if( (sem_id >= OS_MAX_SEMAPHORES) || (OS_bin_sem_table[sem_id].free == TRUE) )        return OS_ERR_INVALID_ID;    /* Give VxWorks Semaphore */    if(semTake(OS_bin_sem_table[sem_id].id , sys_ticks) != OK)        return OS_SEM_TIMEOUT;    return OS_SUCCESS;}/* end OS_BinSemTimedWait *//*--------------------------------------------------------------------------------------    Name: OS_BinSemGetIdByName    Purpose: This function tries to find a binary sem Id given the name of a bin_sem             The id is returned through sem_id    Returns: OS_INVALID_POINTER is semid or sem_name are NULL pointers             OS_ERR_NAME_TOO_LONG if the name given is to long to have been stored             OS_ERR_NAME_NOT_FOUND if the name was not found in the table             OS_SUCCESS if success             ---------------------------------------------------------------------------------------*/int32 OS_BinSemGetIdByName (uint32 *sem_id, const char *sem_name){    uint32 i;    if (sem_id == NULL || sem_name == NULL)        return OS_INVALID_POINTER;    /* a name too long wouldn't have been allowed in the first place     * so we definitely won't find a name too long*/        if (strlen(sem_name) > OS_MAX_API_NAME)            return OS_ERR_NAME_TOO_LONG;    for (i = 0; i < OS_MAX_SEMAPHORES; i++)    {        if ( OS_bin_sem_table[i].free != TRUE &&            ( strcmp (OS_bin_sem_table[i].name , (char*) sem_name) == 0)    )        {            *sem_id = i;            return OS_SUCCESS;        }    }    /* The name was not found in the table,     *  or it was, and the sem_id isn't valid anymore */    return OS_ERR_NAME_NOT_FOUND;    }/* end OS_BinSemGetIdByName *//*---------------------------------------------------------------------------------------    Name: OS_BinSemGetInfo    Purpose: This function will pass back a pointer to structure that contains              all of the relevant info( name and creator) about the specified binary             semaphore.                 Returns: OS_ERR_INVALID_ID if the id passed in is not a valid semaphore              OS_INVALID_POINTER if the sem_prop pointer is null             OS_SUCCESS if success---------------------------------------------------------------------------------------*/int32 OS_BinSemGetInfo (uint32 sem_id, OS_bin_sem_prop_t *sem_prop)  {    /* Check to see that the id given is valid */        if (sem_id >= OS_MAX_SEMAPHORES || OS_bin_sem_table[sem_id].free == TRUE)        return OS_ERR_INVALID_ID;    if (sem_prop == NULL)        return OS_INVALID_POINTER;    /* put the info into the stucture */    semTake(OS_bin_sem_table_sem,WAIT_FOREVER);    sem_prop ->creator =    OS_bin_sem_table[sem_id].creator;    strcpy(sem_prop-> name, OS_bin_sem_table[sem_id].name);    semGive(OS_bin_sem_table_sem);    return OS_SUCCESS;    } /* end OS_BinSemGetInfo *//****************************************************************************************                                  MUTEX API****************************************************************************************//*---------------------------------------------------------------------------------------    Name: OS_MutSemCreate    Purpose: Creates a mutex semaphore initially full.    Returns: OS_INVALID_POINTER if sem_id or sem_name are NULL             OS_ERR_NAME_TOO_LONG if the sem_name is too long to be stored             OS_ERR_NO_FREE_IDS if there are no more free mutex Ids             OS_ERR_NAME_TAKEN if there is already a mutex with the same name             OS_SEM_FAILURE if the OS call failed             OS_SUCCESS if success        Notes: the options parameter is not used in this implementation---------------------------------------------------------------------------------------*/int32 OS_MutSemCreate (uint32 *sem_id, const char *sem_name, uint32 options){    uint32 possible_semid;    uint32 i;        /* Check Parameters */    if (sem_id == NULL || sem_name == NULL)        return OS_INVALID_POINTER;        /* we don't want to allow names too long*/    /* if truncated, two names might be the same */        if (strlen(sem_name) > OS_MAX_API_NAME)            return OS_ERR_NAME_TOO_LONG;    semTake(OS_mut_sem_table_sem,WAIT_FOREVER);    for (possible_semid = 0; possible_semid < OS_MAX_MUTEXES; possible_semid++)    {        if (OS_mut_sem_table[possible_semid].free == TRUE)                break;    }    semGive(OS_mut_sem_table_sem);    if( (possible_semid >= OS_MAX_MUTEXES) ||        (OS_mut_sem_table[possible_semid].free != TRUE) )        return OS_ERR_NO_FREE_IDS;        /* Check to see if the name is already taken */    semTake(OS_mut_sem_table_sem,WAIT_FOREVER);    for (i = 0; i < OS_MAX_MUTEXES; i++)    {        if (strcmp ((char*) sem_name, OS_mut_sem_table[i].name) == 0)        {            semGive(OS_mut_sem_table_sem);            return OS_ERR_NAME_TAKEN;        }    }    semGive(OS_mut_sem_table_sem);    /* Create VxWorks Semaphore */    OS_mut_sem_table[possible_semid].id = semMCreate(SEM_Q_PRIORITY);     /* check if semMCreate failed */    if(OS_mut_sem_table[possible_semid].id == NULL)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -