vendor/linenoise/libwin32fixes.c
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
/* * Modified by Henry Rawas (henryr@schakra.com) * - make it compatible with Visual Studio builds * - added wstrtod to handle INF, NAN * - added gettimeofday routine * - modified rename to retry after failure */ #ifdef _WIN32 #include <process.h> #include <stdlib.h> #include <errno.h> #ifndef FD_SETSIZE # define FD_SETSIZE 16000 #endif //#include <winsock2.h> #include <windows.h> #include <signal.h> #include <time.h> #include <locale.h> #include <math.h> #include "win32fixes.h" /* Redefined here to avoid redis.h so it can be used in other projects */ #define NOTUSED(V) ((void) V) #define THREAD_STACK_SIZE (1024*1024*4) /* Winsock requires library initialization on startup int w32initWinSock(void) { WSADATA t_wsa; WORD wVers; int iError; wVers = MAKEWORD(2, 2); iError = WSAStartup(wVers, &t_wsa); if(iError != NO_ERROR || LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2 ) { return 0; }; return 1; } */ /* Behaves as posix, works without ifdefs, makes compiler happy */ int sigaction(int sig, struct sigaction *in, struct sigaction *out) { NOTUSED(out); /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction * is used. Otherwise, sa_handler is used */ if (in->sa_flags & SA_SIGINFO) signal(sig, in->sa_sigaction); else signal(sig, in->sa_handler); return 0; } /* Terminates process, implemented only for SIGKILL */ int kill(pid_t pid, int sig) { if (sig == SIGKILL) { HANDLE h = OpenProcess(PROCESS_TERMINATE, 0, pid); if (!TerminateProcess(h, 127)) { errno = EINVAL; /* GetLastError() */ CloseHandle(h); return -1; }; CloseHandle(h); return 0; } else { errno = EINVAL; return -1; }; } /* Forced write to disk */ int fsync (int fd) { HANDLE h = (HANDLE) _get_osfhandle(fd); DWORD err; if (h == INVALID_HANDLE_VALUE) { errno = EBADF; return -1; } if (!FlushFileBuffers(h)) { /* Windows error -> Unix */ err = GetLastError(); switch (err) { case ERROR_INVALID_HANDLE: errno = EINVAL; break; default: errno = EIO; } return -1; } return 0; } /* Missing wait3() implementation */ pid_t wait3(int *stat_loc, int options, void *rusage) { NOTUSED(stat_loc); NOTUSED(options); NOTUSED(rusage); return (pid_t) waitpid((intptr_t) -1, 0, WAIT_FLAGS); } /* Replace MS C rtl rand which is 15bit with 32 bit */ int replace_random() { unsigned int x=0; if (RtlGenRandom == NULL) { // load proc if not loaded HMODULE lib = LoadLibraryA("advapi32.dll"); RtlGenRandom = (RtlGenRandomFunc)GetProcAddress(lib, "SystemFunction036"); if (RtlGenRandom == NULL) return 1; } RtlGenRandom(&x, sizeof(UINT_MAX)); return (int)(x >> 1); } /* BSD sockets compatibile replacement int replace_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) { return (setsockopt)((SOCKET)socket, level, optname, (const char *)optval, optlen); } */ /* set size with 64bit support */ int replace_ftruncate(int fd, off64_t length) { HANDLE h = (HANDLE) _get_osfhandle (fd); LARGE_INTEGER l, o; if (h == INVALID_HANDLE_VALUE) { errno = EBADF; return -1; } l.QuadPart = length; if (!SetFilePointerEx(h, l, &o, FILE_BEGIN)) return -1; if (!SetEndOfFile(h)) return -1; return 0; } /* Rename which works on Windows when file exists */ int replace_rename(const char *src, const char *dst) { /* anti-virus may lock file - error code 5. Retry until it works or get a different error */ int retries = 50; while (1) { if (MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) { return 0; } else { errno = GetLastError(); if (errno != 5) break; retries--; if (retries == 0) { retries = 50; Sleep(10); } } } /* On error we will return generic error code without GetLastError() */ return -1; } #if 0 /* Proxy structure to pass func and arg to thread */ typedef struct thread_params { void *(*func)(void *); void * arg; } thread_params; /* Proxy function by windows thread requirements */ static unsigned __stdcall win32_proxy_threadproc(void *arg) { thread_params *p = (thread_params *) arg; p->func(p->arg); /* Dealocate params */ free(p); _endthreadex(0); return 0; } int pthread_create(pthread_t *thread, const void *unused, void *(*start_routine)(void*), void *arg) { HANDLE h; thread_params *params = (thread_params *)malloc(sizeof(thread_params)); NOTUSED(unused); params->func = start_routine; params->arg = arg; #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION # define STACK_SIZE_PARAM_IS_A_RESERVATION 4096 #endif h =(HANDLE) _beginthreadex(NULL, /* Security not used */ THREAD_STACK_SIZE, /* Set custom stack size */ win32_proxy_threadproc, /* calls win32 stdcall proxy */ params, /* real threadproc is passed as paremeter */ STACK_SIZE_PARAM_IS_A_RESERVATION, /* reserve stack */ thread /* returned thread id */ ); if (!h) return errno; CloseHandle(h); return 0; } /* Noop in windows */ int pthread_detach (pthread_t thread) { NOTUSED(thread); return 0; /* noop */ } pthread_t pthread_self(void) { return GetCurrentThreadId(); } int win32_pthread_join(pthread_t *thread, void **value_ptr) { int result; HANDLE h = OpenThread(SYNCHRONIZE, FALSE, *thread); NOTUSED(value_ptr); switch (WaitForSingleObject(h, INFINITE)) { case WAIT_OBJECT_0: result = 0; case WAIT_ABANDONED: result = EINVAL; default: result = GetLastError(); } CloseHandle(h); return result; } int pthread_cond_init(pthread_cond_t *cond, const void *unused) { NOTUSED(unused); cond->waiters = 0; cond->was_broadcast = 0; InitializeCriticalSection(&cond->waiters_lock); cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); if (!cond->sema) { errno = GetLastError(); return -1; } cond->continue_broadcast = CreateEvent(NULL, /* security */ FALSE, /* auto-reset */ FALSE, /* not signaled */ NULL); /* name */ if (!cond->continue_broadcast) { errno = GetLastError(); return -1; } return 0; } int pthread_cond_destroy(pthread_cond_t *cond) { CloseHandle(cond->sema); CloseHandle(cond->continue_broadcast); DeleteCriticalSection(&cond->waiters_lock); return 0; } int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { int last_waiter; EnterCriticalSection(&cond->waiters_lock); cond->waiters++; LeaveCriticalSection(&cond->waiters_lock); /* * Unlock external mutex and wait for signal. * NOTE: we've held mutex locked long enough to increment * waiters count above, so there's no problem with * leaving mutex unlocked before we wait on semaphore. */ LeaveCriticalSection(mutex); /* let's wait - ignore return value */ WaitForSingleObject(cond->sema, INFINITE); /* * Decrease waiters count. If we are the last waiter, then we must * notify the broadcasting thread that it can continue. * But if we continued due to cond_signal, we do not have to do that * because the signaling thread knows that only one waiter continued. */ EnterCriticalSection(&cond->waiters_lock); cond->waiters--; last_waiter = cond->was_broadcast && cond->waiters == 0; LeaveCriticalSection(&cond->waiters_lock); if (last_waiter) { /* * cond_broadcast was issued while mutex was held. This means * that all other waiters have continued, but are contending * for the mutex at the end of this function because the * broadcasting thread did not leave cond_broadcast, yet. * (This is so that it can be sure that each waiter has * consumed exactly one slice of the semaphor.) * The last waiter must tell the broadcasting thread that it * can go on. */ SetEvent(cond->continue_broadcast); /* * Now we go on to contend with all other waiters for * the mutex. Auf in den Kampf! */ } /* lock external mutex again */ EnterCriticalSection(mutex); return 0; } /* * IMPORTANT: This implementation requires that pthread_cond_signal * is called while the mutex is held that is used in the corresponding * pthread_cond_wait calls! */ int pthread_cond_signal(pthread_cond_t *cond) { int have_waiters; EnterCriticalSection(&cond->waiters_lock); have_waiters = cond->waiters > 0; LeaveCriticalSection(&cond->waiters_lock); /* * Signal only when there are waiters */ if (have_waiters) return ReleaseSemaphore(cond->sema, 1, NULL) ? 0 : GetLastError(); else return 0; } #endif /* Redis forks to perform background writing */ /* fork() on unix will split process in two */ /* marking memory pages as Copy-On-Write so */ /* child process will have data snapshot. */ /* Windows has no support for fork(). */ int fork(void) { return -1; } /* Redis CPU GetProcessTimes -> rusage */ int getrusage(int who, struct rusage * r) { FILETIME starttime, exittime, kerneltime, usertime; ULARGE_INTEGER li; if (r == NULL) { errno = EFAULT; return -1; } memset(r, 0, sizeof(struct rusage)); if (who == RUSAGE_SELF) { if (!GetProcessTimes(GetCurrentProcess(), &starttime, &exittime, &kerneltime, &usertime)) { errno = EFAULT; return -1; } } if (who == RUSAGE_CHILDREN) { /* Childless on windows */ starttime.dwLowDateTime = 0; starttime.dwHighDateTime = 0; exittime.dwLowDateTime = 0; exittime.dwHighDateTime = 0; kerneltime.dwLowDateTime = 0; kerneltime.dwHighDateTime = 0; usertime.dwLowDateTime = 0; usertime.dwHighDateTime = 0; } memcpy(&li, &kerneltime, sizeof(FILETIME)); li.QuadPart /= 10L; r->ru_stime.tv_sec = (long)(li.QuadPart / 1000000L); r->ru_stime.tv_usec = (long)(li.QuadPart % 1000000L); memcpy(&li, &usertime, sizeof(FILETIME)); li.QuadPart /= 10L; r->ru_utime.tv_sec = (long)(li.QuadPart / 1000000L); r->ru_utime.tv_usec = (long)(li.QuadPart % 1000000L); return 0; } #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 #if 0 struct timezone { int tz_minuteswest; /* minutes W of Greenwich */ int tz_dsttime; /* type of dst correction */ }; int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; unsigned __int64 tmpres = 0; static int tzflag; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; /*converting file time to unix epoch*/ tmpres -= DELTA_EPOCH_IN_MICROSECS; tmpres /= 10; /*convert into microseconds*/ tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (NULL != tz) { if (!tzflag) { _tzset(); tzflag++; } tz->tz_minuteswest = _timezone / 60; tz->tz_dsttime = _daylight; } return 0; } //#endif static _locale_t clocale = NULL; double wstrtod(const char *nptr, char **eptr) { double d; char *leptr; #if 0 if (clocale == NULL) clocale = _create_locale(LC_ALL, "C"); #endif d = _strtod_l(nptr, &leptr, clocale); /* if 0, check if input was inf */ if (d == 0 && nptr == leptr) { int neg = 0; while (isspace(*nptr)) nptr++; if (*nptr == '+') nptr++; else if (*nptr == '-') { nptr++; neg = 1; } if (strnicmp("INF", nptr, 3) == 0) { if (eptr != NULL) { if (strnicmp("INFINITE", nptr, 8) == 0) *eptr = (char*)(nptr + 8); else *eptr = (char*)(nptr + 3); } if (neg == 1) return -HUGE_VAL; else return HUGE_VAL; } else if (strnicmp("NAN", nptr, 3) == 0) { if (eptr != NULL) *eptr = (char*)(nptr + 3); /* create a NaN : 0 * infinity*/ d = HUGE_VAL; return d * 0; } } if (eptr != NULL) *eptr = leptr; return d; } int strerror_r(int err, char* buf, size_t buflen) { int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, buf, (DWORD)buflen, NULL); if (size == 0) { char* strerr = strerror(err); if (strlen(strerr) >= buflen) { errno = ERANGE; return -1; } strcpy(buf, strerr); } if (size > 2 && buf[size - 2] == '\r') { /* remove extra CRLF */ buf[size - 2] = '\0'; } return 0; } char wsa_strerror_buf[128]; char *wsa_strerror(int err) { int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, wsa_strerror_buf, 128, NULL); if (size == 0) return strerror(err); if (size > 2 && wsa_strerror_buf[size - 2] == '\r') { /* remove extra CRLF */ wsa_strerror_buf[size - 2] = '\0'; } return wsa_strerror_buf; } #endif //0 #endif //_WIN32 |