mapperd: Always sleep before checking request state.
[archipelago] / xseg / peers / user / common.h
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 /*
36  * vkoukis.h
37  */
38
39 #define _GNU_SOURCE
40
41 #ifndef _COMMON_H
42 #define _COMMON_H
43
44 #ifdef DEBUG
45 #define p_debug(str, ...)       \
46         perr(PD, 0, "%s: " str, __func__ , __VA_ARGS__)
47 #else
48 #define p_debug(str, ...)       do {const char *s = __VA_ARGS__} while (0)
49 #endif
50
51 #define always_assert(condition) \
52         do { \
53                 if (!(condition)) \
54                 perr(PFE, 1, "%s:%d: Assertion failed: " # condition, \
55                                 __FILE__, __LINE__); \
56         } while (0)
57
58 #ifdef DEBUG
59 #define assert(condition) \
60         do { \
61                 if (!(condition)) \
62                 perr(PFE, 0, "%s:%d: Assertion failed: " # condition, \
63                                 __FILE__, __LINE__); \
64         } while (0)
65 #else
66 #define assert(condition)       do { } while (0)
67 #endif
68
69 #define PERR_BUF_SIZE           2048    
70 #define HOSTNAME_BUF_SIZE       100
71
72 /* Compiler-specific stuff */
73 #define VAR_MAY_BE_UNUSED(x)    ((void)(x))
74
75 /*
76  * Function prototypes and extern definitions
77  */
78
79 /* Perr fatal error, error, information, warning, debug */
80 enum perr_type { PFE = 1, PE = 0, PI = -1, PW = -2, PD = -3 };
81
82 void init_perr(char *prog_name);
83 void perr_func(int type, int want_errno, char *fmt, ...)
84         __attribute__ ((format (printf, 3, 4)));
85
86 /* No inline form of perr can be used, since it is variadic (See gcc manual) */
87
88 #define __fmt2(fmt0, arg0, arg1, fmt1, ...) fmt0 fmt1 "%s", arg0, arg1, __VA_ARGS__
89
90 #ifdef DEBUG
91 #define perr(type, want_errno, ...) \
92         perr_func(type, want_errno, \
93                 __fmt2("%s: %d: ", __func__, __LINE__, __VA_ARGS__, ""))
94 #else
95 #define perr(type, want_errno, ...) \
96         do {                        \
97                 if (type > PD)      \
98                 perr_func(type, want_errno, \
99                         __fmt2("%s: %d: ", __func__, __LINE__, __VA_ARGS__, "")); \
100         } while (0)
101 #endif
102
103 /* String manipulation */
104 size_t strlcpy(char *dst, const char *src, size_t siz);
105 size_t strlcat(char *dst, const char *src, size_t siz);
106
107 #endif  /* _COMMON_H */