《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
964 B

12 years ago
  1. #ifndef __KERN_SYNC_SYNC_H__
  2. #define __KERN_SYNC_SYNC_H__
  3. #include <x86.h>
  4. #include <intr.h>
  5. #include <mmu.h>
  6. #include <assert.h>
  7. #include <atomic.h>
  8. #include <sched.h>
  9. static inline bool
  10. __intr_save(void) {
  11. if (read_eflags() & FL_IF) {
  12. intr_disable();
  13. return 1;
  14. }
  15. return 0;
  16. }
  17. static inline void
  18. __intr_restore(bool flag) {
  19. if (flag) {
  20. intr_enable();
  21. }
  22. }
  23. #define local_intr_save(x) do { x = __intr_save(); } while (0)
  24. #define local_intr_restore(x) __intr_restore(x);
  25. typedef volatile bool lock_t;
  26. static inline void
  27. lock_init(lock_t *lock) {
  28. *lock = 0;
  29. }
  30. static inline bool
  31. try_lock(lock_t *lock) {
  32. return !test_and_set_bit(0, lock);
  33. }
  34. static inline void
  35. lock(lock_t *lock) {
  36. while (!try_lock(lock)) {
  37. schedule();
  38. }
  39. }
  40. static inline void
  41. unlock(lock_t *lock) {
  42. if (!test_and_clear_bit(0, lock)) {
  43. panic("Unlock failed.\n");
  44. }
  45. }
  46. #endif /* !__KERN_SYNC_SYNC_H__ */