os_kernel_lab/labcodes/lab6/kern/sync/sync.h

58 lines
964 B
C
Raw Permalink Normal View History

2012-08-22 12:32:13 +08:00
#ifndef __KERN_SYNC_SYNC_H__
#define __KERN_SYNC_SYNC_H__
#include <x86.h>
#include <intr.h>
#include <mmu.h>
#include <assert.h>
#include <atomic.h>
#include <sched.h>
static inline bool
__intr_save(void) {
if (read_eflags() & FL_IF) {
intr_disable();
return 1;
}
return 0;
}
static inline void
__intr_restore(bool flag) {
if (flag) {
intr_enable();
}
}
#define local_intr_save(x) do { x = __intr_save(); } while (0)
#define local_intr_restore(x) __intr_restore(x);
typedef volatile bool lock_t;
static inline void
lock_init(lock_t *lock) {
*lock = 0;
}
static inline bool
try_lock(lock_t *lock) {
return !test_and_set_bit(0, lock);
}
static inline void
lock(lock_t *lock) {
while (!try_lock(lock)) {
schedule();
}
}
static inline void
unlock(lock_t *lock) {
if (!test_and_clear_bit(0, lock)) {
panic("Unlock failed.\n");
}
}
#endif /* !__KERN_SYNC_SYNC_H__ */