nlib
Platform_ctr.h
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
13  *---------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_PLATFORM_CTR_H_
17 #define INCLUDE_NN_NLIB_PLATFORM_CTR_H_
18 #ifndef INCLUDE_NN_NLIB_PLATFORM_H_
19 # error do not include directly
20 #endif
21 
22 #ifdef NN_PLATFORM_CTR
23 
24 #ifdef __cplusplus
25 #ifndef __STDC_LIMIT_MACROS
26 #define __STDC_LIMIT_MACROS
27 #endif
28 #ifndef __STDC_CONSTANT_MACROS
29 #define __STDC_CONSTANT_MACROS
30 #endif
31 #endif
32 
33 #define NLIB_HAS_STDHEADER_INTTYPES
34 
35 #ifndef __USE_C99_MATH
36 # define __USE_C99_MATH
37 #endif
38 
39 #include <stdint.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #define NLIB_ALWAYS_INLINE __forceinline
46 #define NLIB_NEVER_INLINE __declspec(noinline)
47 // Do not write like NLIB_(UN)LIKELY(a || b) or NLIB_(UN)LIKELY(a && b).
48 // Write like (NLIB_(UN)LIKELY(a) || NLIB_(UN)LIKELY(b)) instead.
49 // armcc optimizer may generate wrong binary.
50 #define NLIB_LIKELY(x) __builtin_expect(!!(x), 1)
51 #define NLIB_UNLIKELY(x) __builtin_expect(!!(x), 0)
52 #define NLIB_EXPECT(var, exp_value) __builtin_expect((var), (exp_value))
53 #define NLIB_CHECK_RESULT
54 #define NLIB_NORETURN __attribute__((noreturn))
55 #define NLIB_NONNULL __attribute__((nonnull))
56 #define NLIB_NONNULL_1 __attribute__((nonnull (1)))
57 #define NLIB_NONNULL_2 __attribute__((nonnull (2)))
58 #define NLIB_NONNULL_3 __attribute__((nonnull (3)))
59 #define NLIB_NONNULL_4 __attribute__((nonnull (4)))
60 #define NLIB_NONNULL_5 __attribute__((nonnull (5)))
61 #define NLIB_NONNULL_ENABLED
62 #define NLIB_ATTRIBUTE_MALLOC __attribute__((malloc))
63 #define NLIB_ATTRIBUTE_PURE __attribute__((pure))
64 #define NLIB_ATTRIBUTE_CONST __attribute__((const))
65 #define NLIB_ATTRIBUTE_ALLOC_SIZE1(n)
66 #define NLIB_ATTRIBUTE_ALLOC_SIZE2(n0, n1)
67 #define NLIB_ATTRIBUTE_ALLOC_ALIGN(algn)
68 #define NLIB_ATTRIBUTE_ASSUME_ALIGNED(n)
69 #ifndef NLIB_DEPRECATED
70 #define NLIB_DEPRECATED
71 #endif
72 #ifndef NLIB_DEPRECATED_MSG
73 #define NLIB_DEPRECATED_MSG(msg)
74 #endif
75 #define NLIB_VIS_HIDDEN
76 #define NLIB_VIS_PUBLIC
77 #define NLIB_WEAKSYMBOL __attribute__((weak))
78 #define NLIB_LITTLE_ENDIAN
79 
80 void nlib_ctr_barrier(void);
81 #define NLIB_MEMORY_ORDER_RELEASE nlib_ctr_barrier()
82 #define NLIB_MEMORY_ORDER_ACQUIRE nlib_ctr_barrier()
83 #define NLIB_MEMORY_ORDER_ACQ_REL nlib_ctr_barrier()
84 #define NLIB_MEMORY_ORDER_SEQ_CST nlib_ctr_barrier()
85 
86 typedef unsigned int nlib_tls;
87 
88 struct nlib_mutex_ {
89  unsigned int _[3]; // ::nn::os::CriticalSection
90 };
91 typedef struct nlib_mutex_ nlib_mutex;
92 #define NLIB_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
93 #define NLIB_RECURSIVE_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
94 #define NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
95 
96 typedef void* nlib_thread;
97 
98 struct nlib_semaphore_ {
99  unsigned int _[1]; // nn::os::Semaphore
100 };
101 typedef struct nlib_semaphore_ nlib_semaphore;
102 
103 struct nlib_cond_ {
104  unsigned int _; // nn::os::Event
105 };
106 typedef struct nlib_cond_ nlib_cond;
107 #define NLIB_COND_INITIALIZER {0}
108 
109 __declspec(noreturn) int MyNoreturn_(void);
110 #define NLIB_ASSUME(cond) (void)((cond) || MyNoreturn_())
111 
112 #define NLIB_LIBC_nlib_memcmp
113 
114 #define NLIB_ATOMIC_RELAXED (0)
115 #define NLIB_ATOMIC_ACQUIRE (1)
116 #define NLIB_ATOMIC_RELEASE (2)
117 #define NLIB_ATOMIC_ACQ_REL (3)
118 #define NLIB_ATOMIC_SEQ_CST (7)
119 
120 int32_t nlib_atomic_load32(const int32_t* ptr, int memorder);
121 void nlib_atomic_store32(int32_t* ptr, int32_t val, int memorder);
122 int32_t nlib_atomic_exchange32(int32_t* ptr, int32_t val, int memorder);
123 int nlib_atomic_compare_exchange32(int32_t* ptr, int32_t* expected,
124  int32_t desired, int weak,
125  int success_memorder, int failure_memorder);
126 int32_t nlib_atomic_add_fetch32(int32_t* ptr, int32_t val, int memorder);
127 int32_t nlib_atomic_sub_fetch32(int32_t* ptr, int32_t val, int memorder);
128 int32_t nlib_atomic_and_fetch32(int32_t* ptr, int32_t val, int memorder);
129 int32_t nlib_atomic_xor_fetch32(int32_t* ptr, int32_t val, int memorder);
130 int32_t nlib_atomic_or_fetch32(int32_t* ptr, int32_t val, int memorder);
131 int32_t nlib_atomic_fetch_add32(int32_t* ptr, int32_t val, int memorder);
132 int32_t nlib_atomic_fetch_sub32(int32_t* ptr, int32_t val, int memorder);
133 int32_t nlib_atomic_fetch_and32(int32_t* ptr, int32_t val, int memorder);
134 int32_t nlib_atomic_fetch_xor32(int32_t* ptr, int32_t val, int memorder);
135 int32_t nlib_atomic_fetch_or32(int32_t* ptr, int32_t val, int memorder);
136 
137 int64_t nlib_atomic_load64(const int64_t* ptr, int memorder);
138 void nlib_atomic_store64(int64_t* ptr, int64_t val, int memorder);
139 int64_t nlib_atomic_exchange64(int64_t* ptr, int64_t val, int memorder);
140 int nlib_atomic_compare_exchange64(int64_t* ptr, int64_t* expected,
141  int64_t desired, int weak,
142  int success_memorder, int failure_memorder);
143 int64_t nlib_atomic_add_fetch64(int64_t* ptr, int64_t val, int memorder);
144 int64_t nlib_atomic_sub_fetch64(int64_t* ptr, int64_t val, int memorder);
145 int64_t nlib_atomic_and_fetch64(int64_t* ptr, int64_t val, int memorder);
146 int64_t nlib_atomic_xor_fetch64(int64_t* ptr, int64_t val, int memorder);
147 int64_t nlib_atomic_or_fetch64(int64_t* ptr, int64_t val, int memorder);
148 int64_t nlib_atomic_fetch_add64(int64_t* ptr, int64_t val, int memorder);
149 int64_t nlib_atomic_fetch_sub64(int64_t* ptr, int64_t val, int memorder);
150 int64_t nlib_atomic_fetch_and64(int64_t* ptr, int64_t val, int memorder);
151 int64_t nlib_atomic_fetch_xor64(int64_t* ptr, int64_t val, int memorder);
152 int64_t nlib_atomic_fetch_or64(int64_t* ptr, int64_t val, int memorder);
153 static NLIB_ALWAYS_INLINE void* nlib_atomic_exchangeptr(void** ptr, void* val,
154  int memorder) {
155  return (void*)nlib_atomic_exchange32((int32_t*)ptr, (int32_t)val, memorder); // NOLINT
156 }
157 
158 void* nlib_atomic_loadptr(void* const* ptr, int memorder);
159 void nlib_atomic_storeptr(void** ptr, void* val, int memorder);
160 int nlib_atomic_compare_exchangeptr(void** ptr, void** expected,
161  void* desired, int weak,
162  int success_memorder, int failure_memorder);
163 
164 static NLIB_ALWAYS_INLINE void nlib_atomic_thread_fence(int memorder) {
165  if (memorder != NLIB_ATOMIC_RELAXED)
166  nlib_ctr_barrier();
167 }
168 
169 #define NLIB_FD_O_RDONLY (0x0000)
170 #define NLIB_FD_O_WRONLY (0x0001)
171 #define NLIB_FD_O_RDWR (0x0002)
172 #define NLIB_FD_O_APPEND (0x0008)
173 #define NLIB_FD_O_CREAT (0x0100)
174 #define NLIB_FD_O_TRUNC (0x0200)
175 #define NLIB_FD_O_EXCL (0x0400)
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #define NLIB_NOEXCEPT
182 #ifndef NLIB_HAS_ZLIB
183 # define NLIB_HAS_ZLIB
184 #endif
185 
186 #endif
187 #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...
void * nlib_atomic_exchangeptr(void **ptr, void *val, int memorder)
Swaps values in an atomic manner. Its behavior is similar to the one for __atomic_exchange_n() 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:95
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...