4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
35 * PURPOSE: Operating System Dependent layer
37 * Provide OS dependent data structures & functions with
38 * a canonical DAPL interface. Designed to be portable
39 * and hide OS specific quirks of common functions.
41 * $Id: dapl_osd.h,v 1.38 2003/08/20 14:08:57 sjs2 Exp $
52 * <assert.h> keys off of NDEBUG
64 #include <semaphore.h>
73 #include "dapl_debug.h"
76 * networking related headers
80 #include <sys/types.h>
81 #include <sys/socket.h>
83 #include <arpa/inet.h>
90 * initialization function
92 void dapl_os_init(void);
94 #define dapl_os_panic(args) \
96 fprintf(stderr, "PANIC in %s:%i:\n", __FILE__, __LINE__); \
97 fprintf(stderr, args); \
101 int dapl_os_get_env_bool(
104 int dapl_os_get_env_val(
111 typedef volatile DAT_COUNT DAPL_ATOMIC
;
116 * get the current value of '*v', and then increment it.
118 * This is equivalent to an IB atomic fetch and add of 1,
119 * except that a DAT_COUNT might be 32 bits, rather than 64
120 * and it occurs in local memory.
122 * DAT_COUNT dapl_os_atomic_inc(INOUT DAPL_ATOMIC *v)
124 #define dapl_os_atomic_inc(v) ((DAT_COUNT) \
125 (atomic_add_32_nv((uint32_t *)(v), 1) - 1))
130 * decrement the current value of '*v'. No return value is required.
132 * DAT_COUNT dapl_os_atomic_dec(INOUT DAPL_ATOMIC *v)
134 #define dapl_os_atomic_dec(v) assert(*v != 0); \
136 (atomic_add_32_nv((uint32_t *)(v), -1) + 1))
139 * dapl_os_atomic_assign
141 * assign 'new_value' to '*v' if the current value
142 * matches the provided 'match_value'.
144 * Make no assignment if there is no match.
146 * Return the current value in any case.
148 * This matches the IBTA atomic operation compare & swap
149 * except that it is for local memory and a DAT_COUNT may
150 * be only 32 bits, rather than 64.
152 * DAT_COUNT dapl_os_atomic_assign(INOUT DAPL_ATOMIC *v,
153 * IN DAT_COUNT match_value, IN DAT_COUNT new_value)
155 #define dapl_os_atomic_assign(v, match_value, new_value) \
156 atomic_cas_32((uint32_t *)(v), (uint32_t)(match_value), \
157 (uint32_t)(new_value))
162 typedef pthread_t DAPL_OS_THREAD
;
165 dapl_os_thread_create(
166 IN
void (*func
) (void *),
168 OUT DAPL_OS_THREAD
*thread_id
);
175 typedef pthread_mutex_t DAPL_OS_LOCK
;
178 * DAT_RETURN dapl_os_lock_init(IN DAPL_OS_LOCK *m)
180 #define dapl_os_lock_init(m) (void) \
181 ((0 == pthread_mutex_init((m), NULL)) ? \
183 (DAT_CLASS_ERROR | DAT_INTERNAL_ERROR))
185 /* DAT_RETURN dapl_os_lock(IN DAPL_OS_LOCK *m) */
186 #define dapl_os_lock(m) ((DAT_RETURN)( \
187 (0 == pthread_mutex_lock((m))) ? \
189 (DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
191 /* DAT_RETURN dapl_os_unlock(IN DAPL_OS_LOCK *m) */
192 #define dapl_os_unlock(m) ((DAT_RETURN)( \
193 (0 == pthread_mutex_unlock((m))) ? \
195 (DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
197 /* DAT_RETURN dapl_os_lock_destroy(IN DAPL_OS_LOCK *m) */
198 #define dapl_os_lock_destroy(m) ((DAT_RETURN)( \
199 (0 == pthread_mutex_destroy((m))) ? \
201 (DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
207 * The wait object invariant: Presuming a call to dapl_os_wait_object_wait
208 * occurs at some point, there will be at least one wakeup after each call
209 * to dapl_os_wait_object_signal. I.e. Signals are not ignored, though
210 * they may be coallesced.
215 DAT_BOOLEAN signaled
;
217 pthread_mutex_t lock
;
218 } DAPL_OS_WAIT_OBJECT
;
220 /* function prototypes */
222 dapl_os_wait_object_init(
223 IN DAPL_OS_WAIT_OBJECT
*wait_obj
);
226 dapl_os_wait_object_wait(
227 IN DAPL_OS_WAIT_OBJECT
*wait_obj
,
228 IN DAT_TIMEOUT timeout_val
);
231 dapl_os_wait_object_wakeup(
232 IN DAPL_OS_WAIT_OBJECT
*wait_obj
);
235 dapl_os_wait_object_destroy(
236 IN DAPL_OS_WAIT_OBJECT
*wait_obj
);
242 /* void *dapl_os_alloc(int size) */
243 #define dapl_os_alloc(size) malloc((size))
245 /* void *dapl_os_realloc(void *ptr, int size) */
246 #define dapl_os_realloc(ptr, size) realloc((ptr), (size))
248 /* void dapl_os_free(void *ptr, int size) */
249 #define dapl_os_free(ptr, size) free((ptr))
251 /* void * dapl_os_memzero(void *loc, int size) */
252 #define dapl_os_memzero(loc, size) memset((loc), 0, (size))
254 /* void * dapl_os_memcpy(void *dest, const void *src, int len) */
255 #define dapl_os_memcpy(dest, src, len) memcpy((dest), (src), (len))
257 /* int dapl_os_memcmp(const void *mem1, const void *mem2, int len) */
258 #define dapl_os_memcmp(mem1, mem2, len) memcmp((mem1), (mem2), (len))
264 /* unsigned int dapl_os_strlen(const char *str) */
265 #define dapl_os_strlen(str) strlen((str))
266 /* char * dapl_os_strdup(const char *str) */
267 #define dapl_os_strdup(str) strdup((str))
268 /* char *strcpy(char *dest, char *src) */
269 #define dapl_os_strcpy(dest, src) strcpy((dest), (src))
270 /* char *strncpy(char *s1, const char *s2, size_t n) */
271 #define dapl_os_strncpy(dest, src, len) strncpy((dest), (src), (len))
272 /* char *strcat(char *dest, char *src) */
273 #define dapl_os_strcat(dest, src) strcat((dest), (src))
279 typedef DAT_UINT64 DAPL_OS_TIMEVAL
;
282 typedef unsigned long long int DAPL_OS_TICKS
;
284 /* function prototypes */
287 * Sleep for the number of micro seconds specified by the invoking
290 * void dapl_os_sleep_usec(int sleep_time)
292 #define dapl_os_sleep_usec(sleep_time) { \
293 struct timespec sleep_spec; \
294 sleep_spec.tv_sec = (sleep_time) / 100000; \
295 sleep_spec.tv_nsec = (sleep_time) % 100000 * 1000; \
296 nanosleep(&sleep_spec, NULL); \
299 DAT_RETURN
dapl_os_get_time(DAPL_OS_TIMEVAL
*);
303 * Name Service Helper functions
306 #if defined(IBHOSTS_NAMING)
307 #define dapls_osd_getaddrinfo(name, addr_ptr) \
308 getaddrinfo((name), NULL, NULL, (addr_ptr))
309 #define dapls_osd_freeaddrinfo(addr) freeaddrinfo((addr))
311 #endif /* IBHOSTS_NAMING */
314 * *printf format helpers. We use the C string constant concatenation
315 * ability to define 64 bit formats, which unfortunatly are non standard
316 * in the C compiler world. E.g. %llx for gcc, %I64x for Windows
325 * Conversion Functions
328 /* long int dapl_os_strtol(const char *nptr, char **endptr, int base) */
329 #define dapl_os_strtol(nptr, endptr, base) strtol((nptr), (endptr), (base))
336 #define dapl_os_assert(expression) assert((expression))
337 #define dapl_os_printf printf
338 #define dapl_os_vprintf(fmt, args) vprintf((fmt), (args))
339 #define dapl_os_syslog(fmt, args) vsyslog(LOG_USER | LOG_DEBUG, \
345 #endif /* _DAPL_OSD_H_ */