Subversion Repositories Kolibri OS

Rev

Rev 3391 | Rev 3482 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3391 Rev 3480
1
#ifndef _LINUX_WAIT_H
1
#ifndef _LINUX_WAIT_H
2
#define _LINUX_WAIT_H
2
#define _LINUX_WAIT_H
3
 
3
 
4
#include 
4
#include 
5
#include 
5
#include 
6
 
6
 
7
typedef struct __wait_queue wait_queue_t;
7
typedef struct __wait_queue wait_queue_t;
8
typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
8
typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
9
 
9
 
10
typedef struct __wait_queue_head wait_queue_head_t;
10
typedef struct __wait_queue_head wait_queue_head_t;
11
 
11
 
12
struct __wait_queue
12
struct __wait_queue
13
{
13
{
14
    wait_queue_func_t func;
14
    wait_queue_func_t func;
15
    struct list_head task_list;
15
    struct list_head task_list;
16
    evhandle_t evnt;
16
    evhandle_t evnt;
17
};
17
};
18
 
18
 
19
struct __wait_queue_head
19
struct __wait_queue_head
20
{
20
{
21
    spinlock_t lock;
21
    spinlock_t lock;
22
    struct list_head task_list;
22
    struct list_head task_list;
23
};
23
};
24
 
24
 
25
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
25
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
26
{
26
{
27
    list_add(&new->task_list, &head->task_list);
27
    list_add(&new->task_list, &head->task_list);
28
}
28
}
29
 
29
 
30
 
30
 
31
#define __wait_event(wq, condition)                                     \
31
#define __wait_event(wq, condition)                                     \
32
do {                                                                    \
32
do {                                                                    \
33
        DEFINE_WAIT(__wait);                                            \
33
        DEFINE_WAIT(__wait);                                            \
34
                                                                        \
34
                                                                        \
35
        for (;;) {                                                      \
35
        for (;;) {                                                      \
36
                prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
36
                prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
37
                if (condition)                                          \
37
                if (condition)                                          \
38
                        break;                                          \
38
                        break;                                          \
39
                schedule();                                             \
39
                schedule();                                             \
40
        }                                                               \
40
        }                                                               \
41
        finish_wait(&wq, &__wait);                                      \
41
        finish_wait(&wq, &__wait);                                      \
42
} while (0)
42
} while (0)
43
 
43
 
44
 
44
 
45
 
45
 
46
#define wait_event_timeout(wq, condition, timeout)          \
46
#define wait_event_timeout(wq, condition, timeout)          \
47
({                                                          \
47
({                                                          \
48
    long __ret = timeout;                                   \
48
    long __ret = timeout;                                   \
49
do{                                                         \
49
do{                                                         \
50
    wait_queue_t __wait = {                                 \
50
    wait_queue_t __wait = {                                 \
51
        .task_list = LIST_HEAD_INIT(__wait.task_list),      \
51
        .task_list = LIST_HEAD_INIT(__wait.task_list),      \
52
        .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
52
        .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
53
    };                                                      \
53
    };                                                      \
54
    unsigned long flags;                                    \
54
    unsigned long flags;                                    \
55
                                                            \
55
                                                            \
56
    spin_lock_irqsave(&wq.lock, flags);                     \
56
    spin_lock_irqsave(&wq.lock, flags);                     \
57
    if (list_empty(&__wait.task_list))                      \
57
    if (list_empty(&__wait.task_list))                      \
58
        __add_wait_queue(&wq, &__wait);                     \
58
        __add_wait_queue(&wq, &__wait);                     \
59
    spin_unlock_irqrestore(&wq.lock, flags);                \
59
    spin_unlock_irqrestore(&wq.lock, flags);                \
60
                                                            \
60
                                                            \
61
    for(;;){                                                \
61
    for(;;){                                                \
62
        if (condition)                                      \
62
        if (condition)                                      \
63
            break;                                          \
63
            break;                                          \
64
        WaitEventTimeout(__wait.evnt, timeout);             \
64
        WaitEventTimeout(__wait.evnt, timeout);             \
65
    };                                                      \
65
    };                                                      \
66
    if (!list_empty(&__wait.task_list)) {                   \
66
    if (!list_empty(&__wait.task_list)) {                   \
67
        spin_lock_irqsave(&wq.lock, flags);                 \
67
        spin_lock_irqsave(&wq.lock, flags);                 \
68
        list_del_init(&__wait.task_list);                   \
68
        list_del_init(&__wait.task_list);                   \
69
        spin_unlock_irqrestore(&wq.lock, flags);            \
69
        spin_unlock_irqrestore(&wq.lock, flags);            \
70
    };                                                      \
70
    };                                                      \
71
    DestroyEvent(__wait.evnt);                              \
71
    DestroyEvent(__wait.evnt);                              \
72
} while (0);                                                \
72
} while (0);                                                \
73
    __ret;                                                  \
73
    __ret;                                                  \
74
})
74
})
75
 
