📄 osapi.c
字号:
return OS_SEM_FAILURE; /* Set the sem_id to the one that we found open */ /* Set the name of the semaphore, creator, and free as well */ *sem_id = possible_semid; semTake(OS_mut_sem_table_sem,WAIT_FOREVER); strcpy(OS_mut_sem_table[*sem_id].name, (char*)sem_name); OS_mut_sem_table[*sem_id].free = FALSE; OS_mut_sem_table[*sem_id].creator = OS_FindCreator(); semGive(OS_mut_sem_table_sem); return OS_SUCCESS;}/* end OS_MutSemCreate *//*-------------------------------------------------------------------------------------- Name: OS_MutSemDelete Purpose: Deletes the specified Mutex Semaphore. Returns: OS_ERR_INVALID_ID if the id passed in is not a valid mutex OS_ERR_SEM_NOT_FULL if the mutex is empty OS_SEM_FAILURE if the OS call failed OS_SUCCESS if success Notes: The mutex must be full to take it, so we have to check for fullness---------------------------------------------------------------------------------------*/int32 OS_MutSemDelete (uint32 sem_id){ /* Check to see if this sem_id is valid */ if (sem_id >= OS_MAX_MUTEXES || OS_mut_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_MutSemDelete can successfully take the * semaphore, it must have been free, and we can delete it, otherwise return * OS_ERR_SEM_NOT_FULL */ if (semTake(OS_mut_sem_table[sem_id].id, NO_WAIT) != OK) return OS_ERR_SEM_NOT_FULL; /* now we have successfully taken the semaphore */ if (semDelete(OS_mut_sem_table[sem_id].id) != OK) return OS_SEM_FAILURE; /* Delete its presence in the table */ semTake(OS_mut_sem_table_sem,WAIT_FOREVER); OS_mut_sem_table[sem_id].free = TRUE; OS_mut_sem_table[sem_id].id = NULL; strcpy(OS_mut_sem_table[sem_id].name , ""); OS_mut_sem_table[sem_id].creator = UNINITIALIZED; semGive(OS_mut_sem_table_sem); return OS_SUCCESS;}/* end OS_MutSemDelete *//*--------------------------------------------------------------------------------------- Name: OS_MutSemGive Purpose: The function releases the mutex object referenced by sem_id.The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when this function is called, resulting in the mutex becoming available, the scheduling policy shall determine which thread shall acquire the mutex. Returns: OS_SUCCESS if success OS_SEM_FAILURE if the semaphore was not previously initialized OS_ERR_INVALID_ID if the id passed in is not a valid mutex---------------------------------------------------------------------------------------*/int32 OS_MutSemGive (uint32 sem_id){ /* Check Parameters */ if(sem_id >= OS_MAX_MUTEXES || OS_mut_sem_table[sem_id].free == TRUE) return OS_ERR_INVALID_ID; /* Give VxWorks Semaphore */ if(semGive(OS_mut_sem_table[sem_id].id) != OK) return OS_SEM_FAILURE; return OS_SUCCESS;}/* end OS_MutSemGive *//*--------------------------------------------------------------------------------------- Name: OS_MutSemTake Purpose: The mutex object referenced by sem_id shall be locked by calling this function. If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner. Returns: OS_SUCCESS if success OS_SEM_FAILURE if 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 mutex---------------------------------------------------------------------------------------*/int32 OS_MutSemTake (uint32 sem_id){ /* Check Parameters */ if(sem_id >= OS_MAX_MUTEXES || OS_mut_sem_table[sem_id].free == TRUE) return OS_ERR_INVALID_ID; /* Take VxWorks Semaphore */ if(semTake(OS_mut_sem_table[sem_id].id, WAIT_FOREVER) != OK) return OS_SEM_FAILURE; return OS_SUCCESS;}/* end OS_MutSemGive *//*-------------------------------------------------------------------------------------- Name: OS_MutSemGetIdByName Purpose: This function tries to find a mutex 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_MutSemGetIdByName (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_MUTEXES; i++) { if ((OS_mut_sem_table[i].free != TRUE) && (strcmp (OS_mut_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_MutSemGetIdByName *//*--------------------------------------------------------------------------------------- Name: OS_MutnSemGetInfo Purpose: This function will pass back a pointer to structure that contains all of the relevant info( name and creator) about the specified mutex 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_MutSemGetInfo (uint32 sem_id, OS_mut_sem_prop_t *sem_prop) { /* Check to see that the id given is valid */ if (sem_id >= OS_MAX_MUTEXES || OS_mut_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_mut_sem_table_sem,WAIT_FOREVER); sem_prop -> creator = OS_mut_sem_table[sem_id].creator; strcpy(sem_prop-> name, OS_mut_sem_table[sem_id].name); semGive(OS_mut_sem_table_sem); return OS_SUCCESS; } /* end OS_BinSemGetInfo *//**************************************************************************************** INFO API****************************************************************************************//*--------------------------------------------------------------------------------------- Name: OS_Milli2Ticks Purpose: This function accepts a time interval in milli_seconds as input an returns the tick equivalent is o.s. system clock ticks. The tick value is rounded up. This algorthim should change to use a integer divide.---------------------------------------------------------------------------------------*/int32 OS_Milli2Ticks (uint32 milli_seconds){ return( ((sysClkRateGet() * milli_seconds) / 1000) + 1 ); /* * this function can be modified - it gives a good approx without any * floating point (">>10" ~= "/1000") */}/* end OS_Milli2Ticks *//*--------------------------------------------------------------------------------------- Name: OS_InfoGetTicks Purpose: This function returns the duration of a system tick in micro seconds.---------------------------------------------------------------------------------------*/int32 OS_Tick2Micros (void){ return( 1000000 / sysClkRateGet() ); }/* end OS_InfoGetTicks *//*--------------------------------------------------------------------------------------- * Name: OS_GetLocalTime * * Purpose: This functions get the local time of the machine its on * ------------------------------------------------------------------------------------*/int32 OS_GetLocalTime(OS_time_t *time_struct){ int status; struct timespec time; if (time_struct == NULL) return OS_INVALID_POINTER; status = clock_gettime(CLOCK_REALTIME, &time); if (status != OK) return OS_ERROR; time_struct -> seconds = time.tv_sec; time_struct -> microsecs = time.tv_nsec / 1000; return OS_SUCCESS;} /* end OS_GetLocalTime *//**************************************************************************************** INFO API****************************************************************************************//*--------------------------------------------------------------------------------------- Name: OS_IntAttachHandler Purpose: The call associates a specified C routine to a specified interrupt number.Upon occurring of the InterruptNumber the InerruptHandler routine will be called and passed the parameter. Parameters: InterruptNumber : The Interrupt Number that will cause the start of the ISR InerruptHandler : The ISR associatd with this interrupt parameter :The parameter that is passed to the ISR Notes: VxWorks wants the interrupt number to be the interrupt vector - i.e. the address that the the hardware reads from to grab the address of the corresponding ISR. On the PPC, the macro INUM_TO_IVEC does the conversion. The file "arch/ppc/ivPpc.h" in the Tornado2.2 tree has the definition.---------------------------------------------------------------------------------------*/int32 OS_IntAttachHandler (uint32 InterruptNumber, void *InterruptHandler, int32 parameter){ if (InterruptHandler == NULL) return OS_INVALID_POINTER; if(intConnect(INUM_TO_IVEC(InterruptNumber), (VOIDFUNCPTR)InterruptHandler, parameter) != OK) { return OS_ERROR; } return OS_SUCCESS;}/* end OS_IntAttachHandler *//*--------------------------------------------------------------------------------------- Name: OS_IntEnableAll Purpose: Enable the corresponding interrupt number. Parameters: InterruptNumber : The Interrupt Number to be enabled---------------------------------------------------------------------------------------*/int32 OS_IntEnableAll (void){ intUnlock(OS_interrupt_lock_key); return OS_SUCCESS;}/* end OS_IntEnableAll *//*--------------------------------------------------------------------------------------- Name: OS_IntDisableAll Purpose: Disable the corresponding interrupt number. Parameters: InterruptNumber : The Interrupt Number to be disabled---------------------------------------------------------------------------------------*/int32 OS_IntDisableAll (void){ OS_interrupt_lock_key = intLock(); return OS_SUCCESS;}/* end OS_IntDisableAll *//*--------------------------------------------------------------------------------------- * Name: OS_GetErrorName()---------------------------------------------------------------------------------------*/int32 OS_GetErrorName(uint32 error_num, os_err_name_t * err_name){ os_err_name_t local_name; uint32 return_code; return_code = OS_SUCCESS; switch (error_num) { case OS_SUCCESS: strcpy(local_name,"OS_SUCCESS"); break; case OS_ERROR: strcpy(local_name,"OS_ERROR"); break; case OS_INVALID_POINTER: strcpy(local_name,"OS_INVALID_POINTER"); break; case OS_ERROR_ADDRESS_MISALIGNED: strcpy(local_name,"OS_ADDRESS_MISALIGNED"); break; case OS_ERROR_TIMEOUT: strcpy(local_name,"OS_ERROR_TIMEOUT"); break; case OS_INVALID_INT_NUM: strcpy(local_name,"OS_INVALID_INT_NUM"); break; case OS_SEM_FAILURE: strcpy(local_name,"OS_SEM_FAILURE"); break; case OS_SEM_TIMEOUT: strcpy(local_name,"OS_SEM_TIMEOUT"); break; case OS_QUEUE_EMPTY: strcpy(local_name,"OS_QUEUE_EMPTY"); break; case OS_QUEUE_FULL: strcpy(local_name,"OS_QUEUE_FULL"); break; case OS_QUEUE_TIMEOUT: strcpy(local_name,"OS_QUEUE_TIMEOUT"); break; case OS_QUEUE_INVALID_SIZE: strcpy(local_name,"OS_QUEUE_INVALID_SIZE"); break; case OS_QUEUE_ID_ERROR: strcpy(local_name,"OS_QUEUE_ID_ERROR"); break; case OS_ERR_NAME_TOO_LONG: strcpy(local_name,"OS_ERR_NAME_TOO_LONG"); break; case OS_ERR_NO_FREE_IDS: strcpy(local_name,"OS_ERR_NO_FREE_IDS"); break; case OS_ERR_NAME_TAKEN: strcpy(local_name,"OS_ERR_NAME_TAKEN"); break; case OS_ERR_INVALID_ID: strcpy(local_name,"OS_ERR_INVALID_ID"); break; case OS_ERR_NAME_NOT_FOUND: strcpy(local_name,"OS_ERR_NAME_NOT_FOUND"); break; case OS_ERR_SEM_NOT_FULL: strcpy(local_name,"OS_ERR_SEM_NOT_FULL"); break; case OS_ERR_INVALID_PRIORITY: strcpy(local_name,"OS_ERR_INVALID_PRIORITY"); break; default: strcpy(local_name,"ERROR_UNKNOWN"); return_code = OS_ERROR; } strcpy((char*) err_name, local_name); return return_code;}/*--------------------------------------------------------------------------------------- * OS_FindCreator() * This function will return the OSAL ID of the task that created the calling function; * This is an internal call, not to be used by the user.---------------------------------------------------------------------------------------*/uint32 OS_FindCreator(){ int VxWorks_Id; int i; VxWorks_Id = taskIdSelf(); for (i = 0; i < OS_MAX_TASKS; i++) { if (VxWorks_Id == OS_task_table[i].id) break; } return i;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -