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