os_kernel_lab/labcodes/lab8/user/libs/lock.h

43 lines
681 B
C
Raw Normal View History

2012-08-22 12:32:13 +08:00
#ifndef __USER_LIBS_LOCK_H__
#define __USER_LIBS_LOCK_H__
#include <defs.h>
#include <atomic.h>
#include <ulib.h>
#define INIT_LOCK {0}
typedef volatile bool lock_t;
static inline void
lock_init(lock_t *l) {
*l = 0;
}
static inline bool
try_lock(lock_t *l) {
return test_and_set_bit(0, l);
}
static inline void
lock(lock_t *l) {
if (try_lock(l)) {
int step = 0;
do {
yield();
if (++ step == 100) {
step = 0;
sleep(10);
}
} while (try_lock(l));
}
}
static inline void
unlock(lock_t *l) {
test_and_clear_bit(0, l);
}
#endif /* !__USER_LIBS_LOCK_H__ */