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.
38 * libcfs/libcfs/user-prim.c
40 * Implementations of portable APIs for liblustre
42 * Author: Nikita Danilov <nikita@clusterfs.com>
46 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
51 #include <libcfs/libcfs.h>
54 * Wait queue. No-op implementation.
57 void cfs_waitq_init(struct cfs_waitq *waitq)
59 LASSERT(waitq != NULL);
63 void cfs_waitlink_init(struct cfs_waitlink *link)
65 LASSERT(link != NULL);
69 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
71 LASSERT(waitq != NULL);
72 LASSERT(link != NULL);
77 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
79 LASSERT(waitq != NULL);
80 LASSERT(link != NULL);
85 void cfs_waitq_add_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
87 cfs_waitq_add_exclusive(waitq, link);
90 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
92 LASSERT(waitq != NULL);
93 LASSERT(link != NULL);
98 int cfs_waitq_active(struct cfs_waitq *waitq)
100 LASSERT(waitq != NULL);
105 void cfs_waitq_signal(struct cfs_waitq *waitq)
107 LASSERT(waitq != NULL);
111 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
113 LASSERT(waitq != NULL);
117 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
119 LASSERT(waitq != NULL);
123 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
125 LASSERT(link != NULL);
128 /* well, wait for something to happen */
129 cfs_call_wait_handler(0);
132 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
135 LASSERT(link != NULL);
137 cfs_call_wait_handler(timeout);
141 void cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
144 /* sleep(timeout) here instead? */
145 cfs_waitq_timedwait(&l, state, timeout);
149 cfs_pause(cfs_duration_t d)
153 cfs_duration_nsec(d, &s);
157 int cfs_need_resched(void)
162 void cfs_cond_resched(void)
170 void cfs_init_timer(cfs_timer_t *t)
172 CFS_INIT_LIST_HEAD(&t->tl_list);
175 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
177 CFS_INIT_LIST_HEAD(&l->tl_list);
179 l->data = (ulong_ptr_t)arg;
183 int cfs_timer_is_armed(cfs_timer_t *l)
185 if (cfs_time_before(cfs_time_current(), l->expires))
191 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
193 l->expires = deadline;
196 void cfs_timer_disarm(cfs_timer_t *l)
199 cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
205 #ifdef HAVE_LIBPTHREAD
211 struct lustre_thread_arg {
215 static void *cfs_thread_helper(void *data)
217 struct lustre_thread_arg *targ = data;
218 cfs_thread_t f = targ->f;
219 void *arg = targ->arg;
227 int cfs_create_thread(cfs_thread_t func, void *arg, unsigned long flags)
230 pthread_attr_t tattr;
232 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
234 if ( targ_p == NULL )
240 pthread_attr_init(&tattr);
241 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
242 rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
243 pthread_attr_destroy(&tattr);
248 uid_t cfs_curproc_uid(void)
253 gid_t cfs_curproc_gid(void)
258 uid_t cfs_curproc_fsuid(void)
263 gid_t cfs_curproc_fsgid(void)
268 void cfs_enter_debugger(void)
275 void cfs_daemonize(char *str)
280 int cfs_daemonize_ctxt(char *str)
285 cfs_sigset_t cfs_block_allsigs(void)
292 rc = sigprocmask(SIG_SETMASK, &all, &old);
298 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
303 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
309 /* Block all signals except for the @sigs. It's only used in
310 * Linux kernel, just a dummy here. */
311 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs)
316 /* Return old blocked sigs */
317 rc = sigprocmask(SIG_SETMASK, NULL, &old);
323 void cfs_restore_sigs(cfs_sigset_t old)
325 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
330 int cfs_signal_pending(void)
336 rc = sigpending(&set);
341 return !memcmp(&empty, &set, sizeof(set));
344 void cfs_clear_sigpending(void)
352 * In glibc (NOT in Linux, so check above is not right), implement
353 * stack-back-tracing through backtrace() function.
355 #include <execinfo.h>
357 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
359 backtrace(trace->frame, ARRAY_SIZE(trace->frame));
362 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
364 if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
365 return trace->frame[frame_no];
372 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
374 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
382 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
384 /* No libcfs_catastrophe in userspace! */
385 libcfs_debug_msg(msgdata, "LBUG\n");
394 * c-indentation-style: "K&R"