46 #define _WIN32_WINNT 0x0502
49 #ifndef WIN32_LEAN_AND_MEAN
50 #define WIN32_LEAN_AND_MEAN
56 #include <netinet/in.h>
58 #include <semaphore.h>
64#error Either _WIN32 or _LINUX must be defined.
77 #define CDI_STDIN GetStdHandle(STD_INPUT_HANDLE)
78 #define CDI_STDOUT GetStdHandle(STD_OUTPUT_HANDLE)
79 #define CDI_STDERR GetStdHandle(STD_ERROR_HANDLE)
81 #define CDI_STDIN_FILENO _fileno(stdin)
82 #define CDI_STDOUT_FILENO _fileno(stdout)
83 #define CDI_STDERR_FILENO _fileno(stderr)
86 #define CdiThreadFuncName LPTHREAD_START_ROUTINE
87 #define CDI_THREAD DWORD WINAPI
88 #define CDI_THREAD_PARAM LPVOID
99 typedef CRITICAL_SECTION*
CdiCsID;
104 #define CDI_INFINITE INFINITE
107 #define CdiOsAtomicInc16(x) InterlockedIncrement16(x)
108 #define CdiOsAtomicDec16(x) InterlockedDecrement16(x)
109 #define CdiOsAtomicRead16(x) InterlockedAdd16((x), 0)
110 #define CdiOsAtomicAdd16(x, b) InterlockedAdd16((x), (b))
113 #define CdiOsAtomicInc32(x) InterlockedIncrement(x)
114 #define CdiOsAtomicDec32(x) InterlockedDecrement(x)
115 #define CdiOsAtomicRead32(x) InterlockedAdd((x), 0)
116 #define CdiOsAtomicAdd32(x, b) InterlockedAdd((x), (b))
119 #define CdiOsAtomicInc64(x) InterlockedIncrement64(x)
120 #define CdiOsAtomicDec64(x) InterlockedDecrement64(x)
121 #define CdiOsAtomicRead64(x) InterlockedAdd64((x), 0)
122 #define CdiOsAtomicAdd64(x, b) InterlockedAdd64((x), (b))
127 #define CdiOsAtomicLoad16(x) *(volatile uint16_t*)(x)
128 #define CdiOsAtomicStore16(x, v) *volatile (uint16_t*)(x) = (v)
129 #define CdiOsAtomicLoad32(x) *(volatile uint32_t*)(x)
130 #define CdiOsAtomicStore32(x, v) *(volatile uint32_t*)(x) = (v)
131 #define CdiOsAtomicLoad64(x) *(volatile uint64_t*)(x)
132 #define CdiOsAtomicStore64(x, v) *(volatile uint64_t*)(x) = (v)
133 #define CdiOsAtomicLoadPointer(x) *(volatile void**)(x)
134 #define CdiOsAtomicStorePointer(x, v) *(volatile void**)(x) = (v)
136 #define CDI_INVALID_HANDLE_VALUE INVALID_HANDLE_VALUE
138 typedef struct siginfo_t siginfo_t;
148 #define CDI_STATIC_MUTEX_INITIALIZER NULL
149 #define CdiOsStaticMutexLock(x) StaticMutexLockWin32(&(x))
152 static inline void StaticMutexLockWin32(
volatile HANDLE* x) {
154 HANDLE tmp = CreateMutex(NULL, FALSE, NULL);
155 if (InterlockedCompareExchangePointer((PVOID*)x, (PVOID)tmp, NULL) != NULL) {
159 WaitForSingleObject(*x, INFINITE);
161 #define CdiOsStaticMutexUnlock(x) (ReleaseMutex(x) == 0)
164 #define CDI_HUGE_PAGES_BYTE_SIZE (1)
167 #define CDI_STATIC_ASSERT(condition, message) static_assert(condition, message)
170 #define CDI_STDIN stdin
171 #define CDI_STDOUT stdout
172 #define CDI_STDERR stderr
174 #define CDI_STDIN_FILENO STDIN_FILENO
175 #define CDI_STDOUT_FILENO STDOUT_FILENO
176 #define CDI_STDERR_FILENO STDERR_FILENO
178 #define CDI_THREAD_PARAM void*
179 #define CDI_THREAD int
197 #define CDI_INFINITE 0xFFFFFFFF
200 #define CdiOsAtomicInc16(x) __sync_add_and_fetch((x), 1)
202 #define CdiOsAtomicDec16(x) __sync_sub_and_fetch((x), 1)
204 #define CdiOsAtomicRead16(x) __sync_add_and_fetch((x), 0)
206 #define CdiOsAtomicAdd16(x, b) __sync_add_and_fetch((x), (b))
209 #define CdiOsAtomicInc32(x) __sync_add_and_fetch((x), 1)
211 #define CdiOsAtomicDec32(x) __sync_sub_and_fetch((x), 1)
213 #define CdiOsAtomicRead32(x) __sync_add_and_fetch((x), 0)
215 #define CdiOsAtomicAdd32(x, b) __sync_add_and_fetch((x), (b))
218 #define CdiOsAtomicInc64(x) __sync_add_and_fetch((x), 1)
220 #define CdiOsAtomicDec64(x) __sync_sub_and_fetch((x), 1)
222 #define CdiOsAtomicRead64(x) __sync_add_and_fetch((x), 0)
224 #define CdiOsAtomicAdd64(x, b) __sync_add_and_fetch((x), (b))
235 #define CdiOsAtomicLoad16(x) __atomic_load_n((x), __ATOMIC_CONSUME)
237 #define CdiOsAtomicLoad32(x) __atomic_load_n((x), __ATOMIC_CONSUME)
239 #define CdiOsAtomicLoad64(x) __atomic_load_n((x), __ATOMIC_CONSUME)
241 #define CdiOsAtomicLoadPointer(x) __atomic_load_n((x), __ATOMIC_CONSUME)
251 #define CdiOsAtomicStore16(x, v) __atomic_store_n((x), (v), __ATOMIC_RELEASE)
253 #define CdiOsAtomicStore32(x, v) __atomic_store_n((x), (v), __ATOMIC_RELEASE)
255 #define CdiOsAtomicStore64(x, v) __atomic_store_n((x), (v), __ATOMIC_RELEASE)
257 #define CdiOsAtomicStorePointer(x, v) __atomic_store_n((x), (v), __ATOMIC_RELEASE)
260 #define CDI_INVALID_HANDLE_VALUE -1
274 #define CDI_STATIC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
276 #define CdiOsStaticMutexLock(x) pthread_mutex_lock(&(x))
278 #define CdiOsStaticMutexUnlock(x) pthread_mutex_unlock(&(x))
283 #define CDI_HUGE_PAGES_BYTE_SIZE (2 * 1024 * 1024)
289#define CDI_STATIC_ASSERT(condition, message) _Static_assert(condition, message)
292#define CDI_MAX_THREAD_NAME (50)
293#define CDI_OS_SIG_TIMEOUT (0xFFFFFFFF)
296#define CDI_MAX_WAIT_MULTIPLE (64)
299#define CDI_OS_SOCKET_MAX_IOVCNT (10)
318#define CDI_MAX_SIGNAL_HANDLERS (10)
321#define CDI_MAX_FORMATTED_TIMEZONE_STRING_LENGTH (128)
358 const char* thread_name_str,
void* thread_func_arg_ptr,
374 const char* thread_name_str,
void* thread_func_arg_ptr,
CdiSignalType start_signal)
376 return CdiOsThreadCreatePinned(thread_func, thread_id_out_ptr, thread_name_str, thread_func_arg_ptr, start_signal,
529#define CdiOsStrTokR strtok_s
532#define CdiOsStrTokR strtok_r
616 uint32_t timeout_in_ms, uint32_t* ret_signal_index_ptr);
761 char* directory_str,
int directory_buf_size);
800#define CdiOsStrCaseCmp _stricmp
801#define CdiOsStrNCaseCmp _strnicmp
804#define CdiOsStrNCaseCmp strncasecmp
806#define CdiOsStrCaseCmp strcasecmp
819#define CdiOsStrCmp strcmp
831#define CdiOsStrNCmp strncmp
845#define CdiOsGetMilliseconds() (CdiOsGetMicroseconds()/1000)
974 struct sockaddr_in* source_address_ptr);
1009 const struct sockaddr_in* destination_address_ptr,
int* byte_count_ptr);
CDI_INTERFACE bool CdiOsThreadSetData(CdiThreadData handle, void *content_ptr)
Definition os_linux.c:397
pthread_key_t CdiThreadData
Define portable thread data type.
Definition cdi_os_api.h:183
CDI_INTERFACE bool CdiOsEnvironmentVariableSet(const char *name_str, const char *value_str)
Definition os_linux.c:1456
CDI_INTERFACE void CdiOsCritSectionReserve(CdiCsID cs_handle)
Definition os_linux.c:579
CDI_INTERFACE bool CdiOsSignalWait(CdiSignalType signal_handle, uint32_t timeout_in_ms, bool *timed_out_ptr)
Definition os_linux.c:700
CDI_INTERFACE bool CdiOsSemaphoreRelease(CdiSemID sem_handle)
Definition os_linux.c:512
CDI_INTERFACE void CdiOsMemFree(void *mem_ptr)
Definition os_linux.c:937
CDI_INTERFACE bool CdiOsWrite(CdiFileID file_handle, const void *data_ptr, uint32_t byte_count)
Definition os_linux.c:1043
CDI_INTERFACE bool CdiOsSignalCreate(CdiSignalType *signal_handle_ptr)
Definition os_linux.c:600
struct CdiSignalType_t * CdiSignalType
Define portable signal type. Don't use void* here, which prevents the compiler from type checking.
Definition cdi_os_api.h:189
CDI_INTERFACE uint64_t CdiOsGetMicroseconds(void)
Timers get a microsecond timestamp from CLOCK_MONOTONIC on linux or from the performance counter on W...
Definition os_linux.c:1203
CDI_INTERFACE bool CdiOsOpenForWrite(const char *file_name_str, CdiFileID *file_handle_ptr)
Definition os_linux.c:969
CDI_INTERFACE bool CdiOsSignalHandlerSet(int signal_num, CdiSignalHandlerFunction func_ptr)
Definition os_linux.c:285
CDI_INTERFACE bool CdiOsCritSectionDelete(CdiCsID cs_handle)
Definition os_linux.c:589
CDI_INTERFACE bool CdiOsSignalClear(CdiSignalType signal_handle)
Definition os_linux.c:641
CDI_INTERFACE bool CdiOsSocketClose(CdiSocket socket_handle)
Definition os_linux.c:1369
pthread_mutex_t * CdiCsID
Define portable critical section.
Definition cdi_os_api.h:192
CDI_INTERFACE bool CdiOsSignalsWait(CdiSignalType *signal_array, uint8_t num_signals, bool wait_all, uint32_t timeout_in_ms, uint32_t *ret_signal_index_ptr)
Definition os_linux.c:756
CDI_INTERFACE int CdiOsGetLocalTimeString(char *time_str, int max_string_len)
Definition os_linux.c:1227
static bool CdiOsThreadCreate(CdiThreadFuncName thread_func, CdiThreadID *thread_id_out_ptr, const char *thread_name_str, void *thread_func_arg_ptr, CdiSignalType start_signal)
Definition cdi_os_api.h:373
CDI_INTERFACE bool CdiOsThreadFreeData(CdiThreadData handle)
Definition os_linux.c:392
CDI_INTERFACE bool CdiOsSocketOpen(const char *host_address_str, int port_number, const char *bind_address_str, CdiSocket *new_socket_ptr)
Definition os_linux.c:1270
CDI_INTERFACE void CdiOsGetLocalTime(struct tm *local_time_ret_ptr)
Definition os_linux.c:1219
CDI_INTERFACE bool CdiOsThreadJoin(CdiThreadID thread_id, uint32_t timeout_in_ms, bool *timed_out_ptr)
Definition os_linux.c:421
CDI_INTERFACE void CdiOsSleepMicroseconds(uint32_t microseconds)
Definition os_linux.c:1195
CDI_INTERFACE void CdiOsMemFreeHugePage(void *mem_ptr, int64_t mem_size)
Definition os_linux.c:961
FILE * CdiFileID
Define portable File ID type.
Definition cdi_os_api.h:195
CDI_INTERFACE bool CdiOsSemaphoreDelete(CdiSemID sem_handle)
Definition os_linux.c:491
CDI_INTERFACE bool CdiOsSocketWrite(CdiSocket socket_handle, struct iovec *iov, int iovcnt, int *byte_count_ptr)
Definition os_linux.c:1430
CDI_INTERFACE int CdiOsStrCpy(char *dest_str, uint32_t max_str_len, const char *src_str)
Definition os_linux.c:1167
void(* CdiSignalHandlerFunction)(int sig, siginfo_t *siginfo, void *context)
Type used for signal handler.
Definition cdi_os_api.h:302
CDI_INTERFACE void CdiOsUseLogger(void)
Definition os_linux.c:279
CDI_INTERFACE void * CdiOsMemAlloc(int64_t mem_size)
Definition os_linux.c:915
CDI_INTERFACE bool CdiOsFTell(CdiFileID file_handle, uint64_t *current_position_ptr)
Definition os_linux.c:1061
struct CdiSocket_t * CdiSocket
Define portable socket type.
Definition cdi_os_api.h:315
CDI_INTERFACE bool CdiOsThreadAllocData(CdiThreadData *handle_out_ptr)
Definition os_linux.c:387
CDI_INTERFACE bool CdiOsSocketReadFrom(CdiSocket socket_handle, void *buffer_ptr, int *byte_count_ptr, struct sockaddr_in *source_address_ptr)
Definition os_linux.c:1388
CDI_INTERFACE void CdiOsShutdown(void)
Definition os_linux.c:1466
CDI_INTERFACE bool CdiOsCritSectionCreate(CdiCsID *cs_handle_ptr)
Definition os_linux.c:557
CDI_INTERFACE bool CdiOsClose(CdiFileID file_handle)
Definition os_linux.c:995
#define CDI_THREAD_PARAM
Define portable thread function parameter type.
Definition cdi_os_api.h:178
CDI_INTERFACE bool CdiOsThreadCreatePinned(CdiThreadFuncName thread_func, CdiThreadID *thread_id_out_ptr, const char *thread_name_str, void *thread_func_arg_ptr, CdiSignalType start_signal, int cpu_affinity)
Definition os_linux.c:307
CDI_INTERFACE void CdiOsSleep(uint32_t milliseconds)
Definition os_linux.c:1181
CDI_INTERFACE bool CdiOsSignalDelete(CdiSignalType signal_handle)
Definition os_linux.c:624
CDI_INTERFACE void * CdiOsMemAllocZero(int64_t mem_size)
Definition os_linux.c:926
CDI_INTERFACE bool CdiOsIsPathWriteable(const char *directory_str)
Definition os_linux.c:1143
struct CdiThreadID_t * CdiThreadID
Define portable thread type. Separate name from type, otherwise the typedef that follows it will gene...
Definition cdi_os_api.h:312
CDI_INTERFACE bool CdiOsSignalReadState(CdiSignalType signal_handle)
Definition os_linux.c:695
CDI_INTERFACE bool CdiOsSignalGet(CdiSignalType signal_handle)
Definition os_linux.c:686
CDI_INTERFACE bool CdiOsSplitPath(const char *filepath_str, char *filename_str, int filename_buf_size, char *directory_str, int directory_buf_size)
Definition os_linux.c:1087
CDI_INTERFACE bool CdiOsSocketRead(CdiSocket socket_handle, void *buffer_ptr, int *byte_count_ptr)
Definition os_linux.c:1383
sem_t * CdiSemID
Define portable semaphore.
Definition cdi_os_api.h:186
CDI_INTERFACE void CdiOsCritSectionRelease(CdiCsID cs_handle)
Definition os_linux.c:584
CDI_INTERFACE bool CdiOsSemaphoreReserve(CdiSemID sem_handle, int timeout_in_ms)
Definition os_linux.c:526
CDI_INTERFACE bool CdiOsSocketWriteTo(CdiSocket socket_handle, struct iovec *iov, int iovcnt, const struct sockaddr_in *destination_address_ptr, int *byte_count_ptr)
Definition os_linux.c:1443
CDI_INTERFACE bool CdiOsRead(CdiFileID file_handle, void *buffer_ptr, uint32_t byte_count, uint32_t *bytes_read_ptr)
Definition os_linux.c:1008
pthread_mutex_t CdiStaticMutexType
Define portable static mutex type. An example implementation:
Definition cdi_os_api.h:272
CDI_INTERFACE bool CdiOsFSeek(CdiFileID file_handle, int64_t offset, int position)
Definition os_linux.c:1077
CDI_INTERFACE void CdiOsGetUtcTime(struct timespec *ret_time_ptr)
This is an OS call to get the current synced AWS network time in UTC format.
Definition os_linux.c:1214
CDI_INTERFACE bool CdiOsFlush(CdiFileID file_handle)
Definition os_linux.c:1056
CDI_INTERFACE bool CdiOsSignalSet(CdiSignalType signal_handle)
Definition os_linux.c:653
CDI_INTERFACE int CdiOsSemaphoreValueGet(CdiSemID sem_handle)
Definition os_linux.c:545
CDI_THREAD(* CdiThreadFuncName)(CDI_THREAD_PARAM)
Define portable thread function.
Definition cdi_os_api.h:180
CDI_INTERFACE bool CdiOsOpenForRead(const char *file_name_str, CdiFileID *file_handle_ptr)
Definition os_linux.c:981
CDI_INTERFACE void * CdiOsMemAllocHugePage(int64_t mem_size)
Definition os_linux.c:944
CDI_INTERFACE bool CdiOsThreadGetData(CdiThreadData handle, void **content_out_ptr)
Definition os_linux.c:406
CDI_INTERFACE bool CdiOsSocketGetPort(CdiSocket socket_handle, int *port_number_ptr)
Definition os_linux.c:1341
CDI_INTERFACE bool CdiOsSocketGetSockAddrIn(CdiSocket socket_handle, struct sockaddr_in *sockaddr_in_ptr)
Definition os_linux.c:1358
#define CDI_THREAD
Define portable thread function return type.
Definition cdi_os_api.h:179
CDI_INTERFACE bool CdiOsSemaphoreCreate(CdiSemID *ret_sem_handle_ptr, int sem_count)
Definition os_linux.c:463
CDI_INTERFACE const char * CdiOsThreadGetName(CdiThreadID thread_id)
Definition os_linux.c:415
The declarations in this header file correspond to the definitions in cdi_utility_api....
#define CDI_INTERFACE
Specify C linkage when compiling as C++ and define API interface export for Windows.
Definition cdi_utility_api.h:34
Structure used to hold signal handler data.
Definition cdi_os_api.h:305
CdiSignalHandlerFunction func_ptr
Pointer to signal handler.
Definition cdi_os_api.h:307
int signal_num
Signal number of the signal related to the handler.
Definition cdi_os_api.h:306