Subversion Repositories Kolibri OS

Rev

Rev 3482 | Rev 5056 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef _LINUX_WAIT_H
  2. #define _LINUX_WAIT_H
  3.  
  4.  
  5. #include <linux/list.h>
  6. #include <syscall.h>
  7.  
  8. typedef struct __wait_queue wait_queue_t;
  9. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  10. int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
  11.  
  12. typedef struct __wait_queue_head wait_queue_head_t;
  13.  
  14. struct __wait_queue
  15. {
  16.     wait_queue_func_t func;
  17.     struct list_head task_list;
  18.     evhandle_t evnt;
  19. };
  20.  
  21. struct __wait_queue_head
  22. {
  23.     spinlock_t lock;
  24.     struct list_head task_list;
  25. };
  26. static inline int waitqueue_active(wait_queue_head_t *q)
  27. {
  28.         return !list_empty(&q->task_list);
  29. }
  30.  
  31. static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
  32. {
  33.     list_add(&new->task_list, &head->task_list);
  34. }
  35.  
  36. /*
  37. #define __wait_event(wq, condition)                                     \
  38. do {                                                                    \
  39.         DEFINE_WAIT(__wait);                                            \
  40.                                                                         \
  41.         for (;;) {                                                      \
  42.                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
  43.                 if (condition)                                          \
  44.                         break;                                          \
  45.                 schedule();                                             \
  46.         }                                                               \
  47.         finish_wait(&wq, &__wait);                                      \
  48. } while (0)
  49.  
  50. */
  51.  
  52. #define wait_event_timeout(wq, condition, timeout)          \
  53. ({                                                          \
  54.     long __ret = timeout;                                   \
  55. do{                                                         \
  56.     wait_queue_t __wait = {                                 \
  57.         .task_list = LIST_HEAD_INIT(__wait.task_list),      \
  58.         .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
  59.     };                                                      \
  60.     unsigned long flags;                                    \
  61.                                                             \
  62.     spin_lock_irqsave(&wq.lock, flags);                     \
  63.     if (list_empty(&__wait.task_list))                      \
  64.         __add_wait_queue(&wq, &__wait);                     \
  65.     spin_unlock_irqrestore(&wq.lock, flags);                \
  66.                                                             \
  67.     for(;;){                                                \
  68.         if (condition)                                      \
  69.             break;                                          \
  70.         WaitEventTimeout(__wait.evnt, timeout);             \
  71.     };                                                      \
  72.     if (!list_empty(&__wait.task_list)) {                   \
  73.         spin_lock_irqsave(&wq.lock, flags);                 \
  74.         list_del_init(&__wait.task_list);                   \
  75.         spin_unlock_irqrestore(&wq.lock, flags);            \
  76.     };                                                      \
  77.     DestroyEvent(__wait.evnt);                              \
  78. } while (0);                                                \
  79.     __ret;                                                  \
  80. })
  81.  
  82. #define wait_event_interruptible_timeout(wq, condition, timeout)    \
  83.         wait_event_timeout(wq, condition, timeout)
  84.  
  85.  
  86. #define wait_event(wq, condition)                           \
  87. do{                                                         \
  88.     wait_queue_t __wait = {                                 \
  89.         .task_list = LIST_HEAD_INIT(__wait.task_list),      \
  90.         .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
  91.     };                                                      \
  92.     unsigned long flags;                                    \
  93.                                                             \
  94.     spin_lock_irqsave(&wq.lock, flags);                     \
  95.     if (list_empty(&__wait.task_list))                      \
  96.         __add_wait_queue(&wq, &__wait);                     \
  97.     spin_unlock_irqrestore(&wq.lock, flags);                \
  98.                                                             \
  99.     for(;;){                                                \
  100.         if (condition)                                      \
  101.             break;                                          \
  102.         WaitEvent(__wait.evnt);                             \
  103.     };                                                      \
  104.     if (!list_empty_careful(&__wait.task_list)) {           \
  105.         spin_lock_irqsave(&wq.lock, flags);                 \
  106.         list_del_init(&__wait.task_list);                   \
  107.         spin_unlock_irqrestore(&wq.lock, flags);            \
  108.     };                                                      \
  109.     DestroyEvent(__wait.evnt);                              \
  110. } while (0)
  111.  
  112.  
  113.  
  114.  
  115. static inline
  116. void wake_up_all(wait_queue_head_t *q)
  117. {
  118.     wait_queue_t *curr;
  119.     unsigned long flags;
  120.  
  121.     spin_lock_irqsave(&q->lock, flags);
  122.     list_for_each_entry(curr, &q->task_list, task_list)
  123.     {
  124. //        printf("raise event \n");
  125.  
  126.         kevent_t event;
  127.         event.code = -1;
  128.         RaiseEvent(curr->evnt, 0, &event);
  129.     }
  130.     spin_unlock_irqrestore(&q->lock, flags);
  131. }
  132.  
  133.  
  134. static inline void
  135. init_waitqueue_head(wait_queue_head_t *q)
  136. {
  137.     spin_lock_init(&q->lock);
  138.     INIT_LIST_HEAD(&q->task_list);
  139. };
  140.  
  141.  
  142. struct completion {
  143.     unsigned int done;
  144.     wait_queue_head_t wait;
  145. };
  146.  
  147. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  148.  
  149.  
  150. #define DEFINE_WAIT_FUNC(name, function)                                \
  151.         wait_queue_t name = {                                           \
  152.                 .func           = function,                             \
  153.                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
  154.                 .evnt           = CreateEvent(NULL, MANUAL_DESTROY),    \
  155.         }
  156.  
  157. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
  158.  
  159.  
  160. #endif
  161.  
  162.