75
 
76
#define wait_event_interruptible_timeout(wq, condition, timeout)    \
76
#define wait_event_interruptible_timeout(wq, condition, timeout)    \
77
        wait_event_timeout(wq, condition, timeout)
77
        wait_event_timeout(wq, condition, timeout)
78
 
78
 
79
 
79
 
80
#define wait_event(wq, condition)                           \
80
#define wait_event(wq, condition)                           \
81
do{                                                         \
81
do{                                                         \
82
    wait_queue_t __wait = {                                 \
82
    wait_queue_t __wait = {                                 \
83
        .task_list = LIST_HEAD_INIT(__wait.task_list),      \
83
        .task_list = LIST_HEAD_INIT(__wait.task_list),      \
84
        .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
84
        .evnt      = CreateEvent(NULL, MANUAL_DESTROY),     \
85
    };                                                      \
85
    };                                                      \
86
    unsigned long flags;                                    \
86
    unsigned long flags;                                    \
87
                                                            \
87
                                                            \
88
    spin_lock_irqsave(&wq.lock, flags);                     \
88
    spin_lock_irqsave(&wq.lock, flags);                     \
89
    if (list_empty(&__wait.task_list))                      \
89
    if (list_empty(&__wait.task_list))                      \
90
        __add_wait_queue(&wq, &__wait);                     \
90
        __add_wait_queue(&wq, &__wait);                     \
91
    spin_unlock_irqrestore(&wq.lock, flags);                \
91
    spin_unlock_irqrestore(&wq.lock, flags);                \
92
                                                            \
92
                                                            \
93
    for(;;){                                                \
93
    for(;;){                                                \
94
        if (condition)                                      \
94
        if (condition)                                      \
95
            break;                                          \
95
            break;                                          \
96
        WaitEvent(__wait.evnt);                             \
96
        WaitEvent(__wait.evnt);                             \
97
    };                                                      \
97
    };                                                      \
98
    if (!list_empty_careful(&__wait.task_list)) {           \
98
    if (!list_empty_careful(&__wait.task_list)) {           \
99
        spin_lock_irqsave(&wq.lock, flags);                 \
99
        spin_lock_irqsave(&wq.lock, flags);                 \
100
        list_del_init(&__wait.task_list);                   \
100
        list_del_init(&__wait.task_list);                   \
101
        spin_unlock_irqrestore(&wq.lock, flags);            \
101
        spin_unlock_irqrestore(&wq.lock, flags);            \
102
    };                                                      \
102
    };                                                      \
103
    DestroyEvent(__wait.evnt);                              \
103
    DestroyEvent(__wait.evnt);                              \
104
} while (0)
104
} while (0)
105
 
105
 
106
 
106
 
107
 
107
 
108
 
108
 
