osWindows.h 14.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program 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.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

S
slguan 已提交
16 17
#ifndef TDENGINE_OS_WINDOWS_H
#define TDENGINE_OS_WINDOWS_H
S
Shengliang Guan 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <locale.h>
#include <intrin.h>
#include <io.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "winsock2.h"
#include <WS2tcpip.h>
#include <winbase.h>
#include <Winsock2.h>
S
TD-1057  
Shengliang Guan 已提交
40 41
#include <time.h>
#include <inttypes.h>
S
TD-1244  
Shengliang Guan 已提交
42
#include <conio.h>
S
Shengliang Guan 已提交
43
#include <math.h>
S
TD-1057  
Shengliang Guan 已提交
44 45 46
#include "msvcProcess.h"
#include "msvcDirect.h"
#include "msvcFcntl.h"
47
#include "msvcLibgen.h"
S
TD-1057  
Shengliang Guan 已提交
48
#include "msvcStdio.h"
S
TD-1057  
Shengliang Guan 已提交
49 50
#include "sys/msvcStat.h"
#include "sys/msvcTypes.h"
S
Shengliang Guan 已提交
51 52 53 54 55

#ifdef __cplusplus
extern "C" {
#endif

S
slguan 已提交
56 57 58 59 60 61
#define TAOS_OS_FUNC_LZ4
  int32_t BUILDIN_CLZL(uint64_t val);
  int32_t BUILDIN_CLZ(uint32_t val);
  int32_t BUILDIN_CTZL(uint64_t val);
  int32_t BUILDIN_CTZ(uint32_t val);

S
slguan 已提交
62
#define TAOS_OS_FUNC_FILE
S
TD-1037  
Shengliang Guan 已提交
63 64 65
#define TAOS_OS_FUNC_FILE_ISREG
#define TAOS_OS_FUNC_FILE_ISDIR
#define TAOS_OS_FUNC_FILE_ISLNK
S
slguan 已提交
66 67
#define TAOS_OS_FUNC_FILE_SENDIFLE
#define TAOS_OS_FUNC_FILE_GETTMPFILEPATH
S
TD-1912  
Shengliang Guan 已提交
68
#define TAOS_OS_FUNC_FILE_FTRUNCATE 
S
slguan 已提交
69 70 71 72 73 74 75 76 77 78 79

#define TAOS_OS_FUNC_MATH
  #define SWAP(a, b, c)      \
    do {                     \
      c __tmp = (c)(a);      \
      (a) = (c)(b);          \
      (b) = __tmp;           \
    } while (0)
  #define MAX(a,b)  (((a)>(b))?(a):(b))
  #define MIN(a,b)  (((a)<(b))?(a):(b))

S
slguan 已提交
80 81 82 83 84
#define TAOS_OS_FUNC_SEMPHONE_PTHREAD

#define TAOS_OS_FUNC_SOCKET
#define TAOS_OS_FUNC_SOCKET_SETSOCKETOPT
#define TAOS_OS_FUNC_SOCKET_OP
S
TD-1057  
Shengliang Guan 已提交
85 86 87 88
  #define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags)
  #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen)
  #define taosWriteSocket(fd, buf, len) send((SOCKET)fd, buf, len, 0)
  #define taosReadSocket(fd, buf, len) recv((SOCKET)fd, buf, len, 0)
S
TD-1090  
Shengliang Guan 已提交
89
  #define taosCloseSocketNoCheck(fd) closesocket((SOCKET)fd)
S
TD-1057  
Shengliang Guan 已提交
90 91 92 93
  #define taosCloseSocket(fd) closesocket((SOCKET)fd)
typedef SOCKET eventfd_t; 
#define eventfd(a, b) -1

S
TD-1057  
Shengliang Guan 已提交
94 95
#define TAOS_OS_DEF_EPOLL
  #define TAOS_EPOLL_WAIT_TIME 100
96 97 98 99 100 101
  typedef SOCKET EpollFd; 
  #define EpollClose(pollFd) epoll_close(pollFd) 
  
#ifndef EPOLLWAKEUP
  #define EPOLLWAKEUP (1u << 29)
#endif
S
TD-1057  
Shengliang Guan 已提交
102

S
TD-1057  
Shengliang Guan 已提交
103 104 105
#define TAOS_OS_DEF_ZU
  #define PRIzu "ld"  

S
slguan 已提交
106
#define TAOS_OS_FUNC_STRING_WCHAR
107
  int twcslen(const wchar_t *wcs);
S
slguan 已提交
108 109 110 111 112 113 114 115 116 117
#define TAOS_OS_FUNC_STRING_GETLINE
#define TAOS_OS_FUNC_STRING_STR2INT64
  #ifdef _TD_GO_DLL_
    int64_t tsosStr2int64(char *str);
    uint64_t htonll(uint64_t val);
  #else
    #define tsosStr2int64 _atoi64
  #endif
#define TAOS_OS_FUNC_STRING_STRDUP
  #define taosStrdupImp(str) _strdup(str)
118
  #define taosStrndupImp(str, size) _strndup(str, size)  
S
slguan 已提交
119

S
TD-1057  
Shengliang Guan 已提交
120 121 122
char *stpcpy (char *dest, const char *src);
char *stpncpy (char *dest, const char *src, size_t n);

S
slguan 已提交
123 124 125
#define TAOS_OS_FUNC_SYSINFO

#define TAOS_OS_FUNC_TIME_DEF
S
slguan 已提交
126 127 128 129 130 131
  #ifdef _TD_GO_DLL_
    #define MILLISECOND_PER_SECOND (1000LL)
  #else
    #define MILLISECOND_PER_SECOND (1000i64)
  #endif

S
slguan 已提交
132 133 134 135
#define TAOS_OS_FUNC_TIMER_SLEEP
#define TAOS_OS_FUNC_TIMER

// specific
S
slguan 已提交
136
typedef int (*__compar_fn_t)(const void *, const void *);
S
slguan 已提交
137 138 139 140 141 142 143 144 145 146
#define ssize_t int
#define bzero(ptr, size) memset((ptr), 0, (size))
#define strcasecmp  _stricmp
#define strncasecmp _strnicmp
#define wcsncasecmp _wcsnicmp
#define strtok_r strtok_s
#define snprintf _snprintf
#define in_addr_t unsigned long
#define socklen_t int
#define htobe64 htonll
S
Shengliang Guan 已提交
147
#define getpid _getpid
S
slguan 已提交
148

S
slguan 已提交
149 150 151 152 153 154 155
struct tm *localtime_r(const time_t *timep, struct tm *result);
char *     strptime(const char *buf, const char *fmt, struct tm *tm);
char *     strsep(char **stringp, const char *delim);
char *     getpass(const char *prefix);
int        flock(int fd, int option);
int        fsync(int filedes);
char *     strndup(const char *s, size_t n);
S
TD-1057  
Shengliang Guan 已提交
156
char *     dirname(char *pszPathname);
S
TD-1057  
Shengliang Guan 已提交
157
int        gettimeofday(struct timeval *ptv, void *pTimeZone);
S
slguan 已提交
158

S
Shengliang Guan 已提交
159 160 161 162 163 164 165 166 167 168
// for access function in io.h
#define F_OK 00  //Existence only
#define W_OK 02  //Write - only
#define R_OK 04  //Read - only
#define X_OK 06  //Read and write

// for send function in tsocket.c
#define MSG_NOSIGNAL             0
#define SO_NO_CHECK              0x1234
#define SOL_TCP                  0x1234
169 170 171 172 173 174 175 176 177 178 179 180 181 182

#ifndef TCP_KEEPCNT
  #define TCP_KEEPCNT              0x1234
#endif

#ifndef TCP_KEEPIDLE
  #define TCP_KEEPIDLE             0x1234
#endif

#ifndef TCP_KEEPINTVL
  #define TCP_KEEPINTVL            0x1234
#endif

#ifdef _MSC_VER
183
//#if _MSC_VER >= 1900
184
  #define TAOS_OS_FUNC_SOCKET_INET
185
//#endif
186 187
#endif

188 189 190
#define SHUT_RDWR                SD_BOTH
#define SHUT_RD                  SD_RECEIVE
#define SHUT_WR                  SD_SEND
S
Shengliang Guan 已提交
191 192 193 194 195 196 197 198 199

#define LOCK_EX 1
#define LOCK_NB 2
#define LOCK_UN 3

#ifndef PATH_MAX
  #define PATH_MAX 256
#endif

S
TD-1207  
Shengliang Guan 已提交
200 201
#define TAOS_OS_FUNC_SIGNAL

S
Shengliang Guan 已提交
202
typedef struct {
S
slguan 已提交
203
  int    we_wordc;
S
Shengliang Guan 已提交
204
  char **we_wordv;
S
slguan 已提交
205 206
  int    we_offs;
  char   wordPos[20];
S
Shengliang Guan 已提交
207
} wordexp_t;
S
slguan 已提交
208
int  wordexp(const char *words, wordexp_t *pwordexp, int flags);
S
Shengliang Guan 已提交
209 210
void wordfree(wordexp_t *pwordexp);

S
TD-1207  
Shengliang Guan 已提交
211 212 213 214 215 216
#define openlog(a, b, c)
#define closelog()
#define LOG_ERR 0
#define LOG_INFO 1
void syslog(int unused, const char *format, ...);

S
slguan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#define TAOS_OS_FUNC_ATOMIC
  #define atomic_load_8(ptr) (*(char volatile*)(ptr))
  #define atomic_load_16(ptr) (*(short volatile*)(ptr))
  #define atomic_load_32(ptr) (*(long volatile*)(ptr))
  #define atomic_load_64(ptr) (*(__int64 volatile*)(ptr))
  #define atomic_load_ptr(ptr) (*(void* volatile*)(ptr))

  #define atomic_store_8(ptr, val) ((*(char volatile*)(ptr)) = (char)(val))
  #define atomic_store_16(ptr, val) ((*(short volatile*)(ptr)) = (short)(val))
  #define atomic_store_32(ptr, val) ((*(long volatile*)(ptr)) = (long)(val))
  #define atomic_store_64(ptr, val) ((*(__int64 volatile*)(ptr)) = (__int64)(val))
  #define atomic_store_ptr(ptr, val) ((*(void* volatile*)(ptr)) = (void*)(val))

  #define atomic_exchange_8(ptr, val) _InterlockedExchange8((char volatile*)(ptr), (char)(val))
  #define atomic_exchange_16(ptr, val) _InterlockedExchange16((short volatile*)(ptr), (short)(val))
  #define atomic_exchange_32(ptr, val) _InterlockedExchange((long volatile*)(ptr), (long)(val))
  #define atomic_exchange_64(ptr, val) _InterlockedExchange64((__int64 volatile*)(ptr), (__int64)(val))
S
Shengliang Guan 已提交
234
  #ifdef _WIN64 
235 236 237 238 239
    #define atomic_exchange_ptr(ptr, val) _InterlockedExchangePointer((void* volatile*)(ptr), (void*)(val)) 
  #else
    #define atomic_exchange_ptr(ptr, val) _InlineInterlockedExchangePointer((void* volatile*)(ptr), (void*)(val))
  #endif
  
S
slguan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  #ifdef _TD_GO_DLL_
    #define atomic_val_compare_exchange_8 __sync_val_compare_and_swap
  #else
    #define atomic_val_compare_exchange_8(ptr, oldval, newval) _InterlockedCompareExchange8((char volatile*)(ptr), (char)(newval), (char)(oldval))
  #endif
  #define atomic_val_compare_exchange_16(ptr, oldval, newval) _InterlockedCompareExchange16((short volatile*)(ptr), (short)(newval), (short)(oldval))
  #define atomic_val_compare_exchange_32(ptr, oldval, newval) _InterlockedCompareExchange((long volatile*)(ptr), (long)(newval), (long)(oldval))
  #define atomic_val_compare_exchange_64(ptr, oldval, newval) _InterlockedCompareExchange64((__int64 volatile*)(ptr), (__int64)(newval), (__int64)(oldval))
  #define atomic_val_compare_exchange_ptr(ptr, oldval, newval) _InterlockedCompareExchangePointer((void* volatile*)(ptr), (void*)(newval), (void*)(oldval))

  char    interlocked_add_fetch_8(char volatile *ptr, char val);
  short   interlocked_add_fetch_16(short volatile *ptr, short val);
  long    interlocked_add_fetch_32(long volatile *ptr, long val);
  __int64 interlocked_add_fetch_64(__int64 volatile *ptr, __int64 val);

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  char interlocked_and_fetch_8(char volatile* ptr, char val);
  short interlocked_and_fetch_16(short volatile* ptr, short val);
  long interlocked_and_fetch_32(long volatile* ptr, long val);
  __int64 interlocked_and_fetch_64(__int64 volatile* ptr, __int64 val);

  __int64 interlocked_fetch_and_64(__int64 volatile* ptr, __int64 val);

  char interlocked_or_fetch_8(char volatile* ptr, char val);
  short interlocked_or_fetch_16(short volatile* ptr, short val);
  long interlocked_or_fetch_32(long volatile* ptr, long val);
  __int64 interlocked_or_fetch_64(__int64 volatile* ptr, __int64 val);

  char interlocked_xor_fetch_8(char volatile* ptr, char val);
  short interlocked_xor_fetch_16(short volatile* ptr, short val);
  long interlocked_xor_fetch_32(long volatile* ptr, long val);
  __int64 interlocked_xor_fetch_64(__int64 volatile* ptr, __int64 val);

  __int64 interlocked_fetch_xor_64(__int64 volatile* ptr, __int64 val);

S
slguan 已提交
274 275 276 277 278 279 280 281 282 283
  #define atomic_add_fetch_8(ptr, val) interlocked_add_fetch_8((char volatile*)(ptr), (char)(val))
  #define atomic_add_fetch_16(ptr, val) interlocked_add_fetch_16((short volatile*)(ptr), (short)(val))
  #define atomic_add_fetch_32(ptr, val) interlocked_add_fetch_32((long volatile*)(ptr), (long)(val))
  #define atomic_add_fetch_64(ptr, val) interlocked_add_fetch_64((__int64 volatile*)(ptr), (__int64)(val))
  #ifdef _TD_GO_DLL_
    #define atomic_fetch_add_8 __sync_fetch_and_ad
    #define atomic_fetch_add_16 __sync_fetch_and_add
  #else
    #define atomic_fetch_add_8(ptr, val) _InterlockedExchangeAdd8((char volatile*)(ptr), (char)(val))
    #define atomic_fetch_add_16(ptr, val) _InterlockedExchangeAdd16((short volatile*)(ptr), (short)(val))
284 285 286
  #endif  
  #define atomic_fetch_add_8(ptr, val) _InterlockedExchangeAdd8((char volatile*)(ptr), (char)(val))
  #define atomic_fetch_add_16(ptr, val) _InterlockedExchangeAdd16((short volatile*)(ptr), (short)(val))
S
slguan 已提交
287 288
  #define atomic_fetch_add_32(ptr, val) _InterlockedExchangeAdd((long volatile*)(ptr), (long)(val))
  #define atomic_fetch_add_64(ptr, val) _InterlockedExchangeAdd64((__int64 volatile*)(ptr), (__int64)(val))
289
  
S
slguan 已提交
290 291 292 293 294 295 296 297 298 299
  #define atomic_sub_fetch_8(ptr, val) interlocked_add_fetch_8((char volatile*)(ptr), -(char)(val))
  #define atomic_sub_fetch_16(ptr, val) interlocked_add_fetch_16((short volatile*)(ptr), -(short)(val))
  #define atomic_sub_fetch_32(ptr, val) interlocked_add_fetch_32((long volatile*)(ptr), -(long)(val))
  #define atomic_sub_fetch_64(ptr, val) interlocked_add_fetch_64((__int64 volatile*)(ptr), -(__int64)(val))

  #define atomic_fetch_sub_8(ptr, val) _InterlockedExchangeAdd8((char volatile*)(ptr), -(char)(val))
  #define atomic_fetch_sub_16(ptr, val) _InterlockedExchangeAdd16((short volatile*)(ptr), -(short)(val))
  #define atomic_fetch_sub_32(ptr, val) _InterlockedExchangeAdd((long volatile*)(ptr), -(long)(val))
  #define atomic_fetch_sub_64(ptr, val) _InterlockedExchangeAdd64((__int64 volatile*)(ptr), -(__int64)(val))

300 301
  #define atomic_and_fetch_8(ptr, val) interlocked_and_fetch_8((char volatile*)(ptr), (char)(val))
  #define atomic_and_fetch_16(ptr, val) interlocked_and_fetch_16((short volatile*)(ptr), (short)(val))
S
slguan 已提交
302 303 304
  #define atomic_and_fetch_32(ptr, val) interlocked_and_fetch_32((long volatile*)(ptr), (long)(val))
  #define atomic_and_fetch_64(ptr, val) interlocked_and_fetch_64((__int64 volatile*)(ptr), (__int64)(val))

305 306 307 308
  #define atomic_fetch_and_8(ptr, val) _InterlockedAnd8((char volatile*)(ptr), (char)(val))
  #define atomic_fetch_and_16(ptr, val) _InterlockedAnd16((short volatile*)(ptr), (short)(val))
  #define atomic_fetch_and_32(ptr, val) _InterlockedAnd((long volatile*)(ptr), (long)(val))
  #define atomic_fetch_and_64(ptr, val) interlocked_fetch_and_64((__int64 volatile*)(ptr), (__int64)(val))
S
slguan 已提交
309

310 311
  #define atomic_or_fetch_8(ptr, val) interlocked_or_fetch_8((char volatile*)(ptr), (char)(val))
  #define atomic_or_fetch_16(ptr, val) interlocked_or_fetch_16((short volatile*)(ptr), (short)(val))
S
slguan 已提交
312 313 314
  #define atomic_or_fetch_32(ptr, val) interlocked_or_fetch_32((long volatile*)(ptr), (long)(val))
  #define atomic_or_fetch_64(ptr, val) interlocked_or_fetch_64((__int64 volatile*)(ptr), (__int64)(val))

315 316 317 318
  #define atomic_fetch_or_8(ptr, val) _InterlockedOr8((char volatile*)(ptr), (char)(val))
  #define atomic_fetch_or_16(ptr, val) _InterlockedOr16((short volatile*)(ptr), (short)(val))
  #define atomic_fetch_or_32(ptr, val) _InterlockedOr((long volatile*)(ptr), (long)(val))
  #define atomic_fetch_or_64(ptr, val) interlocked_fetch_or_64((__int64 volatile*)(ptr), (__int64)(val))
S
slguan 已提交
319

320 321
  #define atomic_xor_fetch_8(ptr, val) interlocked_xor_fetch_8((char volatile*)(ptr), (char)(val))
  #define atomic_xor_fetch_16(ptr, val) interlocked_xor_fetch_16((short volatile*)(ptr), (short)(val))
S
slguan 已提交
322 323 324
  #define atomic_xor_fetch_32(ptr, val) interlocked_xor_fetch_32((long volatile*)(ptr), (long)(val))
  #define atomic_xor_fetch_64(ptr, val) interlocked_xor_fetch_64((__int64 volatile*)(ptr), (__int64)(val))

325 326
  #define atomic_fetch_xor_8(ptr, val) _InterlockedXor8((char volatile*)(ptr), (char)(val))
  #define atomic_fetch_xor_16(ptr, val) _InterlockedXor16((short volatile*)(ptr), (short)(val))
S
slguan 已提交
327
  #define atomic_fetch_xor_32(ptr, val) _InterlockedXor((long volatile*)(ptr), (long)(val))
328
  #define atomic_fetch_xor_64(ptr, val) interlocked_fetch_xor_64((__int64 volatile*)(ptr), (__int64)(val))
S
slguan 已提交
329 330

  #ifdef _WIN64
331 332 333 334 335 336 337 338 339
    #define atomic_add_fetch_ptr atomic_add_fetch_64
    #define atomic_fetch_add_ptr atomic_fetch_add_64
    #define atomic_sub_fetch_ptr atomic_sub_fetch_64
    #define atomic_fetch_sub_ptr atomic_fetch_sub_64
    #define atomic_and_fetch_ptr atomic_and_fetch_64
    #define atomic_fetch_and_ptr atomic_fetch_and_64
    #define atomic_or_fetch_ptr  atomic_or_fetch_64
    #define atomic_fetch_or_ptr  atomic_fetch_or_64
    #define atomic_xor_fetch_ptr atomic_xor_fetch_64
S
slguan 已提交
340 341
    #define atomic_fetch_xor_ptr atomic_fetch_xor_64
  #else
342 343 344 345 346 347 348 349 350
    #define atomic_add_fetch_ptr atomic_add_fetch_32
    #define atomic_fetch_add_ptr atomic_fetch_add_32
    #define atomic_sub_fetch_ptr atomic_sub_fetch_32
    #define atomic_fetch_sub_ptr atomic_fetch_sub_32
    #define atomic_and_fetch_ptr atomic_and_fetch_32
    #define atomic_fetch_and_ptr atomic_fetch_and_32
    #define atomic_or_fetch_ptr  atomic_or_fetch_32
    #define atomic_fetch_or_ptr  atomic_fetch_or_32
    #define atomic_xor_fetch_ptr atomic_xor_fetch_32
S
slguan 已提交
351 352
    #define atomic_fetch_xor_ptr atomic_fetch_xor_32
  #endif
S
Shengliang Guan 已提交
353 354 355
#ifdef __cplusplus
}
#endif
S
TD-1530  
Shengliang Guan 已提交
356
#endif