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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/include/libcfs/libcfsutil.h
36 * A portability layer for multi-threaded userspace applications.
40 #ifndef __LUSTRE_UTILS_PLATFORM_H
41 #define __LUSTRE_UTILS_PLATFORM_H
45 #ifdef HAVE_LIBREADLINE
46 #define READLINE_LIBRARY
47 #include <readline/readline.h>
49 /* completion_matches() is #if 0-ed out in modern glibc */
51 #ifndef completion_matches
52 # define completion_matches rl_completion_matches
54 extern void using_history(void);
55 extern void stifle_history(int);
56 extern void add_history(char *);
57 #endif /* HAVE_LIBREADLINE */
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)
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 */
87 #include <sys/types.h>
91 #include <sys/semaphore.h>
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).
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.
106 * They are just proto-type now, more tests are needed.
108 #define L_LOCK_DEBUG (0)
110 #define L_SEM_NAMESIZE 32
115 char s_name[L_SEM_NAMESIZE];
119 typedef l_sem_t l_mutex_t;
127 static inline int l_sem_init(l_sem_t *sem, int val)
131 s_name = sem->s_name;
133 char buf[L_SEM_NAMESIZE];
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);
145 printf("open lock: %s\n", s_name);
151 static inline void l_sem_done(l_sem_t *sem)
154 printf("close lock: %s.\n", sem->s_name);
156 sem_close(sem->s_sem);
159 static inline void l_sem_down(l_sem_t *sem)
162 printf("sem down :%s\n", sem->s_name);
164 sem_wait(sem->s_sem);
167 static inline void l_sem_up(l_sem_t *sem)
170 printf("sem up :%s\n", sem->s_name);
172 sem_post(sem->s_sem);
175 static inline void l_mutex_init(l_mutex_t *mutex)
177 l_sem_init((l_sem_t *)mutex, 1);
180 static inline void l_mutex_init_locked(l_mutex_t *mutex)
182 l_sem_init((l_sem_t *)mutex, 0);
185 static inline void l_mutex_done(l_mutex_t *mutex)
187 l_sem_done((l_sem_t *)mutex);
190 static inline void l_mutex_lock(l_mutex_t *mutex)
193 printf("lock cfs_mutex :%s\n", mutex->s_name);
195 sem_wait(mutex->s_sem);
198 static inline void l_mutex_unlock(l_mutex_t *mutex)
201 printf("unlock cfs_mutex: %s\n", mutex->s_name);
203 sem_post(mutex->s_sem);
206 static inline void l_cond_init(l_cond_t *cond)
208 l_mutex_init(&cond->c_guard);
209 l_sem_init(&cond->c_waiter, 0);
213 static inline void l_cond_done(l_cond_t *cond)
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);
221 static inline void l_cond_wait(l_cond_t *cond, l_mutex_t *lock)
223 l_mutex_lock(&cond->c_guard);
225 l_mutex_unlock(&cond->c_guard);
226 l_mutex_unlock(lock);
227 l_sem_down(&cond->c_waiter);
231 static inline void l_cond_broadcast(l_cond_t *cond)
233 l_mutex_lock(&cond->c_guard);
234 while (cond->c_count < 0) {
235 l_sem_up(&cond->c_waiter);
238 l_mutex_unlock(&cond->c_guard);
241 #else /* other platform */
243 #ifdef HAVE_LIBREADLINE
244 #define READLINE_LIBRARY
245 #include <readline/readline.h>
246 #endif /* HAVE_LIBREADLINE */
256 typedef pthread_mutex_t l_mutex_t;
257 typedef pthread_cond_t l_cond_t;
258 #define l_mutex_init(s) pthread_mutex_init(s, NULL)
259 #define l_mutex_lock(s) pthread_mutex_lock(s)
260 #define l_mutex_unlock(s) pthread_mutex_unlock(s)
261 #define l_cond_init(c) pthread_cond_init(c, NULL)
262 #define l_cond_broadcast(c) pthread_cond_broadcast(c)
263 #define l_cond_wait(c, s) pthread_cond_wait(c, s)
264 #endif /* HAVE_LIBPTHREAD */
266 #endif /* __linux__ */