Merge branch 'master' of ssh://git.onelab.eu/git/sliver-openvswitch
[sliver-openvswitch.git] / lib / compiler.h
index 1c01fd1..e867d72 100644 (file)
 #ifndef __has_feature
   #define __has_feature(x) 0
 #endif
+#ifndef __has_extension
+  #define __has_extension(x) 0
+#endif
 
 #if __GNUC__ && !__CHECKER__
 #define NO_RETURN __attribute__((__noreturn__))
 #define OVS_UNUSED __attribute__((__unused__))
 #define PRINTF_FORMAT(FMT, ARG1) __attribute__((__format__(printf, FMT, ARG1)))
+#define SCANF_FORMAT(FMT, ARG1) __attribute__((__format__(scanf, FMT, ARG1)))
 #define STRFTIME_FORMAT(FMT) __attribute__((__format__(__strftime__, FMT, 0)))
 #define MALLOC_LIKE __attribute__((__malloc__))
 #define ALWAYS_INLINE __attribute__((always_inline))
@@ -36,6 +40,7 @@
 #define NO_RETURN
 #define OVS_UNUSED
 #define PRINTF_FORMAT(FMT, ARG1)
+#define SCANF_FORMAT(FMT, ARG1)
 #define STRFTIME_FORMAT(FMT)
 #define MALLOC_LIKE
 #define ALWAYS_INLINE
  *      while the specific MUTEX is held.
  *
  *
+ * On a variable A of mutex type:
+ *
+ *    - OVS_ACQ_BEFORE(B), where B is a mutex or a comma-separated list of
+ *      mutexes, declare that if both A and B are acquired at the same time,
+ *      then A must be acquired before B.  That is, B nests inside A.
+ *
+ *    - OVS_ACQ_AFTER(B) is the opposite of OVS_ACQ_BEFORE(B), that is, it
+ *      declares that A nests inside B.
+ *
+ *
  * On a function, the following attributes apply to mutexes:
  *
  *    - OVS_ACQUIRES(MUTEX) indicate that the function must be called without
@@ -81,8 +96,9 @@
  *    - OVS_REQUIRES(MUTEX) indicate that the function may only be called with
  *      MUTEX held and that the function does not release MUTEX.
  *
- *    - OVS_LOCKS_EXCLUDED(MUTEX) indicates that the function may only be
- *      called when MUTEX is not held.
+ *    - OVS_EXCLUDED(MUTEX) indicates that the function may only be called when
+ *      MUTEX is not held.
+ *
  *
  * The following variants, with the same syntax, apply to reader-writer locks:
  *
  *    OVS_RELEASES         OVS_RELEASES         OVS_RELEASES
  *    OVS_TRY_LOCK         OVS_TRY_RDLOCK       OVS_TRY_WRLOCK
  *    OVS_REQUIRES         OVS_REQ_RDLOCK       OVS_REQ_WRLOCK
- *    OVS_LOCKS_EXCLUDED   OVS_LOCKS_EXCLUDED   OVS_LOCKS_EXCLUDED
+ *    OVS_EXCLUDED         OVS_EXCLUDED         OVS_EXCLUDED
  */
 #define OVS_LOCKABLE __attribute__((lockable))
 #define OVS_REQ_RDLOCK(...) __attribute__((shared_locks_required(__VA_ARGS__)))
 #define OVS_GUARDED __attribute__((guarded_var))
 #define OVS_GUARDED_BY(...) __attribute__((guarded_by(__VA_ARGS__)))
 #define OVS_RELEASES(...) __attribute__((unlock_function(__VA_ARGS__)))
-#define OVS_LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
-#elif __CHECKER__
-/* "sparse" annotations for mutexes and mutex-like constructs.
- *
- * Change the thread-safety check annotations to use "context" attribute.
- *
- * OVS_MACRO_LOCK and OVS_MACRO_RELEASE are suitable for use within macros,
- * where there is no function prototype to annotate. */
-#define OVS_LOCKABLE
-#define OVS_REQ_RDLOCK(...) __attribute__((context(MUTEX, 1, 1)))
-#define OVS_ACQ_RDLOCK(...) __attribute__((context(MUTEX, 0, 1)))
-#define OVS_REQ_WRLOCK(...) __attribute__((context(MUTEX, 1, 1)))
-#define OVS_ACQ_WRLOCK(...) __attribute__((context(MUTEX, 0, 1)))
-#define OVS_REQUIRES(...)   __attribute__((context(MUTEX, 1, 1)))
-#define OVS_ACQUIRES(...)   __attribute__((context(MUTEX, 0, 1)))
-#define OVS_TRY_WRLOCK(RETVAL, ...)
-#define OVS_TRY_RDLOCK(RETVAL, ...)
-#define OVS_TRY_LOCK(REVAL, ...)
-#define OVS_GUARDED
-#define OVS_GUARDED_BY(...)
-#define OVS_RELEASES(...)   __attribute__((context(MUTEX, 1, 0)))
-#define OVS_MACRO_LOCK(...) __context__(MUTEX, 0, 1)
-#define OVS_MACRO_RELEASE(...) __context__(MUTEX, 1, 0)
-#else
+#define OVS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__)))
+#define OVS_ACQ_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
+#define OVS_ACQ_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
+#define OVS_NO_THREAD_SAFETY_ANALYSIS \
+    __attribute__((no_thread_safety_analysis))
+#else  /* not Clang */
 #define OVS_LOCKABLE
 #define OVS_REQ_RDLOCK(...)
 #define OVS_ACQ_RDLOCK(...)
 #define OVS_TRY_LOCK(...)
 #define OVS_GUARDED
 #define OVS_GUARDED_BY(...)
+#define OVS_EXCLUDED(...)
 #define OVS_RELEASES(...)
-#define OVS_MACRO_LOCK(...)
-#define OVS_MACRO_RELEASE(...)
+#define OVS_ACQ_BEFORE(...)
+#define OVS_ACQ_AFTER(...)
+#define OVS_NO_THREAD_SAFETY_ANALYSIS
 #endif
 
 /* ISO C says that a C implementation may choose any integer type for an enum
 #define OVS_PACKED(DECL) __pragma(pack(push, 1)) DECL __pragma(pack(pop))
 #endif
 
+#ifdef _MSC_VER
+#define CCALL __cdecl
+#pragma section(".CRT$XCU",read)
+#define OVS_CONSTRUCTOR(f) \
+    static void __cdecl f(void); \
+    __declspec(allocate(".CRT$XCU")) void (__cdecl*f##_)(void) = f; \
+    static void __cdecl f(void)
+#else
+#define OVS_CONSTRUCTOR(f) \
+    static void f(void) __attribute__((constructor)); \
+    static void f(void)
+#endif
+
 #endif /* compiler.h */