Whamcloud - gitweb
LU-1346 libcfs: cleanup waitq related primitives
[fs/lustre-release.git] / libcfs / include / libcfs / user-prim.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/user-prim.h
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #ifndef __LIBCFS_USER_PRIM_H__
42 #define __LIBCFS_USER_PRIM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 /* Implementations of portable APIs for liblustre */
49
50 /*
51  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
52  */
53
54 #ifndef EXPORT_SYMBOL
55 # define EXPORT_SYMBOL(s)
56 #endif
57
58 #ifndef __KERNEL__
59
60 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
61
62 /*
63  * Just present a single processor until will add thread support.
64  */
65 #ifndef smp_processor_id
66 # define smp_processor_id() 0
67 #endif
68 #ifndef num_online_cpus
69 # define num_online_cpus() 1
70 #endif
71 #ifndef num_possible_cpus
72 # define num_possible_cpus() 1
73 #endif
74 /*
75  * Wait Queue.
76  */
77
78 typedef struct cfs_waitlink {
79         cfs_list_t sleeping;
80         void *process;
81 } wait_queue_t;
82
83 typedef struct cfs_waitq {
84         cfs_list_t sleepers;
85 } wait_queue_head_t;
86
87 #define CFS_DECL_WAITQ(wq) wait_queue_head_t wq
88 void init_waitqueue_head(struct cfs_waitq *waitq);
89 void init_waitqueue_entry_current(struct cfs_waitlink *link);
90 void add_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link);
91 void add_wait_queue_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link);
92 void add_wait_queue_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link);
93 void remove_wait_queue(struct cfs_waitq *waitq, struct cfs_waitlink *link);
94 int waitqueue_active(struct cfs_waitq *waitq);
95 void wake_up(struct cfs_waitq *waitq);
96 void wake_up_nr(struct cfs_waitq *waitq, int nr);
97 void wake_up_all(struct cfs_waitq *waitq);
98 void waitq_wait(struct cfs_waitlink *link, long state);
99 int64_t waitq_timedwait(struct cfs_waitlink *link, long state, int64_t timeout);
100 void schedule_timeout_and_set_state(long state, int64_t timeout);
101 void cfs_pause(cfs_duration_t d);
102 int need_resched(void);
103 void cond_resched(void);
104
105 /*
106  * Task states
107  */
108 #define TASK_INTERRUPTIBLE  (0)
109 #define TASK_UNINTERRUPTIBLE          (1)
110 #define TASK_RUNNING        (2)
111
112 static inline void schedule(void)                       {}
113 static inline void schedule_timeout(int64_t t)  {}
114
115 /*
116  * Lproc
117  */
118 typedef int (cfs_read_proc_t)(char *page, char **start, off_t off,
119                           int count, int *eof, void *data);
120
121 struct file; /* forward ref */
122 typedef int (cfs_write_proc_t)(struct file *file, const char *buffer,
123                                unsigned long count, void *data);
124
125 /*
126  * Signal
127  */
128 typedef sigset_t                        cfs_sigset_t;
129
130 /*
131  * Timer
132  */
133
134 typedef struct {
135         cfs_list_t tl_list;
136         void (*function)(ulong_ptr_t unused);
137         ulong_ptr_t data;
138         long expires;
139 } cfs_timer_t;
140
141
142 #define cfs_in_interrupt()    (0)
143
144 struct miscdevice{
145 };
146
147 static inline int misc_register(struct miscdevice *foo)
148 {
149         return 0;
150 }
151
152 static inline int misc_deregister(struct miscdevice *foo)
153 {
154         return 0;
155 }
156
157 #define cfs_sigfillset(l)               do {} while (0)
158 #define cfs_recalc_sigpending(l)        do {} while (0)
159 /* Fine, crash, but stop giving me compile warnings */
160 #define cfs_kthread_run(fn,d,fmt,...)   LBUG()
161
162 #define CFS_DAEMON_FLAGS                0
163
164 #define L1_CACHE_ALIGN(x)               (x)
165
166 #ifdef HAVE_LIBPTHREAD
167 typedef int (*cfs_thread_t)(void *);
168 void *kthread_run(cfs_thread_t func, void *arg, const char namefmt[], ...);
169 #else
170 /* Fine, crash, but stop giving me compile warnings */
171 #define kthread_run(f, a, n, ...) LBUG()
172 #endif
173
174 uid_t current_uid(void);
175 gid_t current_gid(void);
176 uid_t current_fsuid(void);
177 gid_t current_fsgid(void);
178
179 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
180 size_t strlcpy(char *tgt, const char *src, size_t tgt_len);
181 #endif
182
183 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
184 size_t strlcat(char *tgt, const char *src, size_t tgt_len);
185 #endif
186
187 #define LIBCFS_REALLOC(ptr, size) realloc(ptr, size)
188
189 #define cfs_online_cpus() sysconf(_SC_NPROCESSORS_ONLN)
190
191 // static inline void local_irq_save(unsigned long flag) {return;}
192 // static inline void local_irq_restore(unsigned long flag) {return;}
193
194 enum {
195         CFS_STACK_TRACE_DEPTH = 16
196 };
197
198 struct cfs_stack_trace {
199         void *frame[CFS_STACK_TRACE_DEPTH];
200 };
201
202 /*
203  * arithmetic
204  */
205 #ifndef do_div /* gcc only, platform-specific will override */
206 #define do_div(a,b)                     \
207         ({                              \
208                 unsigned long remainder;\
209                 remainder = (a) % (b);  \
210                 (a) = (a) / (b);        \
211                 (remainder);            \
212         })
213 #endif
214
215 /*
216  * Groups
217  */
218 typedef struct cfs_group_info {
219
220 } cfs_group_info_t;
221
222 #ifndef min
223 # define min(x,y) ((x)<(y) ? (x) : (y))
224 #endif
225
226 #ifndef max
227 # define max(x,y) ((x)>(y) ? (x) : (y))
228 #endif
229
230 #define cfs_get_random_bytes_prim(val, size)     (*val) = 0
231
232 /* utility libcfs init/fini entries */
233 #ifdef __WINNT__
234 extern int libcfs_arch_init(void);
235 extern void libcfs_arch_cleanup(void);
236 #else /* !__WINNT__ */
237 static inline int libcfs_arch_init(void) {
238         return 0;
239 }
240 static inline void libcfs_arch_cleanup(void) {
241 }
242 /* __WINNT__ */
243 #endif
244
245 /* proc interface wrappers for non-win OS */
246 #ifndef __WINNT__
247 #define cfs_proc_open   open
248 #define cfs_proc_mknod  mknod
249 #define cfs_proc_ioctl  ioctl
250 #define cfs_proc_close  close
251 #define cfs_proc_read   read
252 #define cfs_proc_write  write
253 #define cfs_proc_fopen  fopen
254 #define cfs_proc_fclose fclose
255 #define cfs_proc_fgets  fgets
256 /* !__WINNT__ */
257 #endif
258
259 /* !__KERNEL__ */
260 #endif
261
262 /* __LIBCFS_USER_PRIM_H__ */
263 #endif
264 /*
265  * Local variables:
266  * c-indentation-style: "K&R"
267  * c-basic-offset: 8
268  * tab-width: 8
269  * fill-column: 80
270  * scroll-step: 1
271  * End:
272  */