Whamcloud - gitweb
LU-4748 test: get one single qos_threshold_rr number
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-sync.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40
41
42 /*
43  * Wait queue routines
44  */
45
46 /*
47  * init_waitqueue_head
48  *   To initialize the wait queue
49  *
50  * Arguments:
51  *   waitq:  pointer to the wait_queue_head_t structure
52  *
53  * Return Value:
54  *   N/A
55  *
56  * Notes: 
57  *   N/A
58  */
59
60 void init_waitqueue_head(wait_queue_head_t *waitq)
61 {
62     waitq->magic = CFS_WAITQ_MAGIC;
63     waitq->flags = 0;
64     INIT_LIST_HEAD(&(waitq->waiters));
65         spin_lock_init(&(waitq->guard));
66 }
67
68 /*
69  * init_waitqueue_entry_current
70  *   To initialize the wake link node
71  *
72  * Arguments:
73  *   link:  pointer to the wait_queue_t structure
74  *
75  * Return Value:
76  *   N/A
77  *
78  * Notes: 
79  *   N/A
80  */
81
82 void init_waitqueue_entry_current(wait_queue_t *link)
83 {
84     struct task_struct * task = current;
85     PTASK_SLOT   slot = NULL;
86
87     if (!task) {
88         /* should bugchk here */
89         cfs_enter_debugger();
90         return;
91     }
92
93     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
94     cfs_assert(slot->Magic == TASKSLT_MAGIC);
95
96     memset(link, 0, sizeof(wait_queue_t));
97
98     link->magic = CFS_WAITLINK_MAGIC;
99     link->flags = 0;
100
101     link->event = &(slot->Event);
102     link->hits  = &(slot->hits);
103
104     atomic_inc(&slot->count);
105
106     INIT_LIST_HEAD(&(link->waitq[0].link));
107     INIT_LIST_HEAD(&(link->waitq[1].link));
108
109     link->waitq[0].waitl = link->waitq[1].waitl = link;
110 }
111
112
113 /*
114  * cfs_waitlink_fini
115  *   To finilize the wake link node
116  *
117  * Arguments:
118  *   link:  pointer to the wait_queue_t structure
119  *
120  * Return Value:
121  *   N/A
122  *
123  * Notes: 
124  *   N/A
125  */
126
127 void cfs_waitlink_fini(wait_queue_t *link)
128 {
129     struct task_struct * task = current;
130     PTASK_SLOT   slot = NULL;
131
132     if (!task) {
133         /* should bugchk here */
134         cfs_enter_debugger();
135         return;
136     }
137
138     slot = CONTAINING_RECORD(task, TASK_SLOT, task);
139     cfs_assert(slot->Magic == TASKSLT_MAGIC);
140     cfs_assert(link->magic == CFS_WAITLINK_MAGIC);
141     cfs_assert(link->waitq[0].waitq == NULL);
142     cfs_assert(link->waitq[1].waitq == NULL);
143
144     atomic_dec(&slot->count);
145 }
146
147
148 /*
149  * cfs_waitq_add_internal
150  *   To queue the wait link node to the wait queue
151  *
152  * Arguments:
153  *   waitq:  pointer to the wait_queue_head_t structure
154  *   link:   pointer to the wait_queue_t structure
155  *   int:    queue no (Normal or Forward waitq)
156  *
157  * Return Value:
158  *   N/A
159  *
160  * Notes: 
161  *   N/A
162  */
163
164 void cfs_waitq_add_internal(wait_queue_head_t *waitq,
165                             wait_queue_t *link,
166                             __u32 waitqid )
167
168     LASSERT(waitq != NULL);
169     LASSERT(link != NULL);
170     LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
171     LASSERT(link->magic == CFS_WAITLINK_MAGIC);
172     LASSERT(waitqid < CFS_WAITQ_CHANNELS);
173
174         spin_lock(&(waitq->guard));
175     LASSERT(link->waitq[waitqid].waitq == NULL);
176     link->waitq[waitqid].waitq = waitq;
177     if (link->flags & CFS_WAITQ_EXCLUSIVE) {
178         list_add_tail(&link->waitq[waitqid].link, &waitq->waiters);
179     } else {
180         list_add(&link->waitq[waitqid].link, &waitq->waiters);
181     }
182         spin_unlock(&(waitq->guard));
183 }
184 /*
185  * add_wait_queue
186  *   To queue the wait link node to the wait queue
187  *
188  * Arguments:
189  *   waitq:  pointer to the wait_queue_head_t structure
190  *   link:  pointer to the wait_queue_t structure
191  *
192  * Return Value:
193  *   N/A
194  *
195  * Notes: 
196  *   N/A
197  */
198
199 void add_wait_queue(wait_queue_head_t *waitq,
200                    wait_queue_t *link)
201
202     cfs_waitq_add_internal(waitq, link, CFS_WAITQ_CHAN_NORMAL);
203 }
204
205 /*
206  * add_wait_queue_exclusive
207  *   To set the wait link node to exclusive mode
208  *   and queue it to the wait queue
209  *
210  * Arguments:
211  *   waitq:  pointer to the wait_queue_head_t structure
212  *   link:  pointer to the cfs_wait_link structure
213  *
214  * Return Value:
215  *   N/A
216  *
217  * Notes: 
218  *   N/A
219  */
220
221 void add_wait_queue_exclusive( wait_queue_head_t *waitq,
222                               wait_queue_t *link)
223 {
224     LASSERT(waitq != NULL);
225     LASSERT(link != NULL);
226     LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
227     LASSERT(link->magic == CFS_WAITLINK_MAGIC);
228
229         link->flags |= CFS_WAITQ_EXCLUSIVE;
230     add_wait_queue(waitq, link);
231 }
232
233 /*
234  * remove_wait_queue
235  *   To remove the wait link node from the waitq
236  *
237  * Arguments:
238  *   waitq:  pointer to the cfs_ waitq_t structure
239  *   link:  pointer to the wait_queue_t structure
240  *
241  * Return Value:
242  *   N/A
243  *
244  * Notes: 
245  *   N/A
246  */
247
248 void remove_wait_queue( wait_queue_head_t *waitq,
249                     wait_queue_t *link)
250 {
251     int i = 0;
252
253     LASSERT(waitq != NULL);
254     LASSERT(link != NULL);
255
256     LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
257     LASSERT(link->magic == CFS_WAITLINK_MAGIC);
258
259         spin_lock(&(waitq->guard));
260
261     for (i=0; i < CFS_WAITQ_CHANNELS; i++) {
262         if (link->waitq[i].waitq == waitq)
263             break;
264     }
265
266     if (i < CFS_WAITQ_CHANNELS) {
267         link->waitq[i].waitq = NULL;
268         list_del_init(&link->waitq[i].link);
269     } else {
270         cfs_enter_debugger();
271     }
272
273         spin_unlock(&(waitq->guard));
274 }
275
276 /*
277  * waitqueue_active
278  *   Is the waitq active (not empty) ?
279  *
280  * Arguments:
281  *   waitq:  pointer to the cfs_ waitq_t structure
282  *
283  * Return Value:
284  *   Zero: the waitq is empty
285  *   Non-Zero: the waitq is active
286  *
287  * Notes: 
288  *   We always returns TRUE here, the same to Darwin.
289  */
290
291 int waitqueue_active(wait_queue_head_t *waitq)
292 {
293     LASSERT(waitq != NULL);
294     LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
295
296         return (1);
297 }
298
299 /*
300  * wake_up_nr
301  *   To wake up all the non-exclusive tasks plus nr exclusive
302  *   ones in the waitq
303  *
304  * Arguments:
305  *   waitq:  pointer to the wait_queue_head_t structure
306  *   nr:    number of exclusive tasks to be woken up
307  *
308  * Return Value:
309  *   N/A
310  *
311  * Notes: 
312  *   N/A
313  */
314
315
316 void wake_up_nr(wait_queue_head_t *waitq, int nr)
317 {
318     int     result;
319     cfs_waitlink_channel_t * scan;
320
321     LASSERT(waitq != NULL);
322     LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
323
324         spin_lock(&waitq->guard);
325     list_for_each_entry(scan, &waitq->waiters,
326                             link) {
327
328         wait_queue_t *waitl = scan->waitl;
329
330         result = cfs_wake_event(waitl->event);
331         LASSERT( result == FALSE || result == TRUE );
332
333         if (result) {
334             atomic_inc(waitl->hits);
335         }
336
337         if ((waitl->flags & CFS_WAITQ_EXCLUSIVE) && --nr == 0)
338             break;
339     }
340
341         spin_unlock(&waitq->guard);
342         return;
343 }
344
345 /*
346  * wake_up
347  *   To wake up all the non-exclusive tasks and 1 exclusive
348  *
349  * Arguments:
350  *   waitq:  pointer to the wait_queue_head_t structure
351  *
352  * Return Value:
353  *   N/A
354  *
355  * Notes: 
356  *   N/A
357  */
358
359 void wake_up(wait_queue_head_t *waitq)
360 {
361     wake_up_nr(waitq, 1);
362 }
363
364
365 /*
366  * wake_up_all
367  *   To wake up all the tasks in the waitq
368  *
369  * Arguments:
370  *   waitq:  pointer to the wait_queue_head_t structure
371  *
372  * Return Value:
373  *   N/A
374  *
375  * Notes: 
376  *   N/A
377  */
378
379 void wake_up_all(wait_queue_head_t *waitq)
380 {
381     LASSERT(waitq != NULL);
382     LASSERT(waitq->magic ==CFS_WAITQ_MAGIC);
383
384         wake_up_nr(waitq, 0);
385 }
386
387 /*
388  * waitq_wait
389  *   To wait on the link node until it is signaled.
390  *
391  * Arguments:
392  *   link:  pointer to the wait_queue_t structure
393  *
394  * Return Value:
395  *   N/A
396  *
397  * Notes: 
398  *   N/A
399  */
400
401 void waitq_wait(wait_queue_t *link, long state)
402
403     LASSERT(link != NULL);
404     LASSERT(link->magic == CFS_WAITLINK_MAGIC);
405
406     if (atomic_read(link->hits) > 0) {
407         atomic_dec(link->hits);
408         LASSERT((__u32)atomic_read(link->hits) < (__u32)0xFFFFFF00);
409     } else {
410         cfs_wait_event_internal(link->event, 0);
411     }
412 }
413
414 /*
415  * waitq_timedwait
416  *   To wait the link node to be signaled with a timeout limit
417  *
418  * Arguments:
419  *   link:   pointer to the wait_queue_t structure
420  *   timeout: the timeout limitation
421  *
422  * Return Value:
423  *   Woken up: return the difference of the current time and
424  *             the timeout
425  *   Timeout:  return 0
426  *
427  * Notes: 
428  *   What if it happens to be woken up at the just timeout time !?
429  */
430
431 int64_t waitq_timedwait( wait_queue_t *link,
432                              long state,
433                              int64_t timeout)
434
435
436     if (atomic_read(link->hits) > 0) {
437         atomic_dec(link->hits);
438         LASSERT((__u32)atomic_read(link->hits) < (__u32)0xFFFFFF00);
439         return (int64_t)TRUE;
440     }
441
442     return (int64_t)cfs_wait_event_internal(link->event, timeout);
443 }