1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*
* Internal xtrx portable layer header file
* Copyright (c) 2017 Sergey Kostanbaev <sergey.kostanbaev@fairwaves.co>
* For more information, please visit: http://xtrx.io
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef XTRX_PORT_H
#define XTRX_PORT_H
/** @file xtrx_port.h
*
* Portability layer accross differrent platform and OSes
*/
#ifdef _MSC_VER
#if !defined(__cplusplus)
#define inline __inline
#endif
#endif
#if defined(__linux)
/* to use non-standard GNU-extenstions */
#define _GNU_SOURCE
#endif
#include <stdint.h>
//#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
//#define XTRX_API WINAPI
//#else
//#define XTRX_API
//#endif
#include <sys/types.h>
#if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
#include <sys/time.h>
#include <unistd.h>
#include <endian.h>
#endif
#include <time.h>
#include <limits.h>
#include <errno.h>
#if defined(__linux) || defined(__APPLE__)
#include <pthread.h>
#include <semaphore.h>
#elif defined(_WIN32) || defined(_WIN32_WCE) || defined(__CYGWIN__)
#include <windows.h>
/** @ingroup threading
* Basic emulation of posix threading and mutexes
*/
typedef CRITICAL_SECTION pthread_mutex_t;
static inline int pthread_mutex_init(pthread_mutex_t *__mutex, const void *__mutexattr) {
InitializeCriticalSection(__mutex);
return 0;
}
static inline int pthread_mutex_destroy(pthread_mutex_t *__mutex) {
DeleteCriticalSection(__mutex);
return 0;
}
static inline int pthread_mutex_lock(pthread_mutex_t *__mutex) {
EnterCriticalSection(__mutex);
return 0;
}
static inline int pthread_mutex_unlock(pthread_mutex_t *__mutex) {
LeaveCriticalSection(__mutex);
return 0;
}
typedef HANDLE pthread_t;
int pthread_create(pthread_t *__restrict nt,
const void *__restrict __attr,
void *(*start_routine) (void *),
void *__restrict arg);
int pthread_join(pthread_t th, void **thread_return);
typedef HANDLE sem_t;
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_destroy(sem_t *sem);
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
/** @ingroup timing
* Basic timing function with high resolution
*/
#ifdef _MSC_VER
struct timespec {
time_t tv_sec;
unsigned tv_nsec;
};
typedef enum {
CLOCK_REALTIME = 0,
CLOCK_MONOTONIC_RAW = 0, /**< Use Realtime as the last resort */
} clockid_t;
#else
#define CLOCK_MONOTONIC_RAW CLOCK_REALTIME
#endif
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int usleep(unsigned usec);
unsigned sleep(unsigned seconds);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
/** @ingroup dynamic
* Emulation of dl functions
*/
#define RTLD_LOCAL 0
#define RTLD_NOW 2
void *dlopen(const char *file, int mode);
int dlclose(void *handle);
void *dlsym(void *__restrict handle,
const char *__restrict name);
char *dlerror(void);
/** @ingroup endian
*/
static inline uint32_t __bswap_16 (uint16_t __bsx)
{
return (__bsx >> 8) | (__bsx << 8);
}
static inline uint32_t __bswap_32 (uint32_t __bsx)
{
return __builtin_bswap32(__bsx);
}
static inline uint64_t __bswap_64 (uint64_t __bsx)
{
return __builtin_bswap64(__bsx);
}
# define htobe16(x) __bswap_16 (x)
# define htole16(x) (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) (x)
# define htobe32(x) __bswap_32 (x)
# define htole32(x) (x)
# define be32toh(x) __bswap_32 (x)
# define le32toh(x) (x)
# define htobe64(x) __bswap_64 (x)
# define htole64(x) (x)
# define be64toh(x) __bswap_64 (x)
# define le64toh(x) (x)
/** @ingroup memory
*/
static inline int posix_memalign(void **memptr, size_t alignment, size_t size) {
*memptr = _aligned_malloc(size, alignment);
return (*memptr == NULL) ? -1 : 0;
}
// TODO replace this constant
#define ENAVAIL 1000
#else
#error Unknown platform
#endif
/** @ingroup general
* General portability layes
*/
#if defined(__cplusplus)
extern "C" {
#endif
const struct tm* xtrxll_localtime(time_t now);
#if defined(__cplusplus)
}
#endif
#endif
|