4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/user-prim.c
38 * Implementations of portable APIs for liblustre
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
49 #include <libcfs/libcfs.h>
52 * Wait queue. No-op implementation.
55 void cfs_waitq_init(struct cfs_waitq *waitq)
57 LASSERT(waitq != NULL);
61 void cfs_waitlink_init(struct cfs_waitlink *link)
63 LASSERT(link != NULL);
67 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
69 LASSERT(waitq != NULL);
70 LASSERT(link != NULL);
75 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
77 LASSERT(waitq != NULL);
78 LASSERT(link != NULL);
83 void cfs_waitq_add_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
85 cfs_waitq_add_exclusive(waitq, link);
88 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
90 LASSERT(waitq != NULL);
91 LASSERT(link != NULL);
96 int cfs_waitq_active(struct cfs_waitq *waitq)
98 LASSERT(waitq != NULL);
103 void cfs_waitq_signal(struct cfs_waitq *waitq)
105 LASSERT(waitq != NULL);
109 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
111 LASSERT(waitq != NULL);
115 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
117 LASSERT(waitq != NULL);
121 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
123 LASSERT(link != NULL);
126 /* well, wait for something to happen */
127 cfs_call_wait_handler(0);
130 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
133 LASSERT(link != NULL);
135 cfs_call_wait_handler(timeout);
139 void cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
142 /* sleep(timeout) here instead? */
143 cfs_waitq_timedwait(&l, state, timeout);
147 cfs_pause(cfs_duration_t d)
151 cfs_duration_nsec(d, &s);
155 int cfs_need_resched(void)
160 void cfs_cond_resched(void)
168 void cfs_init_timer(cfs_timer_t *t)
170 CFS_INIT_LIST_HEAD(&t->tl_list);
173 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
175 CFS_INIT_LIST_HEAD(&l->tl_list);
177 l->data = (ulong_ptr_t)arg;
181 int cfs_timer_is_armed(cfs_timer_t *l)
183 if (cfs_time_before(cfs_time_current(), l->expires))
189 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
191 l->expires = deadline;
194 void cfs_timer_disarm(cfs_timer_t *l)
197 cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
203 #ifdef HAVE_LIBPTHREAD
209 struct lustre_thread_arg {
213 static void *cfs_thread_helper(void *data)
215 struct lustre_thread_arg *targ = data;
216 cfs_thread_t f = targ->f;
217 void *arg = targ->arg;
225 int cfs_create_thread(cfs_thread_t func, void *arg, unsigned long flags)
228 pthread_attr_t tattr;
230 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
232 if ( targ_p == NULL )
238 pthread_attr_init(&tattr);
239 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
240 rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
241 pthread_attr_destroy(&tattr);
246 uid_t cfs_curproc_uid(void)
251 gid_t cfs_curproc_gid(void)
256 uid_t cfs_curproc_fsuid(void)
261 gid_t cfs_curproc_fsgid(void)
266 void cfs_enter_debugger(void)
273 void cfs_daemonize(char *str)
278 int cfs_daemonize_ctxt(char *str)
283 cfs_sigset_t cfs_block_allsigs(void)
290 rc = sigprocmask(SIG_SETMASK, &all, &old);
296 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
301 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
307 /* Block all signals except for the @sigs. It's only used in
308 * Linux kernel, just a dummy here. */
309 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs)
314 /* Return old blocked sigs */
315 rc = sigprocmask(SIG_SETMASK, NULL, &old);
321 void cfs_restore_sigs(cfs_sigset_t old)
323 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
328 int cfs_signal_pending(void)
334 rc = sigpending(&set);
339 return !memcmp(&empty, &set, sizeof(set));
342 void cfs_clear_sigpending(void)
350 * In glibc (NOT in Linux, so check above is not right), implement
351 * stack-back-tracing through backtrace() function.
353 #include <execinfo.h>
355 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
357 backtrace(trace->frame, ARRAY_SIZE(trace->frame));
360 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
362 if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
363 return trace->frame[frame_no];
370 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
372 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
380 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
382 /* No libcfs_catastrophe in userspace! */
383 libcfs_debug_msg(msgdata, "LBUG\n");
392 * c-indentation-style: "K&R"