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_NONNULL __attribute__((nonnull))
54 #define NLIB_NONNULL_1 __attribute__((nonnull (1)))
55 #define NLIB_NONNULL_2 __attribute__((nonnull (2)))
56 #define NLIB_NONNULL_3 __attribute__((nonnull (3)))
57 #define NLIB_NONNULL_4 __attribute__((nonnull (4)))
58 #define NLIB_NONNULL_5 __attribute__((nonnull (5)))
59 #define NLIB_NONNULL_ENABLED
60 #define NLIB_ATTRIBUTE_MALLOC __attribute__((malloc))
61 #define NLIB_ATTRIBUTE_PURE __attribute__((pure))
62 #define NLIB_ATTRIBUTE_CONST __attribute__((const))
63 #define NLIB_ATTRIBUTE_ALLOC_SIZE1(n)
64 #define NLIB_ATTRIBUTE_ALLOC_SIZE2(n0, n1)
65 #define NLIB_ATTRIBUTE_ALLOC_ALIGN(algn)
66 #define NLIB_ATTRIBUTE_ASSUME_ALIGNED(n)
67 #ifndef NLIB_DEPRECATED
68 #define NLIB_DEPRECATED
69 #endif
70 #ifndef NLIB_DEPRECATED_MSG
71 #define NLIB_DEPRECATED_MSG(msg)
72 #endif
73 #define NLIB_VIS_HIDDEN
74 #define NLIB_VIS_PUBLIC
75 #define NLIB_WEAKSYMBOL __attribute__((weak))
76 #define NLIB_LITTLE_ENDIAN
77 
78 void nlib_ctr_barrier(void);
79 #define NLIB_MEMORY_ORDER_RELEASE nlib_ctr_barrier()
80 #define NLIB_MEMORY_ORDER_ACQUIRE nlib_ctr_barrier()
81 #define NLIB_MEMORY_ORDER_ACQ_REL nlib_ctr_barrier()
82 #define NLIB_MEMORY_ORDER_SEQ_CST nlib_ctr_barrier()
83 
84 typedef unsigned int nlib_tls;
85 
86 struct nlib_mutex_ {
87  unsigned int _[3]; // ::nn::os::CriticalSection
88 };
89 typedef struct nlib_mutex_ nlib_mutex;
90 #define NLIB_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
91 #define NLIB_RECURSIVE_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
92 #define NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER { 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL }
93 
94 typedef void* nlib_thread;
95 
96 struct nlib_semaphore_ {
97  unsigned int _[1]; // nn::os::Semaphore
98 };
99 typedef struct nlib_semaphore_ nlib_semaphore;
100 
101 struct nlib_cond_ {
102  unsigned int _; // nn::os::Event
103 };
104 typedef struct nlib_cond_ nlib_cond;
105 #define NLIB_COND_INITIALIZER {0}
106 
107 __declspec(noreturn) int MyNoreturn_(void);
108 #define NLIB_ASSUME(cond) (void)((cond) || MyNoreturn_())
109 
110 #define NLIB_LIBC_nlib_memcmp
111 
112 #define NLIB_ATOMIC_RELAXED (0)
113 #define NLIB_ATOMIC_ACQUIRE (1)
114 #define NLIB_ATOMIC_RELEASE (2)
115 #define NLIB_ATOMIC_ACQ_REL (3)
116 #define NLIB_ATOMIC_SEQ_CST (7)
117 
118 int32_t nlib_atomic_load32(const int32_t* ptr, int memorder);
119 void nlib_atomic_store32(int32_t* ptr, int32_t val, int memorder);
120 int32_t nlib_atomic_exchange32(int32_t* ptr, int32_t val, int memorder);
121 int nlib_atomic_compare_exchange32(int32_t* ptr, int32_t* expected,
122  int32_t desired, int weak,
123  int success_memorder, int failure_memorder);
124 int32_t nlib_atomic_add_fetch32(int32_t* ptr, int32_t val, int memorder);
125 int32_t nlib_atomic_sub_fetch32(int32_t* ptr, int32_t val, int memorder);
126 int32_t nlib_atomic_and_fetch32(int32_t* ptr, int32_t val, int memorder);
127 int32_t nlib_atomic_xor_fetch32(int32_t* ptr, int32_t val, int memorder);
128 int32_t nlib_atomic_or_fetch32(int32_t* ptr, int32_t val, int memorder);
129 int32_t nlib_atomic_fetch_add32(int32_t* ptr, int32_t val, int memorder);
130 int32_t nlib_atomic_fetch_sub32(int32_t* ptr, int32_t val, int memorder);
131 int32_t nlib_atomic_fetch_and32(int32_t* ptr, int32_t val, int memorder);
132 int32_t nlib_atomic_fetch_xor32(int32_t* ptr, int32_t val, int memorder);
133 int32_t nlib_atomic_fetch_or32(int32_t* ptr, int32_t val, int memorder);
134 
135 int64_t nlib_atomic_load64(const int64_t* ptr, int memorder);
136 void nlib_atomic_store64(int64_t* ptr, int64_t val, int memorder);
137 int64_t nlib_atomic_exchange64(int64_t* ptr, int64_t val, int memorder);
138 int nlib_atomic_compare_exchange64(int64_t* ptr, int64_t* expected,
139  int64_t desired, int weak,
140  int success_memorder, int failure_memorder);
141 int64_t nlib_atomic_add_fetch64(int64_t* ptr, int64_t val, int memorder);
142 int64_t nlib_atomic_sub_fetch64(int64_t* ptr, int64_t val, int memorder);
143 int64_t nlib_atomic_and_fetch64(int64_t* ptr, int64_t val, int memorder);
144 int64_t nlib_atomic_xor_fetch64(int64_t* ptr, int64_t val, int memorder);
145 int64_t nlib_atomic_or_fetch64(int64_t* ptr, int64_t val, int memorder);
146 int64_t nlib_atomic_fetch_add64(int64_t* ptr, int64_t val, int memorder);
147 int64_t nlib_atomic_fetch_sub64(int64_t* ptr, int64_t val, int memorder);
148 int64_t nlib_atomic_fetch_and64(int64_t* ptr, int64_t val, int memorder);
149 int64_t nlib_atomic_fetch_xor64(int64_t* ptr, int64_t val, int memorder);
150 int64_t nlib_atomic_fetch_or64(int64_t* ptr, int64_t val, int memorder);
151 static NLIB_ALWAYS_INLINE void* nlib_atomic_exchangeptr(void** ptr, void* val,
152  int memorder) {
153  return (void*)nlib_atomic_exchange32((int32_t*)ptr, (int32_t)val, memorder); // NOLINT
154 }
155 
156 void* nlib_atomic_loadptr(void* const* ptr, int memorder);
157 void nlib_atomic_storeptr(void** ptr, void* val, int memorder);
158 int nlib_atomic_compare_exchangeptr(void** ptr, void** expected,
159  void* desired, int weak,
160  int success_memorder, int failure_memorder);
161 
162 static NLIB_ALWAYS_INLINE void nlib_atomic_thread_fence(int memorder) {
163  if (memorder != NLIB_ATOMIC_RELAXED)
164  nlib_ctr_barrier();
165 }
166 
167 #define NLIB_FD_O_RDONLY (0x0000)
168 #define NLIB_FD_O_WRONLY (0x0001)
169 #define NLIB_FD_O_RDWR (0x0002)
170 #define NLIB_FD_O_APPEND (0x0008)
171 #define NLIB_FD_O_CREAT (0x0100)
172 #define NLIB_FD_O_TRUNC (0x0200)
173 #define NLIB_FD_O_EXCL (0x0400)
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #define NLIB_NOEXCEPT
180 #ifndef NLIB_HAS_ZLIB
181 # define NLIB_HAS_ZLIB
182 #endif
183 
184 #endif
185 #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...