2 * hdhomerun_os_windows.h
4 * Copyright © 2006-2008 Silicondust Engineering Ltd. <www.silicondust.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * As a special exception to the GNU Lesser General Public License,
20 * you may link, statically or dynamically, an application with a
21 * publicly distributed version of the Library to produce an
22 * executable file containing portions of the Library, and
23 * distribute that executable file under terms of your choice,
24 * without any of the additional requirements listed in clause 4 of
25 * the GNU Lesser General Public License.
27 * By "a publicly distributed version of the Library", we mean
28 * either the unmodified Library as distributed by Silicondust, or a
29 * modified version of the Library that is distributed under the
30 * conditions defined in the GNU Lesser General Public License.
43 #include <sys/types.h>
44 #include <sys/timeb.h>
48 int WSAAPI getaddrinfo(
51 const struct addrinfo *hints,
56 void WSAAPI freeaddrinfo(
64 #if defined(DLL_EXPORT)
65 #define LIBTYPE __declspec( dllexport )
66 #elif defined(DLL_IMPORT)
67 #define LIBTYPE __declspec( dllimport )
73 typedef signed __int8 int8_t;
74 typedef signed __int16 int16_t;
75 typedef signed __int32 int32_t;
76 typedef signed __int64 int64_t;
77 typedef unsigned __int8 uint8_t;
78 typedef unsigned __int16 uint16_t;
79 typedef unsigned __int32 uint32_t;
80 typedef unsigned __int64 uint64_t;
81 typedef HANDLE pthread_t;
82 typedef HANDLE pthread_mutex_t;
85 #define close closesocket
86 #define sock_getlasterror WSAGetLastError()
87 #define sock_getlasterror_socktimeout (WSAGetLastError() == WSAETIMEDOUT)
89 #define va_copy(x, y) x = y
92 #define strdup _strdup
94 #define strcasecmp _stricmp
96 #define snprintf _snprintf
97 #define fseeko _fseeki64
98 #define ftello _ftelli64
99 #define THREAD_FUNC_PREFIX DWORD WINAPI
100 #define SIGPIPE SIGABRT
102 static inline int msleep(unsigned int ms)
108 static inline int sleep(unsigned int sec)
114 static inline uint64_t getcurrenttime(void)
118 return ((uint64_t)tb.time * 1000) + tb.millitm;
121 static inline int setsocktimeout(int s, int level, int optname, uint64_t timeout)
123 int t = (int)timeout;
124 return setsockopt(s, level, optname, (char *)&t, sizeof(t));
127 static inline int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg)
129 *tid = CreateThread(NULL, 0, start, arg, 0, NULL);
131 return (int)GetLastError();
136 static inline int pthread_join(pthread_t tid, void **value_ptr)
140 if (!GetExitCodeThread(tid, &ExitCode)) {
141 return (int)GetLastError();
143 if (ExitCode != STILL_ACTIVE) {
149 static inline void pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
151 *mutex = CreateMutex(NULL, FALSE, NULL);
154 static inline void pthread_mutex_lock(pthread_mutex_t *mutex)
156 WaitForSingleObject(*mutex, INFINITE);
159 static inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
161 ReleaseMutex(*mutex);
165 * The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
166 * Attempting to restore on exit fails to restore if the program is terminated by the user.
167 * Solution - set the output format each printf.
169 static inline void console_vprintf(const char *fmt, va_list ap)
171 UINT cp = GetConsoleOutputCP();
172 SetConsoleOutputCP(CP_UTF8);
174 SetConsoleOutputCP(cp);
177 static inline void console_printf(const char *fmt, ...)
181 console_vprintf(fmt, ap);