109
static inline
109
static inline
110
void wake_up_all(wait_queue_head_t *q)
110
void wake_up_all(wait_queue_head_t *q)
111
{
111
{
112
    wait_queue_t *curr;
112
    wait_queue_t *curr;
113
    unsigned long flags;
113
    unsigned long flags;
114
 
114
 
115
    spin_lock_irqsave(&q->lock, flags);
115
    spin_lock_irqsave(&q->lock, flags);
116
    list_for_each_entry(curr, &q->task_list, task_list)
116
    list_for_each_entry(curr, &q->task_list, task_list)
117
    {
117
    {
118
//        printf("raise event \n");
118
//        printf("raise event \n");
119
 
119
 
120
        kevent_t event;
120
        kevent_t event;
121
        event.code = -1;
121
        event.code = -1;
122
        RaiseEvent(curr->evnt, 0, &event);
122
        RaiseEvent(curr->evnt, 0, &event);
123
    }
123
    }
124
    spin_unlock_irqrestore(&q->lock, flags);
124
    spin_unlock_irqrestore(&q->lock, flags);
125
}
125
}
126
 
126
 
127
 
127
 
128
static inline void
128
static inline void
129
init_waitqueue_head(wait_queue_head_t *q)
129
init_waitqueue_head(wait_queue_head_t *q)
130
{
130
{
131
    spin_lock_init(&q->lock);
131
    spin_lock_init(&q->lock);
132
    INIT_LIST_HEAD(&q->task_list);
132
    INIT_LIST_HEAD(&q->task_list);
133
};
133
};
134
 
134
 
135
 
135
 
136
/*
136
/*
137
 * Workqueue flags and constants.  For details, please refer to
137
 * Workqueue flags and constants.  For details, please refer to
138
 * Documentation/workqueue.txt.
138
 * Documentation/workqueue.txt.
139
 */
139
 */
140
enum {
140
enum {
141
    WQ_NON_REENTRANT    = 1 << 0, /* guarantee non-reentrance */
141
    WQ_NON_REENTRANT    = 1 << 0, /* guarantee non-reentrance */
142
    WQ_UNBOUND          = 1 << 1, /* not bound to any cpu */
142
    WQ_UNBOUND          = 1 << 1, /* not bound to any cpu */
143
    WQ_FREEZABLE        = 1 << 2, /* freeze during suspend */
143
    WQ_FREEZABLE        = 1 << 2, /* freeze during suspend */
144
    WQ_MEM_RECLAIM      = 1 << 3, /* may be used for memory reclaim */
144
    WQ_MEM_RECLAIM      = 1 << 3, /* may be used for memory reclaim */
145
    WQ_HIGHPRI          = 1 << 4, /* high priority */
145
    WQ_HIGHPRI          = 1 << 4, /* high priority */
146
    WQ_CPU_INTENSIVE    = 1 << 5, /* cpu instensive workqueue */
146
    WQ_CPU_INTENSIVE    = 1 << 5, /* cpu instensive workqueue */
147
 
147
 
148
    WQ_DRAINING         = 1 << 6, /* internal: workqueue is draining */
148
    WQ_DRAINING         = 1 << 6, /* internal: workqueue is draining */
149
    WQ_RESCUER          = 1 << 7, /* internal: workqueue has rescuer */
149
    WQ_RESCUER          = 1 << 7, /* internal: workqueue has rescuer */
150
 
150
 
151
    WQ_MAX_ACTIVE       = 512,    /* I like 512, better ideas? */
151
    WQ_MAX_ACTIVE       = 512,    /* I like 512, better ideas? */
152
    WQ_MAX_UNBOUND_PER_CPU  = 4,      /* 4 * #cpus for unbound wq */
152
    WQ_MAX_UNBOUND_PER_CPU  = 4,      /* 4 * #cpus for unbound wq */
153
    WQ_DFL_ACTIVE       = WQ_MAX_ACTIVE / 2,
153
    WQ_DFL_ACTIVE       = WQ_MAX_ACTIVE / 2,
154
};
154
};
155
 
155
 
