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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_LNET
40 #ifndef AUTOCONF_INCLUDED
41 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/fs_struct.h>
46 #include <linux/sched.h>
48 #include <libcfs/libcfs.h>
50 #if defined(CONFIG_KGDB)
54 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
55 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
58 cfs_waitq_init(cfs_waitq_t *waitq)
60 init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
62 EXPORT_SYMBOL(cfs_waitq_init);
65 cfs_waitlink_init(cfs_waitlink_t *link)
67 init_waitqueue_entry(LINUX_WAITQ(link), current);
69 EXPORT_SYMBOL(cfs_waitlink_init);
72 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
74 add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
76 EXPORT_SYMBOL(cfs_waitq_add);
78 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
80 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
83 wait->flags |= WQ_FLAG_EXCLUSIVE;
84 __add_wait_queue(q, wait);
87 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
90 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
93 add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
95 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
98 * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
99 * waiting threads, which is not always desirable because all threads will
100 * be waken up again and again, even user only needs a few of them to be
101 * active most time. This is not good for performance because cache can
102 * be polluted by different threads.
104 * LIFO list can resolve this problem because we always wakeup the most
105 * recent active thread by default.
107 * NB: please don't call non-exclusive & exclusive wait on the same
108 * waitq if cfs_waitq_add_exclusive_head is used.
111 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
115 spin_lock_irqsave(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
116 __add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
117 spin_unlock_irqrestore(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
119 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
122 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
124 remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
126 EXPORT_SYMBOL(cfs_waitq_del);
129 cfs_waitq_active(cfs_waitq_t *waitq)
131 return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
133 EXPORT_SYMBOL(cfs_waitq_active);
136 cfs_waitq_signal(cfs_waitq_t *waitq)
138 wake_up(LINUX_WAITQ_HEAD(waitq));
140 EXPORT_SYMBOL(cfs_waitq_signal);
143 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
145 wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
147 EXPORT_SYMBOL(cfs_waitq_signal_nr);
150 cfs_waitq_broadcast(cfs_waitq_t *waitq)
152 wake_up_all(LINUX_WAITQ_HEAD(waitq));
154 EXPORT_SYMBOL(cfs_waitq_broadcast);
157 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
161 EXPORT_SYMBOL(cfs_waitq_wait);
164 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
167 return schedule_timeout(timeout);
169 EXPORT_SYMBOL(cfs_waitq_timedwait);
172 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
174 set_current_state(state);
175 schedule_timeout(timeout);
177 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
180 cfs_schedule_timeout(int64_t timeout)
182 schedule_timeout(timeout);
184 EXPORT_SYMBOL(cfs_schedule_timeout);
191 EXPORT_SYMBOL(cfs_schedule);
193 /* deschedule for a bit... */
195 cfs_pause(cfs_duration_t ticks)
197 set_current_state(TASK_UNINTERRUPTIBLE);
198 schedule_timeout(ticks);
200 EXPORT_SYMBOL(cfs_pause);
202 int cfs_need_resched(void)
204 return need_resched();
206 EXPORT_SYMBOL(cfs_need_resched);
208 void cfs_cond_resched(void)
212 EXPORT_SYMBOL(cfs_cond_resched);
214 void cfs_init_timer(cfs_timer_t *t)
218 EXPORT_SYMBOL(cfs_init_timer);
220 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
224 t->data = (unsigned long)arg;
226 EXPORT_SYMBOL(cfs_timer_init);
228 void cfs_timer_done(cfs_timer_t *t)
232 EXPORT_SYMBOL(cfs_timer_done);
234 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
236 mod_timer(t, deadline);
238 EXPORT_SYMBOL(cfs_timer_arm);
240 void cfs_timer_disarm(cfs_timer_t *t)
244 EXPORT_SYMBOL(cfs_timer_disarm);
246 int cfs_timer_is_armed(cfs_timer_t *t)
248 return timer_pending(t);
250 EXPORT_SYMBOL(cfs_timer_is_armed);
252 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
256 EXPORT_SYMBOL(cfs_timer_deadline);
258 void cfs_enter_debugger(void)
260 #if defined(CONFIG_KGDB)
262 #elif defined(__arch_um__)
269 void cfs_daemonize(char *str) {
274 SIGNAL_MASK_LOCK(current, flags);
275 sigfillset(¤t->blocked);
277 SIGNAL_MASK_UNLOCK(current, flags);
281 int cfs_daemonize_ctxt(char *str) {
284 #ifndef HAVE_UNSHARE_FS_STRUCT
286 struct task_struct *tsk = current;
287 struct fs_struct *fs = NULL;
288 fs = copy_fs_struct(tsk->fs);
301 cfs_block_allsigs(void)
306 SIGNAL_MASK_LOCK(current, flags);
307 old = current->blocked;
308 sigfillset(¤t->blocked);
310 SIGNAL_MASK_UNLOCK(current, flags);
316 cfs_block_sigs(sigset_t bits)
321 SIGNAL_MASK_LOCK(current, flags);
322 old = current->blocked;
323 current->blocked = bits;
325 SIGNAL_MASK_UNLOCK(current, flags);
329 /* Block all signals except for the @sigs */
331 cfs_block_sigsinv(unsigned long sigs)
336 SIGNAL_MASK_LOCK(current, flags);
337 old = current->blocked;
338 siginitsetinv(¤t->blocked, sigs);
340 SIGNAL_MASK_UNLOCK(current, flags);
346 cfs_restore_sigs (cfs_sigset_t old)
350 SIGNAL_MASK_LOCK(current, flags);
351 current->blocked = old;
353 SIGNAL_MASK_UNLOCK(current, flags);
357 cfs_signal_pending(void)
359 return signal_pending(current);
363 cfs_clear_sigpending(void)
367 SIGNAL_MASK_LOCK(current, flags);
369 SIGNAL_MASK_UNLOCK(current, flags);
373 libcfs_arch_init(void)
379 libcfs_arch_cleanup(void)
384 EXPORT_SYMBOL(libcfs_arch_init);
385 EXPORT_SYMBOL(libcfs_arch_cleanup);
386 EXPORT_SYMBOL(cfs_enter_debugger);
387 EXPORT_SYMBOL(cfs_daemonize);
388 EXPORT_SYMBOL(cfs_daemonize_ctxt);
389 EXPORT_SYMBOL(cfs_block_allsigs);
390 EXPORT_SYMBOL(cfs_block_sigs);
391 EXPORT_SYMBOL(cfs_block_sigsinv);
392 EXPORT_SYMBOL(cfs_restore_sigs);
393 EXPORT_SYMBOL(cfs_signal_pending);
394 EXPORT_SYMBOL(cfs_clear_sigpending);