1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:
4 * Copyright (c) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or modify it under
9 * the terms of version 2 of the GNU General Public License as published by
10 * the Free Software Foundation. Lustre is distributed in the hope that it
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. You should have received a
14 * copy of the GNU General Public License along with Lustre; if not, write
15 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
19 #define DEBUG_SUBSYSTEM S_LIBCFS
21 #include <libcfs/libcfs.h>
30 * To initialize the wait queue
33 * waitq: pointer to the cfs_waitq_t structure
42 void cfs_waitq_init(cfs_waitq_t *waitq)
44 waitq->magic = CFS_WAITQ_MAGIC;
46 INIT_LIST_HEAD(&(waitq->waiters));
47 spin_lock_init(&(waitq->guard));
52 * To initialize the wake link node
55 * link: pointer to the cfs_waitlink_t structure
64 void cfs_waitlink_init(cfs_waitlink_t *link)
66 cfs_task_t * task = cfs_current();
67 PTASK_SLOT slot = NULL;
70 /* should bugchk here */
75 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
76 cfs_assert(slot->Magic == TASKSLT_MAGIC);
78 memset(link, 0, sizeof(cfs_waitlink_t));
80 link->magic = CFS_WAITLINK_MAGIC;
83 link->event = &(slot->Event);
84 link->hits = &(slot->hits);
86 atomic_inc(&slot->count);
88 INIT_LIST_HEAD(&(link->waitq[0].link));
89 INIT_LIST_HEAD(&(link->waitq[1].link));
91 link->waitq[0].waitl = link->waitq[1].waitl = link;
97 * To finilize the wake link node
100 * link: pointer to the cfs_waitlink_t structure
109 void cfs_waitlink_fini(cfs_waitlink_t *link)
111 cfs_task_t * task = cfs_current();
112 PTASK_SLOT slot = NULL;
115 /* should bugchk here */
116 cfs_enter_debugger();
120 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
121 cfs_assert(slot->Magic == TASKSLT_MAGIC);
122 cfs_assert(link->magic == CFS_WAITLINK_MAGIC);
123 cfs_assert(link->waitq[0].waitq == NULL);
124 cfs_assert(link->waitq[1].waitq == NULL);
126 atomic_dec(&slot->count);
131 * cfs_waitq_add_internal
132 * To queue the wait link node to the wait queue
135 * waitq: pointer to the cfs_waitq_t structure
136 * link: pointer to the cfs_waitlink_t structure
137 * int: queue no (Normal or Forward waitq)
146 void cfs_waitq_add_internal(cfs_waitq_t *waitq,
147 cfs_waitlink_t *link,
150 LASSERT(waitq != NULL);
151 LASSERT(link != NULL);
152 LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
153 LASSERT(link->magic == CFS_WAITLINK_MAGIC);
154 LASSERT(waitqid < CFS_WAITQ_CHANNELS);
156 spin_lock(&(waitq->guard));
157 LASSERT(link->waitq[waitqid].waitq == NULL);
158 link->waitq[waitqid].waitq = waitq;
159 if (link->flags & CFS_WAITQ_EXCLUSIVE) {
160 list_add_tail(&link->waitq[waitqid].link, &waitq->waiters);
162 list_add(&link->waitq[waitqid].link, &waitq->waiters);
164 spin_unlock(&(waitq->guard));
168 * To queue the wait link node to the wait queue
171 * waitq: pointer to the cfs_waitq_t structure
172 * link: pointer to the cfs_waitlink_t structure
181 void cfs_waitq_add(cfs_waitq_t *waitq,
182 cfs_waitlink_t *link)
184 cfs_waitq_add_internal(waitq, link, CFS_WAITQ_CHAN_NORMAL);
188 * cfs_waitq_add_exclusive
189 * To set the wait link node to exclusive mode
190 * and queue it to the wait queue
193 * waitq: pointer to the cfs_waitq_t structure
194 * link: pointer to the cfs_wait_link structure
203 void cfs_waitq_add_exclusive( cfs_waitq_t *waitq,
204 cfs_waitlink_t *link)
206 LASSERT(waitq != NULL);
207 LASSERT(link != NULL);
208 LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
209 LASSERT(link->magic == CFS_WAITLINK_MAGIC);
211 link->flags |= CFS_WAITQ_EXCLUSIVE;
212 cfs_waitq_add(waitq, link);
217 * To be determinated.
220 * waitq: pointer to the cfs_waitq_t structure
221 * link: pointer to the cfs_waitlink_t structure
230 void cfs_waitq_forward( cfs_waitlink_t *link,
233 cfs_waitq_add_internal(waitq, link, CFS_WAITQ_CHAN_FORWARD);
238 * To remove the wait link node from the waitq
241 * waitq: pointer to the cfs_ waitq_t structure
242 * link: pointer to the cfs_waitlink_t structure
251 void cfs_waitq_del( cfs_waitq_t *waitq,
252 cfs_waitlink_t *link)
256 LASSERT(waitq != NULL);
257 LASSERT(link != NULL);
259 LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
260 LASSERT(link->magic == CFS_WAITLINK_MAGIC);
262 spin_lock(&(waitq->guard));
264 for (i=0; i < CFS_WAITQ_CHANNELS; i++) {
265 if (link->waitq[i].waitq == waitq)
269 if (i < CFS_WAITQ_CHANNELS) {
270 link->waitq[i].waitq = NULL;
271 list_del_init(&link->waitq[i].link);
273 cfs_enter_debugger();
276 spin_unlock(&(waitq->guard));
281 * Is the waitq active (not empty) ?
284 * waitq: pointer to the cfs_ waitq_t structure
287 * Zero: the waitq is empty
288 * Non-Zero: the waitq is active
291 * We always returns TRUE here, the same to Darwin.
294 int cfs_waitq_active(cfs_waitq_t *waitq)
296 LASSERT(waitq != NULL);
297 LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
303 * cfs_waitq_signal_nr
304 * To wake up all the non-exclusive tasks plus nr exclusive
308 * waitq: pointer to the cfs_waitq_t structure
309 * nr: number of exclusive tasks to be woken up
319 void cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
322 cfs_waitlink_channel_t * scan;
324 LASSERT(waitq != NULL);
325 LASSERT(waitq->magic == CFS_WAITQ_MAGIC);
327 spin_lock(&waitq->guard);
329 list_for_each_entry(scan, &waitq->waiters, cfs_waitlink_channel_t, link) {
331 cfs_waitlink_t *waitl = scan->waitl;
333 result = cfs_wake_event(waitl->event);
334 LASSERT( result == FALSE || result == TRUE );
337 atomic_inc(waitl->hits);
340 if ((waitl->flags & CFS_WAITQ_EXCLUSIVE) && --nr == 0)
344 spin_unlock(&waitq->guard);
350 * To wake up all the non-exclusive tasks and 1 exclusive
353 * waitq: pointer to the cfs_waitq_t structure
362 void cfs_waitq_signal(cfs_waitq_t *waitq)
364 cfs_waitq_signal_nr(waitq, 1);
369 * cfs_waitq_broadcast
370 * To wake up all the tasks in the waitq
373 * waitq: pointer to the cfs_waitq_t structure
382 void cfs_waitq_broadcast(cfs_waitq_t *waitq)
384 LASSERT(waitq != NULL);
385 LASSERT(waitq->magic ==CFS_WAITQ_MAGIC);
387 cfs_waitq_signal_nr(waitq, 0);
392 * To wait on the link node until it is signaled.
395 * link: pointer to the cfs_waitlink_t structure
404 void cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
406 LASSERT(link != NULL);
407 LASSERT(link->magic == CFS_WAITLINK_MAGIC);
409 if (atomic_read(link->hits) > 0) {
410 atomic_dec(link->hits);
411 LASSERT((__u32)atomic_read(link->hits) < (__u32)0xFFFFFF00);
413 cfs_wait_event(link->event, 0);
418 * cfs_waitq_timedwait
419 * To wait the link node to be signaled with a timeout limit
422 * link: pointer to the cfs_waitlink_t structure
423 * timeout: the timeout limitation
426 * Woken up: return the difference of the current time and
431 * What if it happens to be woken up at the just timeout time !?
434 cfs_duration_t cfs_waitq_timedwait( cfs_waitlink_t *link,
435 cfs_task_state_t state,
436 cfs_duration_t timeout)
439 if (atomic_read(link->hits) > 0) {
440 atomic_dec(link->hits);
441 LASSERT((__u32)atomic_read(link->hits) < (__u32)0xFFFFFF00);
445 return (cfs_duration_t)cfs_wait_event(link->event, timeout);