nlib
Platform_ctr.h
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_PLATFORM_CTR_H_
4 #define INCLUDE_NN_NLIB_PLATFORM_CTR_H_
5 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
6 # error do not include directly
7 #endif
8 
9 #ifdef NN_PLATFORM_CTR
10 
11 #define NLIB_HAS_STDHEADER_STDINT
12 #define NLIB_HAS_STDHEADER_INTTYPES
13 
14 #ifndef __USE_C99_MATH
15 # define __USE_C99_MATH
16 #endif
17 
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define NLIB_ALWAYS_INLINE __forceinline
25 #define NLIB_NEVER_INLINE __declspec(noinline)
26 // Do not write like NLIB_(UN)LIKELY(a || b) or NLIB_(UN)LIKELY(a && b).
27 // Write like (NLIB_(UN)LIKELY(a) || NLIB_(UN)LIKELY(b)) instead.
28 // armcc optimizer may generate wrong binary.
29 #define NLIB_LIKELY(x) __builtin_expect(!!(x), 1)
30 #define NLIB_UNLIKELY(x) __builtin_expect(!!(x), 0)
31 #define NLIB_EXPECT(var, exp_value) __builtin_expect((var), (exp_value))
32 #define NLIB_CHECK_RESULT
33 #define NLIB_NORETURN __attribute__((noreturn))
34 #define NLIB_NONNULL __attribute__((nonnull))
35 #define NLIB_NONNULL_1 __attribute__((nonnull (1)))
36 #define NLIB_NONNULL_2 __attribute__((nonnull (2)))
37 #define NLIB_NONNULL_3 __attribute__((nonnull (3)))
38 #define NLIB_NONNULL_4 __attribute__((nonnull (4)))
39 #define NLIB_NONNULL_5 __attribute__((nonnull (5)))
40 #define NLIB_NONNULL_ENABLED
41 #define NLIB_ATTRIBUTE_MALLOC __attribute__((malloc))
42 #define NLIB_ATTRIBUTE_PURE __attribute__((pure))
43 #define NLIB_ATTRIBUTE_CONST __attribute__((const))
44 #define NLIB_ATTRIBUTE_ALLOC_SIZE1(n)
45 #define NLIB_ATTRIBUTE_ALLOC_SIZE2(n0, n1)
46 #define NLIB_ATTRIBUTE_ALLOC_ALIGN(algn)
47 #define NLIB_ATTRIBUTE_ASSUME_ALIGNED(n)
48 #ifndef NLIB_DEPRECATED
49 #define NLIB_DEPRECATED
50 #endif
51 #ifndef NLIB_DEPRECATED_MSG
52 #define NLIB_DEPRECATED_MSG(msg)
53 #endif
54 #define NLIB_VIS_HIDDEN
55 #define NLIB_VIS_PUBLIC
56 #define NLIB_WEAKSYMBOL __attribute__((weak))
57 #define NLIB_LITTLE_ENDIAN
58 
59 void nlib_ctr_barrier(void);
60 #define NLIB_MEMORY_ORDER_RELEASE nlib_ctr_barrier()
61 #define NLIB_MEMORY_ORDER_ACQUIRE nlib_ctr_barrier()
62 #define NLIB_MEMORY_ORDER_ACQ_REL nlib_ctr_barrier()
63 #define NLIB_MEMORY_ORDER_SEQ_CST nlib_ctr_barrier()
64 
65 typedef unsigned int nlib_tls;
66 
67 struct nlib_mutex_ {
68  unsigned int _[3]; // ::nn::os::CriticalSection
69 };
70 typedef struct nlib_mutex_ nlib_mutex;
71 #define NLIB_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
72 #define NLIB_RECURSIVE_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
73 #define NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
74 
75 typedef void* nlib_thread;
76 
77 struct nlib_semaphore_ {
78  unsigned int _[1]; // nn::os::Semaphore
79 };
80 typedef struct nlib_semaphore_ nlib_semaphore;
81 
82 struct nlib_cond_ {
83  unsigned int _; // nn::os::Event
84 };
85 typedef struct nlib_cond_ nlib_cond;
86 #define NLIB_COND_INITIALIZER {0}
87 
88 __declspec(noreturn) int MyNoreturn_(void);
89 #define NLIB_ASSUME(cond) (void)((cond) || MyNoreturn_())
90 
91 #define NLIB_LIBC_nlib_memcmp
92 
93 #define NLIB_ATOMIC_RELAXED (0)
94 #define NLIB_ATOMIC_ACQUIRE (1)
95 #define NLIB_ATOMIC_RELEASE (2)
96 #define NLIB_ATOMIC_ACQ_REL (3)
97 #define NLIB_ATOMIC_SEQ_CST (7)
98 
99 int32_t nlib_atomic_load32(const int32_t* ptr, int memorder);
100 void nlib_atomic_store32(int32_t* ptr, int32_t val, int memorder);
101 int32_t nlib_atomic_exchange32(int32_t* ptr, int32_t val, int memorder);
102 int nlib_atomic_compare_exchange32(int32_t* ptr, int32_t* expected,
103  int32_t desired, int weak,
104  int success_memorder, int failure_memorder);
105 int32_t nlib_atomic_add_fetch32(int32_t* ptr, int32_t val, int memorder);
106 int32_t nlib_atomic_sub_fetch32(int32_t* ptr, int32_t val, int memorder);
107 int32_t nlib_atomic_and_fetch32(int32_t* ptr, int32_t val, int memorder);
108 int32_t nlib_atomic_xor_fetch32(int32_t* ptr, int32_t val, int memorder);
109 int32_t nlib_atomic_or_fetch32(int32_t* ptr, int32_t val, int memorder);
110 int32_t nlib_atomic_fetch_add32(int32_t* ptr, int32_t val, int memorder);
111 int32_t nlib_atomic_fetch_sub32(int32_t* ptr, int32_t val, int memorder);
112 int32_t nlib_atomic_fetch_and32(int32_t* ptr, int32_t val, int memorder);
113 int32_t nlib_atomic_fetch_xor32(int32_t* ptr, int32_t val, int memorder);
114 int32_t nlib_atomic_fetch_or32(int32_t* ptr, int32_t val, int memorder);
115 
116 int64_t nlib_atomic_load64(const int64_t* ptr, int memorder);
117 void nlib_atomic_store64(int64_t* ptr, int64_t val, int memorder);
118 int64_t nlib_atomic_exchange64(int64_t* ptr, int64_t val, int memorder);
119 int nlib_atomic_compare_exchange64(int64_t* ptr, int64_t* expected,
120  int64_t desired, int weak,
121  int success_memorder, int failure_memorder);
122 int64_t nlib_atomic_add_fetch64(int64_t* ptr, int64_t val, int memorder);
123 int64_t nlib_atomic_sub_fetch64(int64_t* ptr, int64_t val, int memorder);
124 int64_t nlib_atomic_and_fetch64(int64_t* ptr, int64_t val, int memorder);
125 int64_t nlib_atomic_xor_fetch64(int64_t* ptr, int64_t val, int memorder);
126 int64_t nlib_atomic_or_fetch64(int64_t* ptr, int64_t val, int memorder);
127 int64_t nlib_atomic_fetch_add64(int64_t* ptr, int64_t val, int memorder);
128 int64_t nlib_atomic_fetch_sub64(int64_t* ptr, int64_t val, int memorder);
129 int64_t nlib_atomic_fetch_and64(int64_t* ptr, int64_t val, int memorder);
130 int64_t nlib_atomic_fetch_xor64(int64_t* ptr, int64_t val, int memorder);
131 int64_t nlib_atomic_fetch_or64(int64_t* ptr, int64_t val, int memorder);
132 
133 void* nlib_atomic_loadptr(void* const* ptr, int memorder);
134 void nlib_atomic_storeptr(void** ptr, void* val, int memorder);
135 int nlib_atomic_compare_exchangeptr(void** ptr, void** expected,
136  void* desired, int weak,
137  int success_memorder, int failure_memorder);
138 
139 static NLIB_ALWAYS_INLINE void nlib_atomic_thread_fence(int memorder) {
140  if (memorder != NLIB_ATOMIC_RELAXED)
141  nlib_ctr_barrier();
142 }
143 
144 #define NLIB_FD_O_RDONLY (0x0000)
145 #define NLIB_FD_O_WRONLY (0x0001)
146 #define NLIB_FD_O_RDWR (0x0002)
147 #define NLIB_FD_O_APPEND (0x0008)
148 #define NLIB_FD_O_CREAT (0x0100)
149 #define NLIB_FD_O_TRUNC (0x0200)
150 #define NLIB_FD_O_EXCL (0x0400)
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #define NLIB_NOEXCEPT
157 #ifndef NLIB_HAS_ZLIB
158 # define NLIB_HAS_ZLIB
159 #endif
160 
161 #endif
162 #endif // INCLUDE_NN_NLIB_PLATFORM_CTR_H_
int32_t nlib_atomic_xor_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_xor_fetch() of gcc...
int64_t nlib_atomic_fetch_and64(int64_t *ptr, int64_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_fetch_and() of gcc...
int nlib_atomic_compare_exchangeptr(void **ptr, void **expected, void *desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
int32_t nlib_atomic_load32(const int32_t *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:69
int64_t nlib_atomic_fetch_add64(int64_t *ptr, int64_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_fetch_add() of gcc...
sem_t nlib_semaphore
The type for a semaphore object.
int32_t nlib_atomic_or_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_or_fetch() of gcc...
int64_t nlib_atomic_fetch_sub64(int64_t *ptr, int64_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_fetch_sub() of gcc...
int64_t nlib_atomic_and_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_and_fetch() of gcc...
int nlib_atomic_compare_exchange64(int64_t *ptr, int64_t *expected, int64_t desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
int64_t nlib_atomic_fetch_or64(int64_t *ptr, int64_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_fetch_or() of gcc...
pthread_key_t nlib_tls
The type for TLS slot IDs.
void * nlib_atomic_loadptr(void *const *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...
int64_t nlib_atomic_exchange64(int64_t *ptr, int64_t val, int memorder)
Swaps values in an atomic operation. Its behavior is similar to the one for __atomic_exchange_n() of ...
int64_t nlib_atomic_sub_fetch64(int64_t *ptr, int64_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_sub_fetch() of gcc...
int32_t nlib_atomic_fetch_xor32(int32_t *ptr, int32_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_fetch_xor() of gcc...
int32_t nlib_atomic_sub_fetch32(int32_t *ptr, int32_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_sub_fetch() of gcc...
int32_t nlib_atomic_fetch_sub32(int32_t *ptr, int32_t val, int memorder)
Subtracts atomic values. Its behavior is similar to the one for __atomic_fetch_sub() of gcc...
int32_t nlib_atomic_add_fetch32(int32_t *ptr, int32_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_add_fetch() of gcc...
void nlib_atomic_storeptr(void **ptr, void *val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
void nlib_atomic_thread_fence(int memorder)
Places the specified memory barrier.
int32_t nlib_atomic_fetch_and32(int32_t *ptr, int32_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_fetch_and() of gcc...
pthread_cond_t nlib_cond
The type for a condition variable object.
#define NLIB_ATOMIC_RELAXED
Similar to __ATOMIC_RELAXED of gcc or std::memory_order_relaxed of C++11.
pthread_mutex_t nlib_mutex
The type for mutex variables.
int64_t nlib_atomic_xor_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_xor_fetch() of gcc...
int64_t nlib_atomic_add_fetch64(int64_t *ptr, int64_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_add_fetch() of gcc...
int32_t nlib_atomic_and_fetch32(int32_t *ptr, int32_t val, int memorder)
Calculates AND of atomic values. Its behavior is similar to the one for __atomic_and_fetch() of gcc...
int nlib_atomic_compare_exchange32(int32_t *ptr, int32_t *expected, int32_t desired, int weak, int success_memorder, int failure_memorder)
Compares and swaps atomic values. Its behavior is similar to the one for __atomic_compare_exchange_n(...
void nlib_atomic_store64(int64_t *ptr, int64_t val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
int64_t nlib_atomic_or_fetch64(int64_t *ptr, int64_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_or_fetch() of gcc...
int32_t nlib_atomic_fetch_or32(int32_t *ptr, int32_t val, int memorder)
Calculates OR of atomic values. Its behavior is similar to the one for __atomic_fetch_or() of gcc...
int64_t nlib_atomic_fetch_xor64(int64_t *ptr, int64_t val, int memorder)
Calculates XOR of atomic values. Its behavior is similar to the one for __atomic_fetch_xor() of gcc...
int32_t nlib_atomic_fetch_add32(int32_t *ptr, int32_t val, int memorder)
Adds atomic values. Its behavior is similar to the one for __atomic_fetch_add() of gcc...
void nlib_atomic_store32(int32_t *ptr, int32_t val, int memorder)
Stores a value in an atomic operation. Its behavior is similar to the one for __atomic_store_n() of g...
int32_t nlib_atomic_exchange32(int32_t *ptr, int32_t val, int memorder)
Swaps values in an atomic operation. Its behavior is similar to the one for __atomic_exchange_n() of ...
pthread_t nlib_thread
The identifier for threads.
int64_t nlib_atomic_load64(const int64_t *ptr, int memorder)
Loads a value in an atomic operation. Its behavior is similar to the one for __atomic_load_n() of gcc...