156
struct work_struct;
156
struct work_struct;
157
 
157
 
158
struct workqueue_struct {
158
struct workqueue_struct {
159
    spinlock_t lock;
159
    spinlock_t lock;
160
    struct list_head worklist;
160
    struct list_head worklist;
161
};
161
};
162
 
162
 
163
typedef void (*work_func_t)(struct work_struct *work);
163
typedef void (*work_func_t)(struct work_struct *work);
164
 
164
 
165
struct work_struct {
165
struct work_struct {
166
    struct list_head entry;
166
    struct list_head entry;
167
    struct workqueue_struct *data;
167
    struct workqueue_struct *data;
168
    work_func_t func;
168
    work_func_t func;
169
};
169
};
170
 
170
 
171
struct delayed_work {
171
struct delayed_work {
172
    struct work_struct work;
172
    struct work_struct work;
173
};
173
};
-
 
174
 
-
 
175
static inline struct delayed_work *to_delayed_work(struct work_struct *work)
-
 
176
{
-
 
177
    return container_of(work, struct delayed_work, work);
-
 
178
}
-
 
179
 
-
 
180
 
174
 
181
 
175
struct workqueue_struct *alloc_workqueue_key(const char *fmt,
182
struct workqueue_struct *alloc_workqueue_key(const char *fmt,
176
                           unsigned int flags, int max_active);
183
                           unsigned int flags, int max_active);
177
 
184
 
178
 
185
 
179
#define alloc_ordered_workqueue(fmt, flags, args...)            \
186
#define alloc_ordered_workqueue(fmt, flags, args...)            \
180
        alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args)
187
        alloc_workqueue(fmt, WQ_UNBOUND | (flags), 1, ##args)
181
 
188
 
182
int queue_delayed_work(struct workqueue_struct *wq,
189
int queue_delayed_work(struct workqueue_struct *wq,
183
                        struct delayed_work *dwork, unsigned long delay);
190
                        struct delayed_work *dwork, unsigned long delay);
-
 
191
 
-
 
192
#define INIT_WORK(_work, _func)                 \
-
 
193
    do {                                        \
-
 
194
        INIT_LIST_HEAD(&(_work)->entry);        \
-
 
195
        (_work)->func = _func;                  \
-
 
196
    } while (0)
-
 
197
 
184
 
198
 
185
#define INIT_DELAYED_WORK(_work, _func)         \
199
#define INIT_DELAYED_WORK(_work, _func)         \
186
    do {                                        \
200
    do {                                        \
187
        INIT_LIST_HEAD(&(_work)->work.entry);   \
201
        INIT_LIST_HEAD(&(_work)->work.entry);   \
188
        (_work)->work.func = _func;             \
202
        (_work)->work.func = _func;             \
189
    } while (0)
203
    } while (0)
190
 
204
 
191
 
205
 
192
struct completion {
206
struct completion {
193
    unsigned int done;
207
    unsigned int done;
194
    wait_queue_head_t wait;
208
    wait_queue_head_t wait;
195
};
209
};
196
 
210
 
197
int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
211
int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
198
 
212
 
199
 
213
 
200
#define DEFINE_WAIT_FUNC(name, function)                                \
214
#define DEFINE_WAIT_FUNC(name, function)                                \
201
        wait_queue_t name = {                                           \
215
        wait_queue_t name = {                                           \
202
                .func           = function,                             \
216
                .func           = function,                             \
203
                .task_list      = LIST_HEAD_INIT((name).task_list),     \
217
                .task_list      = LIST_HEAD_INIT((name).task_list),     \
204
                .evnt           = CreateEvent(NULL, MANUAL_DESTROY),    \
218
                .evnt           = CreateEvent(NULL, MANUAL_DESTROY),    \
205
        }
219
        }
206
 
220
 
207
#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
221
#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
-
 
222
 
-
 
223
 
208
 
224
 
209
 
225
 
210
#endif
226
#endif