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 2008 Sun Microsystems, Inc. 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.
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_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
85 LASSERT(waitq != NULL);
86 LASSERT(link != NULL);
91 int cfs_waitq_active(struct cfs_waitq *waitq)
93 LASSERT(waitq != NULL);
98 void cfs_waitq_signal(struct cfs_waitq *waitq)
100 LASSERT(waitq != NULL);
104 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
106 LASSERT(waitq != NULL);
110 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
112 LASSERT(waitq != NULL);
116 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
118 LASSERT(link != NULL);
122 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
125 LASSERT(link != NULL);
130 void cfs_schedule_timeout(cfs_task_state_t state, int64_t timeout)
133 /* sleep(timeout) here instead? */
134 cfs_waitq_timedwait(&l, state, timeout);
138 cfs_pause(cfs_duration_t d)
142 cfs_duration_nsec(d, &s);
150 void cfs_init_timer(cfs_timer_t *t)
152 CFS_INIT_LIST_HEAD(&t->tl_list);
155 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
157 CFS_INIT_LIST_HEAD(&l->tl_list);
159 l->data = (unsigned long)arg;
163 #define cfs_jiffies \
165 unsigned long _ret = 0; \
167 if (gettimeofday(&tv, NULL) == 0) \
172 int cfs_timer_is_armed(cfs_timer_t *l)
174 if (cfs_time_before(cfs_jiffies, l->expires))
180 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
182 l->expires = deadline;
185 void cfs_timer_disarm(cfs_timer_t *l)
189 long cfs_timer_deadline(cfs_timer_t *l)
195 #ifdef HAVE_LIBPTHREAD
201 struct lustre_thread_arg {
205 static void *cfs_thread_helper(void *data)
207 struct lustre_thread_arg *targ = data;
208 cfs_thread_t f = targ->f;
209 void *arg = targ->arg;
216 int cfs_create_thread(cfs_thread_t func, void *arg)
219 pthread_attr_t tattr;
221 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
223 if ( targ_p == NULL )
229 pthread_attr_init(&tattr);
230 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
231 rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
232 pthread_attr_destroy(&tattr);
237 uid_t cfs_curproc_uid(void)
242 int cfs_parse_int_tunable(int *value, char *name)
244 char *env = getenv(name);
250 *value = strtoull(env, &end, 0);
254 CERROR("Can't parse tunable %s=%s\n", name, env);
259 void cfs_enter_debugger(void)
266 void cfs_daemonize(char *str)
271 int cfs_daemonize_ctxt(char *str)
276 cfs_sigset_t cfs_block_allsigs(void)
283 rc = sigprocmask(SIG_SETMASK, &all, &old);
289 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
294 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
300 void cfs_restore_sigs(cfs_sigset_t old)
302 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
307 int cfs_signal_pending(void)
313 rc = sigpending(&set);
318 return !memcmp(&empty, &set, sizeof(set));
321 void cfs_clear_sigpending(void)
329 * In glibc (NOT in Linux, so check above is not right), implement
330 * stack-back-tracing through backtrace() function.
332 #include <execinfo.h>
334 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
336 backtrace(trace->frame, ARRAY_SIZE(trace->frame));
339 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
341 if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
342 return trace->frame[frame_no];
349 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
351 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
359 void lbug_with_loc(char *file, const char *func, const int line)
361 /* No libcfs_catastrophe in userspace! */
362 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
371 * c-indentation-style: "K&R"