- //*****************************************************************************
- //File Name: kb_machblue_core_semaphore.c
- //
- //Description: semaphore function
- //
- // used by Machblue to access the platform's synchronization
- //
- //Author: steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "semaphor.h"
- #include "mutex.h"
- #include "ostime.h"
- #include "machblue_defines.h"
- #include "machblue_customer.h"
- #include "machblue_porting_core.h"
- /**
- * Creates a new semaphore.
- * semaphore < pointer to semaphore to create >
- * initial_value < initial number of resource >
- * @return MB_SUCCESS and updates "semaphore" or MB_FAILURE on failure.
- */
- mb_error_t mb_semaphore_create(mb_semaphore_t *semaphore,unsigned int initial_value)
- {
- semaphore_t *pSem;
- pSem=semaphore_create_fifo_timeout(initial_value);
- if(pSem==NULL)
- {
- mb_printf("n[Machblue]:Semaphore create error.");
- return MB_FAILURE;
- }
- *semaphore=(mb_semaphore_t)pSem;
- return MB_SUCCESS;
- }
- /**
- * Deletes a semaphore.
- * semaphore < semaphore to delete >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_semaphore_delete(mb_semaphore_t semaphore)
- {
- semaphore_t *pSem=(semaphore_t *)semaphore;
- if(pSem==NULL)
- {
- mb_printf("n[Machblue]:Semaphore delete NULL.");
- return MB_FAILURE;
- }
- semaphore_delete(pSem);
- return MB_SUCCESS;
- }
- /**
- * Signals a counting semaphore (V).
- * semaphore < counting semaphore to signal >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_semaphore_signal(mb_semaphore_t semaphore)
- {
- semaphore_t *pSem=(semaphore_t *)semaphore;
- if(pSem==NULL)
- {
- mb_printf("n[Machblue]:Semaphore signal NULL.");
- return MB_FAILURE;
- }
- semaphore_signal(pSem);
- return MB_SUCCESS;
- }
- /**
- * Waits on a counting semaphore (P).
- * semaphore < Semaphore to wait on >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_semaphore_wait(mb_semaphore_t semaphore)
- {
- int ret;
- semaphore_t *pSem=(semaphore_t *)semaphore;
- if(pSem==NULL)
- {
- mb_printf("n[Machblue]:Semaphore wait NULL.");
- return MB_FAILURE;
- }
- ret=semaphore_wait_timeout(pSem,(clock_t*)TIMEOUT_INFINITY);
- if(ret!=0)
- {
- mb_printf("n[Machblue]:Semaphore wait error[%d].",ret);
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * Tries to wait on a semaphore (non-blocking). Returns immediatly
- * (does not block) if the semaphore has a zero count otherwise atomically
- * decreases count (consumes the resource).
- * @return MB_SUCCESS on success, MB_BUSY if the semaphore count is zero
- * or MB_FAILURE on failure.
- */
- mb_error_t mb_semaphore_trywait(mb_semaphore_t semaphore)
- {
- int ret;
- semaphore_t *pSem=(semaphore_t *)semaphore;
- if(pSem==NULL)
- {
- mb_printf("n[Machblue]:Semaphore try wait NULL.");
- return MB_FAILURE;
- }
- ret=semaphore_wait_timeout(pSem,(clock_t*)TIMEOUT_IMMEDIATE );
- if(ret!=0)
- {
- return MB_BUSY;
- }
- return MB_SUCCESS;
- }
- /**
- * Creates a new mutex. This should maps to the fast mutex implementation
- * (a.k.a. non reccursive mutex) on the platform.
- * mutex < pointer to mutex to create >
- * @return MB_SUCCESS and update "mutex" on success, or MB_FAILURE on failure.
- */
- mb_error_t mb_mutex_create(mb_mutex_t *mutex)
- {
- mutex_t *pMutex=NULL;
- pMutex=mutex_create_fifo();
- if(pMutex==NULL)
- {
- mb_printf("n[Machblue]:Mutex create error.");
- return MB_FAILURE;
- }
- *mutex=(mb_mutex_t)pMutex;
- return MB_SUCCESS;
- }
- /**
- * Deletes a mutex.
- * mutex < mutex to delete >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_mutex_delete(mb_mutex_t mutex)
- {
- mutex_t *pMutex=(mutex_t *)mutex;
- int ret;
- if(pMutex==NULL)
- {
- mb_printf("n[Machblue]:Mutex delete NULL.");
- return MB_FAILURE;
- }
- ret=mutex_delete(pMutex);
- if(ret!=0)
- {
- mb_printf("n[Machblue]:Mutex delete error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * Locks a mutex.
- * mutex < mutex to lock >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_mutex_lock(mb_mutex_t mutex)
- {
- mutex_t *pMutex=(mutex_t *)mutex;
- if(pMutex==NULL)
- {
- mb_printf("n[Machblue]:Mutex lock NULL.");
- return MB_FAILURE;
- }
- mutex_lock(pMutex);
- return MB_SUCCESS;
- }
- /**
- * Unlocks a mutex
- * mutex < mutex to unlock >
- * @return MB_SUCCESS on success or MB_FAILURE on failure.
- */
- mb_error_t mb_mutex_unlock(mb_mutex_t mutex)
- {
- mutex_t *pMutex=(mutex_t *)mutex;
- int ret;
- if(pMutex==NULL)
- {
- mb_printf("n[Machblue]:Mutex unlock NULL.");
- return MB_FAILURE;
- }
- ret=mutex_release(pMutex);
- if(ret!=0)
- {
- mb_printf("n[Machblue]:Mutex unlock error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * Tries to lock a mutex (non-blocking). Returns immediatly (does
- * not block) if the mutex is already locked otherwise locks it.
- * mutex < mutex to lock >
- * @return MB_SUCCESS on success, MB_BUSY if the mutex is already locked
- * or MB_FAILURE on failure.
- */
- mb_error_t mb_mutex_trylock(mb_mutex_t mutex)
- {
- mutex_t *pMutex=(mutex_t *)mutex;
- if(pMutex==NULL)
- {
- mb_printf("n[Machblue]:Mutex try lock NULL.");
- return MB_FAILURE;
- }
- if((pMutex->mutex_count)>0)
- return MB_BUSY;
- mutex_lock(pMutex);
- return MB_SUCCESS;
- }
- /** @} */