Whamcloud - gitweb
cd85563091d22c23a15d7e586230dc2a14b2c6b6
[fs/lustre-release.git] / libcfs / include / libcfs / util / platform.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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/libcfsutil.h
35  *
36  * A portability layer for multi-threaded userspace applications.
37  *
38  */
39
40 #ifndef __LUSTRE_UTILS_PLATFORM_H
41 #define __LUSTRE_UTILS_PLATFORM_H
42
43 #ifdef __linux__
44
45 #ifdef HAVE_LIBREADLINE
46 #define READLINE_LIBRARY
47 #include <readline/readline.h>
48
49 /* completion_matches() is #if 0-ed out in modern glibc */
50
51 #ifndef completion_matches
52 #  define completion_matches rl_completion_matches
53 #endif
54 extern void using_history(void);
55 extern void stifle_history(int);
56 extern void add_history(char *);
57 #endif /* HAVE_LIBREADLINE */
58
59 #include <errno.h>
60 #include <string.h>
61 #if HAVE_LIBPTHREAD
62 #include <sys/ipc.h>
63 #include <sys/shm.h>
64 #include <pthread.h>
65
66 typedef pthread_mutex_t l_mutex_t;
67 typedef pthread_cond_t  l_cond_t;
68 #define l_mutex_init(s)         pthread_mutex_init(s, NULL)
69 #define l_mutex_lock(s)         pthread_mutex_lock(s)
70 #define l_mutex_unlock(s)       pthread_mutex_unlock(s)
71 #define l_cond_init(c)          pthread_cond_init(c, NULL)
72 #define l_cond_broadcast(c)     pthread_cond_broadcast(c)
73 #define l_cond_wait(c, s)       pthread_cond_wait(c, s)
74 #endif
75
76 #elif __APPLE__
77
78 #ifdef HAVE_LIBREADLINE
79 #define READLINE_LIBRARY
80 #include <readline/readline.h>
81 typedef VFunction       rl_vintfunc_t;
82 typedef VFunction       rl_voidfunc_t;
83 #endif /* HAVE_LIBREADLINE */
84
85 #include <stdlib.h>
86 #include <errno.h>
87 #include <sys/types.h>
88 #include <fcntl.h>
89 #include <stdio.h>
90 #include <sys/shm.h>
91 #include <sys/semaphore.h>
92
93 /*
94  * POSIX compliant inter-process synchronization aren't supported well
95  * in Darwin, pthread_mutex_t and pthread_cond_t can only work as
96  * inter-thread synchronization, they wouldn't work even being put in
97  * shared memory for multi-process. PTHREAD_PROCESS_SHARED is not 
98  * supported by Darwin also (pthread_mutexattr_setpshared() with the 
99  * PTHREAD_PROCESS_SHARED attribute will return EINVAL). 
100  *
101  * The only inter-process sychronization mechanism can be used in Darwin
102  * is POSIX NAMED semaphores and file lock, here we use NAMED semaphore
103  * to implement mutex and condition. 
104  *
105  * XXX Liang:
106  * They are just proto-type now, more tests are needed. 
107  */
108 #define L_LOCK_DEBUG            (0)             
109
110 #define L_SEM_NAMESIZE          32
111
112 typedef struct {
113         sem_t           *s_sem;
114 #if L_LOCK_DEBUG
115         char            s_name[L_SEM_NAMESIZE];
116 #endif
117 } l_sem_t;
118
119 typedef l_sem_t         l_mutex_t;
120
121 typedef struct {
122         l_mutex_t       c_guard;
123         int             c_count;
124         l_sem_t         c_waiter;
125 } l_cond_t;
126
127 static inline int l_sem_init(l_sem_t *sem, int val)
128 {
129         char *s_name;
130 #if L_LOCK_DEBUG
131         s_name = sem->s_name;
132 #else
133         char buf[L_SEM_NAMESIZE];
134         s_name = buf;
135 #endif
136         /* get an unique name for named semaphore */
137         snprintf(s_name, L_SEM_NAMESIZE, "%d-%p", (int)getpid(), sem);
138         sem->s_sem = sem_open(s_name, O_CREAT, 0600, val);
139         if ((int)sem->s_sem == SEM_FAILED) {
140                 fprintf(stderr, "lock %s creating fail: %d, %d!\n",
141                                 s_name, (int)sem->s_sem, errno);
142                 return -1;
143         } else {
144 #if L_LOCK_DEBUG
145                 printf("open lock: %s\n", s_name);
146 #endif
147         }
148         return 0;
149 }
150
151 static inline void l_sem_done(l_sem_t *sem)
152 {
153 #if L_LOCK_DEBUG
154         printf("close lock: %s.\n", sem->s_name);
155 #endif
156         sem_close(sem->s_sem);
157 }
158
159 static inline void l_sem_down(l_sem_t *sem)
160 {
161 #if L_LOCK_DEBUG
162         printf("sem down :%s\n", sem->s_name);
163 #endif
164         sem_wait(sem->s_sem);
165 }
166
167 static inline void l_sem_up(l_sem_t *sem)
168 {
169 #if L_LOCK_DEBUG
170         printf("sem up  :%s\n", sem->s_name);
171 #endif
172         sem_post(sem->s_sem);
173 }
174
175 static inline void l_mutex_init(l_mutex_t *mutex)
176 {
177         l_sem_init((l_sem_t *)mutex, 1);
178 }
179
180 static inline void l_mutex_init_locked(l_mutex_t *mutex)
181 {
182         l_sem_init((l_sem_t *)mutex, 0);
183 }
184
185 static inline void l_mutex_done(l_mutex_t *mutex)
186 {
187         l_sem_done((l_sem_t *)mutex);
188 }
189
190 static inline void l_mutex_lock(l_mutex_t *mutex)
191 {
192 #if L_LOCK_DEBUG
193         printf("lock cfs_mutex  :%s\n", mutex->s_name);
194 #endif
195         sem_wait(mutex->s_sem);
196 }
197
198 static inline void l_mutex_unlock(l_mutex_t *mutex)
199 {
200 #if L_LOCK_DEBUG
201         printf("unlock cfs_mutex: %s\n", mutex->s_name);
202 #endif
203         sem_post(mutex->s_sem);
204 }
205
206 static inline void l_cond_init(l_cond_t *cond)
207 {
208         l_mutex_init(&cond->c_guard);
209         l_sem_init(&cond->c_waiter, 0);
210         cond->c_count = 0;
211 }
212
213 static inline void l_cond_done(l_cond_t *cond)
214 {
215         if (cond->c_count != 0)
216                 fprintf(stderr, "your waiter list is not empty: %d!\n", cond->c_count);
217         l_mutex_done(&cond->c_guard);
218         l_sem_done(&cond->c_waiter);
219 }
220
221 static inline void l_cond_wait(l_cond_t *cond, l_mutex_t *lock)
222 {
223         l_mutex_lock(&cond->c_guard);
224         cond->c_count --;
225         l_mutex_unlock(&cond->c_guard);
226         l_mutex_unlock(lock);
227         l_sem_down(&cond->c_waiter);
228         l_mutex_lock(lock);
229 }
230
231 static inline void l_cond_broadcast(l_cond_t *cond)
232 {
233         l_mutex_lock(&cond->c_guard);
234         while (cond->c_count < 0) {
235                 l_sem_up(&cond->c_waiter);
236                 cond->c_count ++;
237         }
238         l_mutex_unlock(&cond->c_guard);
239 }
240
241 #else /* other platform */
242
243 #ifdef HAVE_LIBREADLINE
244 #define READLINE_LIBRARY
245 #include <readline/readline.h>
246 #endif /* HAVE_LIBREADLINE */
247 #include <errno.h>
248 #include <string.h>
249 #if HAVE_LIBPTHREAD
250 #include <sys/ipc.h>
251 #include <sys/shm.h>
252 #include <pthread.h>
253
254 typedef pthread_mutex_t l_mutex_t;
255 typedef pthread_cond_t  l_cond_t;
256 #define l_mutex_init(s)         pthread_mutex_init(s, NULL)
257 #define l_mutex_lock(s)         pthread_mutex_lock(s)
258 #define l_mutex_unlock(s)       pthread_mutex_unlock(s)
259 #define l_cond_init(c)          pthread_cond_init(c, NULL)
260 #define l_cond_broadcast(c)     pthread_cond_broadcast(c)
261 #define l_cond_wait(c, s)       pthread_cond_wait(c, s)
262 #endif /* HAVE_LIBPTHREAD */
263
264 #endif /* __linux__  */
265
266 #endif