1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #ifndef AUTOCONF_INCLUDED
39 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/fs_struct.h>
44 #include <linux/sched.h>
46 #include <libcfs/libcfs.h>
48 #if defined(CONFIG_KGDB)
52 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
53 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
56 cfs_waitq_init(cfs_waitq_t *waitq)
58 init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
60 EXPORT_SYMBOL(cfs_waitq_init);
63 cfs_waitlink_init(cfs_waitlink_t *link)
65 init_waitqueue_entry(LINUX_WAITQ(link), current);
67 EXPORT_SYMBOL(cfs_waitlink_init);
70 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
72 add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
74 EXPORT_SYMBOL(cfs_waitq_add);
76 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
78 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
81 wait->flags |= WQ_FLAG_EXCLUSIVE;
82 __add_wait_queue(q, wait);
85 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
88 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
91 add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
93 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
96 * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
97 * waiting threads, which is not always desirable because all threads will
98 * be waken up again and again, even user only needs a few of them to be
99 * active most time. This is not good for performance because cache can
100 * be polluted by different threads.
102 * LIFO list can resolve this problem because we always wakeup the most
103 * recent active thread by default.
105 * NB: please don't call non-exclusive & exclusive wait on the same
106 * waitq if cfs_waitq_add_exclusive_head is used.
109 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
113 spin_lock_irqsave(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
114 __add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
115 spin_unlock_irqrestore(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
117 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
120 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
122 remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
124 EXPORT_SYMBOL(cfs_waitq_del);
127 cfs_waitq_active(cfs_waitq_t *waitq)
129 return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
131 EXPORT_SYMBOL(cfs_waitq_active);
134 cfs_waitq_signal(cfs_waitq_t *waitq)
136 wake_up(LINUX_WAITQ_HEAD(waitq));
138 EXPORT_SYMBOL(cfs_waitq_signal);
141 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
143 wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
145 EXPORT_SYMBOL(cfs_waitq_signal_nr);
148 cfs_waitq_broadcast(cfs_waitq_t *waitq)
150 wake_up_all(LINUX_WAITQ_HEAD(waitq));
152 EXPORT_SYMBOL(cfs_waitq_broadcast);
155 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
159 EXPORT_SYMBOL(cfs_waitq_wait);
162 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
165 return schedule_timeout(timeout);
167 EXPORT_SYMBOL(cfs_waitq_timedwait);
170 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
172 set_current_state(state);
173 schedule_timeout(timeout);
175 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
178 cfs_schedule_timeout(int64_t timeout)
180 schedule_timeout(timeout);
182 EXPORT_SYMBOL(cfs_schedule_timeout);
189 EXPORT_SYMBOL(cfs_schedule);
191 /* deschedule for a bit... */
193 cfs_pause(cfs_duration_t ticks)
195 set_current_state(TASK_UNINTERRUPTIBLE);
196 schedule_timeout(ticks);
198 EXPORT_SYMBOL(cfs_pause);
200 int cfs_need_resched(void)
202 return need_resched();
204 EXPORT_SYMBOL(cfs_need_resched);
206 void cfs_cond_resched(void)
210 EXPORT_SYMBOL(cfs_cond_resched);
212 void cfs_init_timer(cfs_timer_t *t)
216 EXPORT_SYMBOL(cfs_init_timer);
218 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
222 t->data = (unsigned long)arg;
224 EXPORT_SYMBOL(cfs_timer_init);
226 void cfs_timer_done(cfs_timer_t *t)
230 EXPORT_SYMBOL(cfs_timer_done);
232 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
234 mod_timer(t, deadline);
236 EXPORT_SYMBOL(cfs_timer_arm);
238 void cfs_timer_disarm(cfs_timer_t *t)
242 EXPORT_SYMBOL(cfs_timer_disarm);
244 int cfs_timer_is_armed(cfs_timer_t *t)
246 return timer_pending(t);
248 EXPORT_SYMBOL(cfs_timer_is_armed);
250 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
254 EXPORT_SYMBOL(cfs_timer_deadline);
256 void cfs_enter_debugger(void)
258 #if defined(CONFIG_KGDB)
260 #elif defined(__arch_um__)
267 void cfs_daemonize(char *str) {
272 SIGNAL_MASK_LOCK(current, flags);
273 sigfillset(¤t->blocked);
275 SIGNAL_MASK_UNLOCK(current, flags);
279 int cfs_daemonize_ctxt(char *str) {
282 #ifndef HAVE_UNSHARE_FS_STRUCT
284 struct task_struct *tsk = current;
285 struct fs_struct *fs = NULL;
286 fs = copy_fs_struct(tsk->fs);
299 cfs_block_allsigs(void)
304 SIGNAL_MASK_LOCK(current, flags);
305 old = current->blocked;
306 sigfillset(¤t->blocked);
308 SIGNAL_MASK_UNLOCK(current, flags);
314 cfs_block_sigs(sigset_t bits)
319 SIGNAL_MASK_LOCK(current, flags);
320 old = current->blocked;
321 current->blocked = bits;
323 SIGNAL_MASK_UNLOCK(current, flags);
328 cfs_restore_sigs (cfs_sigset_t old)
332 SIGNAL_MASK_LOCK(current, flags);
333 current->blocked = old;
335 SIGNAL_MASK_UNLOCK(current, flags);
339 cfs_signal_pending(void)
341 return signal_pending(current);
345 cfs_clear_sigpending(void)
349 SIGNAL_MASK_LOCK(current, flags);
351 SIGNAL_MASK_UNLOCK(current, flags);
355 libcfs_arch_init(void)
361 libcfs_arch_cleanup(void)
366 EXPORT_SYMBOL(libcfs_arch_init);
367 EXPORT_SYMBOL(libcfs_arch_cleanup);
368 EXPORT_SYMBOL(cfs_enter_debugger);
369 EXPORT_SYMBOL(cfs_daemonize);
370 EXPORT_SYMBOL(cfs_daemonize_ctxt);
371 EXPORT_SYMBOL(cfs_block_allsigs);
372 EXPORT_SYMBOL(cfs_block_sigs);
373 EXPORT_SYMBOL(cfs_restore_sigs);
374 EXPORT_SYMBOL(cfs_signal_pending);
375 EXPORT_SYMBOL(cfs_clear_sigpending);