8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / udapl / udapl_tavor / include / dapl_osd.h
blobdbed11b5e753ac06ac309bb1c1479ec250d47acb
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
33 * HEADER: dapl_osd.h
35 * PURPOSE: Operating System Dependent layer
36 * Description:
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 $
44 #ifndef _DAPL_OSD_H_
45 #define _DAPL_OSD_H_
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
52 * <assert.h> keys off of NDEBUG
54 #ifdef DAPL_DBG
55 #undef NDEBUG
56 #else
57 #define NDEBUG
58 #endif
60 #include <dat/udat.h>
61 #include <assert.h>
62 #include <errno.h>
63 #include <pthread.h>
64 #include <semaphore.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <stdarg.h>
69 #include <time.h>
70 #include <syslog.h>
71 #include <netdb.h>
72 #include <atomic.h>
73 #include "dapl_debug.h"
76 * networking related headers
78 #include <unistd.h>
79 #include <fcntl.h>
80 #include <sys/types.h>
81 #include <sys/socket.h>
82 #include <ctype.h>
83 #include <arpa/inet.h>
85 #ifndef _INLINE_
86 #define _INLINE_
87 #endif /* _INLINE_ */
90 * initialization function
92 void dapl_os_init(void);
94 #define dapl_os_panic(args) \
95 { \
96 fprintf(stderr, "PANIC in %s:%i:\n", __FILE__, __LINE__); \
97 fprintf(stderr, args); \
98 exit(1); \
101 int dapl_os_get_env_bool(
102 char *env_str);
104 int dapl_os_get_env_val(
105 char *env_str,
106 int def_val);
109 * Atomic operations
111 typedef volatile DAT_COUNT DAPL_ATOMIC;
114 * dapl_os_atomic_inc
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))
128 * dapl_os_atomic_dec
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); \
135 ((DAT_COUNT) \
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))
160 * Thread Functions
162 typedef pthread_t DAPL_OS_THREAD;
164 DAT_RETURN
165 dapl_os_thread_create(
166 IN void (*func) (void *),
167 IN void *data,
168 OUT DAPL_OS_THREAD *thread_id);
172 * Lock Functions
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)) ? \
182 DAT_SUCCESS : \
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))) ? \
188 DAT_SUCCESS : \
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))) ? \
194 DAT_SUCCESS : \
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))) ? \
200 DAT_SUCCESS : \
201 (DAT_CLASS_ERROR | DAT_INTERNAL_ERROR)))
203 * Wait Objects
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.
213 typedef struct
215 DAT_BOOLEAN signaled;
216 pthread_cond_t cv;
217 pthread_mutex_t lock;
218 } DAPL_OS_WAIT_OBJECT;
220 /* function prototypes */
221 DAT_RETURN
222 dapl_os_wait_object_init(
223 IN DAPL_OS_WAIT_OBJECT *wait_obj);
225 DAT_RETURN
226 dapl_os_wait_object_wait(
227 IN DAPL_OS_WAIT_OBJECT *wait_obj,
228 IN DAT_TIMEOUT timeout_val);
230 DAT_RETURN
231 dapl_os_wait_object_wakeup(
232 IN DAPL_OS_WAIT_OBJECT *wait_obj);
234 DAT_RETURN
235 dapl_os_wait_object_destroy(
236 IN DAPL_OS_WAIT_OBJECT *wait_obj);
239 * Memory Functions
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))
261 * String Functions
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))
276 * Timer Functions
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
288 * function
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
318 #define F64d "%lld"
319 #define F64u "%llu"
320 #define F64x "%llx"
321 #define F64X "%llX"
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))
332 * Helper Functions
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, \
340 (fmt), (args))
341 #ifdef __cplusplus
343 #endif
345 #endif /* _DAPL_OSD_H_ */