1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #include <mbmaster.h> 6 7 static mb_handler_t mb_handler[MBMASTER_CONFIG_HANDLER_MAX]; 8 mb_alloc_handler(void)9mb_handler_t* mb_alloc_handler(void) 10 { 11 MB_CRITICAL_ALLOC(); 12 MB_CRITICAL_ENTER(); 13 for (uint8_t i = 0; i < MBMASTER_CONFIG_HANDLER_MAX; i++) { 14 if (mb_handler[i].used == 0) { 15 mb_handler[i].used = 1; 16 MB_CRITICAL_EXIT(); 17 return &mb_handler[i]; 18 } 19 } 20 MB_CRITICAL_EXIT(); 21 return NULL; 22 } 23 mb_free_handler(mb_handler_t * handler)24void mb_free_handler(mb_handler_t *handler) 25 { 26 MB_CRITICAL_ALLOC(); 27 MB_CRITICAL_ENTER(); 28 if (handler->used ==1) { 29 handler->used = 0; 30 } 31 MB_CRITICAL_EXIT(); 32 } 33 mb_mutex_create(MB_MUTEX_T * mutex)34mb_status_t mb_mutex_create(MB_MUTEX_T *mutex) 35 { 36 kstat_t stat; 37 stat = krhino_mutex_create(mutex, "mb_mutex"); 38 if (stat == RHINO_SUCCESS) { 39 return MB_SUCCESS; 40 } else { 41 return MB_MUTEX_ERROR; 42 } 43 } 44 mb_mutex_lock(MB_MUTEX_T * mutex)45mb_status_t mb_mutex_lock(MB_MUTEX_T *mutex) 46 { 47 kstat_t stat; 48 stat = krhino_mutex_lock(mutex, RHINO_WAIT_FOREVER); 49 if (stat == RHINO_SUCCESS) { 50 return MB_SUCCESS; 51 } else { 52 return MB_MUTEX_ERROR; 53 } 54 } 55 mb_mutex_unlock(MB_MUTEX_T * mutex)56mb_status_t mb_mutex_unlock(MB_MUTEX_T *mutex) 57 { 58 kstat_t stat; 59 stat = krhino_mutex_unlock(mutex); 60 if (stat == RHINO_SUCCESS) { 61 return MB_SUCCESS; 62 } else { 63 return MB_MUTEX_ERROR; 64 } 65 } 66 mb_mutex_del(MB_MUTEX_T * mutex)67mb_status_t mb_mutex_del(MB_MUTEX_T *mutex) 68 { 69 kstat_t stat; 70 stat = krhino_mutex_del(mutex); 71 if (stat == RHINO_SUCCESS) { 72 return MB_SUCCESS; 73 } else { 74 return MB_MUTEX_ERROR; 75 } 76 } 77