1 #ifndef __ASM_SEMAPHORE_H__ 2 #define __ASM_SEMAPHORE_H__ 3 4 #define CONFIG_JFFS2_SEMAPHORE 0 // no mutex, 1 use static, 2 use dynamic 5 #if CONFIG_JFFS2_SEMAPHORE == 0 6 //#include <cyg/hal/drv_api.h> 7 8 struct semaphore { 9 int x; 10 }; 11 12 #define DECLARE_MUTEX(x) 13 #define DECLARE_MUTEX_LOCKED(x) 14 15 #define init_MUTEX(sem) 16 #define init_MUTEX_LOCKED(sem) 17 #define down(sem) 18 #define down_interruptible(sem) 0 19 #define down_trylock(sem) 20 #define up(sem) 21 22 #elif CONFIG_JFFS2_SEMAPHORE == 1 23 #include <rtthread.h> 24 25 struct semaphore { 26 struct rt_mutex mutex; 27 }; 28 29 #define DECLARE_MUTEX(x) 30 #define DECLARE_MUTEX_LOCKED(x) 31 rt_inline void init_MUTEX(struct semaphore * sem) 32 { 33 if (rt_mutex_init((rt_mutex_t)sem, "mutex", RT_IPC_FLAG_FIFO) == RT_EOK) 34 { 35 /* detach the object from system object container */ 36 rt_object_detach(&(((rt_mutex_t)sem)->parent.parent)); 37 return; 38 } 39 rt_kprintf("get an error at %s:%d \n", __FUNCTION__, __LINE__); 40 RT_ASSERT(0); 41 } 42 43 rt_inline void init_MUTEX_LOCKED(struct semaphore * sem) 44 { 45 rt_enter_critical(); 46 if (rt_mutex_init((rt_mutex_t)sem, "mutex", RT_IPC_FLAG_FIFO) == RT_EOK) 47 { 48 /* detach the object from system object container */ 49 rt_object_detach(&(((rt_mutex_t)sem)->parent.parent)); 50 rt_exit_critical(); 51 rt_mutex_take((rt_mutex_t)sem, RT_WAITING_FOREVER); 52 return; 53 } 54 rt_exit_critical(); 55 56 rt_kprintf("get an error at %s:%d \n", __FUNCTION__, __LINE__); 57 RT_ASSERT(0); 58 } 59 60 rt_inline down(struct semaphore * sem) 61 { 62 rt_mutex_take((rt_mutex_t)sem, RT_WAITING_FOREVER); 63 } 64 rt_inline int down_interruptible(struct semaphore* sem) 65 { 66 rt_mutex_take((rt_mutex_t)sem, RT_WAITING_FOREVER); 67 return 0; 68 } 69 rt_inline up(struct semaphore * sem) 70 { 71 rt_mutex_release((rt_mutex_t)sem); 72 } 73 #elif CONFIG_JFFS2_SEMAPHORE == 2 74 75 #include <rtthread.h> 76 77 struct semaphore { 78 rt_mutex_t mutex; 79 }; 80 81 #define DECLARE_MUTEX(x) 82 #define DECLARE_MUTEX_LOCKED(x) 83 84 rt_inline void init_MUTEX(struct semaphore * sem) 85 { 86 sem->mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); 87 } 88 rt_inline init_MUTEX_LOCKED(struct semaphore * sem) 89 { 90 sem->mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); 91 rt_mutex_take(sem->mutex, RT_WAITING_FOREVER); 92 } 93 rt_inline down(struct semaphore * sem) 94 { 95 rt_mutex_take(sem->mutex, RT_WAITING_FOREVER); 96 } 97 rt_inline int down_interruptible(struct semaphore* sem) 98 { 99 rt_mutex_take(sem->mutex, RT_WAITING_FOREVER); 100 return 0; 101 } 102 /* 103 Attempt to lock the mutex pointed to by the mutex argument without waiting. 104 If the mutex is already locked by some other thread then this function 105 returns FALSE. If the function can lock the mutex without waiting, then 106 TRUE is returned. 107 void cyg_drv_mutex_unlock( cyg_drv_mutex *mutex ) 108 */ 109 110 //#define down_trylock(struct semaphore * sem) rt_mutex_take((rt_mutex_t)sem, RT_WAITING_NO) 111 rt_inline up(struct semaphore * sem) 112 { 113 rt_mutex_release(sem->mutex); 114 } 115 116 #else 117 #error "CONFIG_JFFS2_SEMAPHORE should be 0, 1 or 2" 118 #endif 119 120 #endif /* __ASM_SEMAPHORE_H__ */ 121