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 [sun.com URL with a
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 #ifndef __LUSTRE_UTILS_PLATFORM_H
37 #define __LUSTRE_UTILS_PLATFORM_H
41 #ifdef HAVE_LIBREADLINE
42 #define READLINE_LIBRARY
43 #include <readline/readline.h>
45 /* completion_matches() is #if 0-ed out in modern glibc */
47 #ifndef completion_matches
48 # define completion_matches rl_completion_matches
50 extern void using_history(void);
51 extern void stifle_history(int);
52 extern void add_history(char *);
53 #endif /* HAVE_LIBREADLINE */
62 typedef pthread_mutex_t l_mutex_t;
63 typedef pthread_cond_t l_cond_t;
64 #define l_mutex_init(s) pthread_mutex_init(s, NULL)
65 #define l_mutex_lock(s) pthread_mutex_lock(s)
66 #define l_mutex_unlock(s) pthread_mutex_unlock(s)
67 #define l_cond_init(c) pthread_cond_init(c, NULL)
68 #define l_cond_broadcast(c) pthread_cond_broadcast(c)
69 #define l_cond_wait(c, s) pthread_cond_wait(c, s)
74 #ifdef HAVE_LIBREADLINE
75 #define READLINE_LIBRARY
76 #include <readline/readline.h>
77 typedef VFunction rl_vintfunc_t;
78 typedef VFunction rl_voidfunc_t;
79 #endif /* HAVE_LIBREADLINE */
83 #include <sys/types.h>
87 #include <sys/semaphore.h>
90 * POSIX compliant inter-process synchronization aren't supported well
91 * in Darwin, pthread_mutex_t and pthread_cond_t can only work as
92 * inter-thread synchronization, they wouldn't work even being put in
93 * shared memory for multi-process. PTHREAD_PROCESS_SHARED is not
94 * supported by Darwin also (pthread_mutexattr_setpshared() with the
95 * PTHREAD_PROCESS_SHARED attribute will return EINVAL).
97 * The only inter-process sychronization mechanism can be used in Darwin
98 * is POSIX NAMED semaphores and file lock, here we use NAMED semaphore
99 * to implement mutex and condition.
102 * They are just proto-type now, more tests are needed.
104 #define L_LOCK_DEBUG (0)
106 #define L_SEM_NAMESIZE 32
111 char s_name[L_SEM_NAMESIZE];
115 typedef l_sem_t l_mutex_t;
123 static inline int l_sem_init(l_sem_t *sem, int val)
127 s_name = sem->s_name;
129 char buf[L_SEM_NAMESIZE];
132 /* get an unique name for named semaphore */
133 snprintf(s_name, L_SEM_NAMESIZE, "%d-%p", (int)getpid(), sem);
134 sem->s_sem = sem_open(s_name, O_CREAT, 0600, val);
135 if ((int)sem->s_sem == SEM_FAILED) {
136 fprintf(stderr, "lock %s creating fail: %d, %d!\n",
137 s_name, (int)sem->s_sem, errno);
141 printf("open lock: %s\n", s_name);
147 static inline void l_sem_done(l_sem_t *sem)
150 printf("close lock: %s.\n", sem->s_name);
152 sem_close(sem->s_sem);
155 static inline void l_sem_down(l_sem_t *sem)
158 printf("sem down :%s\n", sem->s_name);
160 sem_wait(sem->s_sem);
163 static inline void l_sem_up(l_sem_t *sem)
166 printf("sem up :%s\n", sem->s_name);
168 sem_post(sem->s_sem);
171 static inline void l_mutex_init(l_mutex_t *mutex)
173 l_sem_init((l_sem_t *)mutex, 1);
176 static inline void l_mutex_init_locked(l_mutex_t *mutex)
178 l_sem_init((l_sem_t *)mutex, 0);
181 static inline void l_mutex_done(l_mutex_t *mutex)
183 l_sem_done((l_sem_t *)mutex);
186 static inline void l_mutex_lock(l_mutex_t *mutex)
189 printf("lock mutex :%s\n", mutex->s_name);
191 sem_wait(mutex->s_sem);
194 static inline void l_mutex_unlock(l_mutex_t *mutex)
197 printf("unlock mutex: %s\n", mutex->s_name);
199 sem_post(mutex->s_sem);
202 static inline void l_cond_init(l_cond_t *cond)
204 l_mutex_init(&cond->c_guard);
205 l_sem_init(&cond->c_waiter, 0);
209 static inline void l_cond_done(l_cond_t *cond)
211 if (cond->c_count != 0)
212 fprintf(stderr, "your waiter list is not empty: %d!\n", cond->c_count);
213 l_mutex_done(&cond->c_guard);
214 l_sem_done(&cond->c_waiter);
217 static inline void l_cond_wait(l_cond_t *cond, l_mutex_t *lock)
219 l_mutex_lock(&cond->c_guard);
221 l_mutex_unlock(&cond->c_guard);
222 l_mutex_unlock(lock);
223 l_sem_down(&cond->c_waiter);
227 static inline void l_cond_broadcast(l_cond_t *cond)
229 l_mutex_lock(&cond->c_guard);
230 while (cond->c_count < 0) {
231 l_sem_up(&cond->c_waiter);
234 l_mutex_unlock(&cond->c_guard);
237 #else /* other platform */
239 #ifdef HAVE_LIBREADLINE
240 #define READLINE_LIBRARY
241 #include <readline/readline.h>
242 #endif /* HAVE_LIBREADLINE */
250 typedef pthread_mutex_t l_mutex_t;
251 typedef pthread_cond_t l_cond_t;
252 #define l_mutex_init(s) pthread_mutex_init(s, NULL)
253 #define l_mutex_lock(s) pthread_mutex_lock(s)
254 #define l_mutex_unlock(s) pthread_mutex_unlock(s)
255 #define l_cond_init(c) pthread_cond_init(c, NULL)
256 #define l_cond_broadcast(c) pthread_cond_broadcast(c)
257 #define l_cond_wait(c, s) pthread_cond_wait(c, s)
258 #endif /* HAVE_LIBPTHREAD */
260 #endif /* __linux__ */