1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef __LUSTRE_UTILS_PLATFORM_H
23 #define __LUSTRE_UTILS_PLATFORM_H
27 #ifdef HAVE_LIBREADLINE
28 #define READLINE_LIBRARY
29 #include <readline/readline.h>
31 /* completion_matches() is #if 0-ed out in modern glibc */
33 #ifndef completion_matches
34 # define completion_matches rl_completion_matches
36 extern void using_history(void);
37 extern void stifle_history(int);
38 extern void add_history(char *);
39 #endif /* HAVE_LIBREADLINE */
48 typedef pthread_mutex_t l_mutex_t;
49 typedef pthread_cond_t l_cond_t;
50 #define l_mutex_init(s) pthread_mutex_init(s, NULL)
51 #define l_mutex_lock(s) pthread_mutex_lock(s)
52 #define l_mutex_unlock(s) pthread_mutex_unlock(s)
53 #define l_cond_init(c) pthread_cond_init(c, NULL)
54 #define l_cond_broadcast(c) pthread_cond_broadcast(c)
55 #define l_cond_wait(c, s) pthread_cond_wait(c, s)
60 #ifdef HAVE_LIBREADLINE
61 #define READLINE_LIBRARY
62 #include <readline/readline.h>
63 typedef VFunction rl_vintfunc_t;
64 typedef VFunction rl_voidfunc_t;
65 #endif /* HAVE_LIBREADLINE */
69 #include <sys/types.h>
73 #include <sys/semaphore.h>
76 * POSIX compliant inter-process synchronization aren't supported well
77 * in Darwin, pthread_mutex_t and pthread_cond_t can only work as
78 * inter-thread synchronization, they wouldn't work even being put in
79 * shared memory for multi-process. PTHREAD_PROCESS_SHARED is not
80 * supported by Darwin also (pthread_mutexattr_setpshared() with the
81 * PTHREAD_PROCESS_SHARED attribute will return EINVAL).
83 * The only inter-process sychronization mechanism can be used in Darwin
84 * is POSIX NAMED semaphores and file lock, here we use NAMED semaphore
85 * to implement mutex and condition.
88 * They are just proto-type now, more tests are needed.
90 #define L_LOCK_DEBUG (0)
92 #define L_SEM_NAMESIZE 32
97 char s_name[L_SEM_NAMESIZE];
101 typedef l_sem_t l_mutex_t;
109 static inline int l_sem_init(l_sem_t *sem, int val)
113 s_name = sem->s_name;
115 char buf[L_SEM_NAMESIZE];
118 /* get an unique name for named semaphore */
119 snprintf(s_name, L_SEM_NAMESIZE, "%d-%p", (int)getpid(), sem);
120 sem->s_sem = sem_open(s_name, O_CREAT, 0600, val);
121 if ((int)sem->s_sem == SEM_FAILED) {
122 fprintf(stderr, "lock %s creating fail: %d, %d!\n",
123 s_name, (int)sem->s_sem, errno);
127 printf("open lock: %s\n", s_name);
133 static inline void l_sem_done(l_sem_t *sem)
136 printf("close lock: %s.\n", sem->s_name);
138 sem_close(sem->s_sem);
141 static inline void l_sem_down(l_sem_t *sem)
144 printf("sem down :%s\n", sem->s_name);
146 sem_wait(sem->s_sem);
149 static inline void l_sem_up(l_sem_t *sem)
152 printf("sem up :%s\n", sem->s_name);
154 sem_post(sem->s_sem);
157 static inline void l_mutex_init(l_mutex_t *mutex)
159 l_sem_init((l_sem_t *)mutex, 1);
162 static inline void l_mutex_init_locked(l_mutex_t *mutex)
164 l_sem_init((l_sem_t *)mutex, 0);
167 static inline void l_mutex_done(l_mutex_t *mutex)
169 l_sem_done((l_sem_t *)mutex);
172 static inline void l_mutex_lock(l_mutex_t *mutex)
175 printf("lock mutex :%s\n", mutex->s_name);
177 sem_wait(mutex->s_sem);
180 static inline void l_mutex_unlock(l_mutex_t *mutex)
183 printf("unlock mutex: %s\n", mutex->s_name);
185 sem_post(mutex->s_sem);
188 static inline void l_cond_init(l_cond_t *cond)
190 l_mutex_init(&cond->c_guard);
191 l_sem_init(&cond->c_waiter, 0);
195 static inline void l_cond_done(l_cond_t *cond)
197 if (cond->c_count != 0)
198 fprintf(stderr, "your waiter list is not empty: %d!\n", cond->c_count);
199 l_mutex_done(&cond->c_guard);
200 l_sem_done(&cond->c_waiter);
203 static inline void l_cond_wait(l_cond_t *cond, l_mutex_t *lock)
205 l_mutex_lock(&cond->c_guard);
207 l_mutex_unlock(&cond->c_guard);
208 l_mutex_unlock(lock);
209 l_sem_down(&cond->c_waiter);
213 static inline void l_cond_broadcast(l_cond_t *cond)
215 l_mutex_lock(&cond->c_guard);
216 while (cond->c_count < 0) {
217 l_sem_up(&cond->c_waiter);
220 l_mutex_unlock(&cond->c_guard);
223 #else /* other platform */
225 #ifdef HAVE_LIBREADLINE
226 #define READLINE_LIBRARY
227 #include <readline/readline.h>
228 #endif /* HAVE_LIBREADLINE */
236 typedef pthread_mutex_t l_mutex_t;
237 typedef pthread_cond_t l_cond_t;
238 #define l_mutex_init(s) pthread_mutex_init(s, NULL)
239 #define l_mutex_lock(s) pthread_mutex_lock(s)
240 #define l_mutex_unlock(s) pthread_mutex_unlock(s)
241 #define l_cond_init(c) pthread_cond_init(c, NULL)
242 #define l_cond_broadcast(c) pthread_cond_broadcast(c)
243 #define l_cond_wait(c, s) pthread_cond_wait(c, s)
244 #endif /* HAVE_LIBPTHREAD */
246 #endif /* __linux__ */