2004-03-03 00:56:51 +01:00
|
|
|
// misc. POSIX routines for Win32
|
|
|
|
//
|
2004-06-03 20:36:48 +02:00
|
|
|
// Copyright (c) 2004 Jan Wassenberg
|
2004-03-03 00:56:51 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// Contact info:
|
|
|
|
// Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
// http://www.stud.uni-karlsruhe.de/~urkt/
|
|
|
|
|
|
|
|
// collection of hacks :P
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
#include "lib.h"
|
|
|
|
#include "win_internal.h"
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-08-28 13:14:23 +02:00
|
|
|
// cast intptr_t to HANDLE; centralized for easier changing, e.g. avoiding
|
|
|
|
// warnings. i = -1 converts to INVALID_HANDLE_VALUE (same value).
|
|
|
|
static inline HANDLE cast_to_HANDLE(intptr_t i)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
|
|
|
return (HANDLE)((char*)0 + i);
|
|
|
|
}
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// file
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-07-05 04:27:51 +02:00
|
|
|
int open(const char* fn, int oflag, ...)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-12-07 02:12:35 +01:00
|
|
|
const bool is_com_port = strncmp(fn, "/dev/tty", 8) == 0;
|
|
|
|
// also used later, before aio_reopen
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-12-07 02:12:35 +01:00
|
|
|
// "/dev/tty?" => "COM?"
|
2004-05-06 19:14:30 +02:00
|
|
|
if(is_com_port)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-12-07 02:12:35 +01:00
|
|
|
char port[] = "COM ";
|
|
|
|
// Windows only supports COM1..COM4.
|
|
|
|
char c = fn[8]+1;
|
|
|
|
if(!('1' <= c && c <= '4'))
|
|
|
|
return -1;
|
2004-03-03 00:56:51 +01:00
|
|
|
port[3] = (char)(fn[8]+1);
|
|
|
|
fn = port;
|
|
|
|
}
|
|
|
|
|
2004-07-05 04:27:51 +02:00
|
|
|
mode_t mode = 0;
|
|
|
|
if(oflag & O_CREAT)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, oflag);
|
|
|
|
mode = va_arg(args, mode_t);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2004-08-05 14:45:27 +02:00
|
|
|
WIN_SAVE_LAST_ERROR; // CreateFile
|
2004-07-05 04:27:51 +02:00
|
|
|
int fd = _open(fn, oflag, mode);
|
2004-07-12 16:25:39 +02:00
|
|
|
WIN_RESTORE_LAST_ERROR;
|
|
|
|
|
2004-08-05 14:45:27 +02:00
|
|
|
#ifdef PARANOIA
|
|
|
|
debug_out("open %s = %d\n", fn, fd);
|
|
|
|
#endif
|
2004-07-12 16:25:39 +02:00
|
|
|
|
2004-12-07 02:12:35 +01:00
|
|
|
// open it for async I/O as well (_open defaults to deny_none sharing):
|
|
|
|
// if not stdin/stdout/stderr and
|
2004-03-03 00:56:51 +01:00
|
|
|
if(fd > 2)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-12-07 02:12:35 +01:00
|
|
|
// not a COM port. don't currently need aio access for those;
|
2004-07-05 04:27:51 +02:00
|
|
|
// also, aio_reopen's CreateFile reports access denied when trying to open.
|
2004-05-06 19:14:30 +02:00
|
|
|
if(!is_com_port)
|
2004-07-05 04:27:51 +02:00
|
|
|
aio_reopen(fd, fn, oflag);
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-08-05 14:45:27 +02:00
|
|
|
// CRT doesn't like more than 255 files open.
|
|
|
|
// warn now, so that we notice why so many are open
|
2004-12-07 02:12:35 +01:00
|
|
|
#ifndef NDEBUG
|
2004-08-05 14:45:27 +02:00
|
|
|
if(fd > 256)
|
|
|
|
debug_warn("wposix: too many files open (CRT limitation)");
|
2004-12-07 02:12:35 +01:00
|
|
|
#endif
|
2004-08-05 14:45:27 +02:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int close(int fd)
|
|
|
|
{
|
2004-08-05 14:45:27 +02:00
|
|
|
#ifdef PARANOIA
|
2004-08-03 14:57:06 +02:00
|
|
|
debug_out("close %d\n", fd);
|
2004-08-05 14:45:27 +02:00
|
|
|
#endif
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
assert(3 <= fd && fd < 256);
|
2004-03-03 00:56:51 +01:00
|
|
|
aio_close(fd);
|
|
|
|
return _close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ioctl(int fd, int op, int* data)
|
|
|
|
{
|
2004-08-28 13:14:23 +02:00
|
|
|
const HANDLE h = cast_to_HANDLE(_get_osfhandle(fd));
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
switch(op)
|
|
|
|
{
|
|
|
|
case TIOCMGET:
|
|
|
|
/* TIOCM_* mapped directly to MS_*_ON */
|
|
|
|
GetCommModemStatus(h, (DWORD*)data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCMBIS:
|
|
|
|
/* only RTS supported */
|
|
|
|
if(*data & TIOCM_RTS)
|
|
|
|
EscapeCommFunction(h, SETRTS);
|
|
|
|
else
|
|
|
|
EscapeCommFunction(h, CLRRTS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCMIWAIT:
|
|
|
|
static DWORD mask;
|
|
|
|
DWORD new_mask = 0;
|
|
|
|
if(*data & TIOCM_CD)
|
|
|
|
new_mask |= EV_RLSD;
|
|
|
|
if(*data & TIOCM_CTS)
|
|
|
|
new_mask |= EV_CTS;
|
|
|
|
if(new_mask != mask)
|
|
|
|
SetCommMask(h, mask = new_mask);
|
|
|
|
WaitCommEvent(h, &mask, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-07 02:12:35 +01:00
|
|
|
/*
|
2004-03-03 00:56:51 +01:00
|
|
|
// currently only sets st_mode (file or dir) and st_size.
|
|
|
|
int stat(const char* fn, struct stat* s)
|
|
|
|
{
|
|
|
|
memset(s, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if(!GetFileAttributesEx(fn, GetFileExInfoStandard, &fad))
|
|
|
|
return -1;
|
|
|
|
|
2004-12-07 02:12:35 +01:00
|
|
|
s->st_mtime = fad.ftLastAccessTime
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
// dir
|
2004-05-15 05:02:26 +02:00
|
|
|
if(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
2004-03-03 00:56:51 +01:00
|
|
|
s->st_mode = S_IFDIR;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s->st_mode = S_IFREG;
|
|
|
|
s->st_size = (off_t)((((u64)fad.nFileSizeHigh) << 32) | fad.nFileSizeLow);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-07 02:12:35 +01:00
|
|
|
*/
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// dir
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
char* realpath(const char* fn, char* path)
|
|
|
|
{
|
|
|
|
if(!GetFullPathName(fn, PATH_MAX, path, 0))
|
|
|
|
return 0;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mkdir(const char* path, mode_t)
|
|
|
|
{
|
|
|
|
return CreateDirectory(path, 0)? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct _DIR
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
HANDLE handle;
|
|
|
|
struct dirent ent; // must not be overwritten by calls for different dirs
|
|
|
|
bool not_first;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
static const DWORD hs = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
DIR* opendir(const char* name)
|
|
|
|
{
|
|
|
|
DWORD fa = GetFileAttributes(name);
|
2004-09-02 04:47:11 +02:00
|
|
|
if((fa == INVALID_FILE_ATTRIBUTES) || // GetFileAttributes failed
|
|
|
|
!(fa & FILE_ATTRIBUTE_DIRECTORY) || // not a directory
|
|
|
|
(fa & hs)) // hidden or system dir
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
_DIR* d = (_DIR*)calloc(sizeof(_DIR), 1);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
char path[MAX_PATH+1];
|
|
|
|
strncpy(path, name, MAX_PATH-2);
|
|
|
|
strcat(path, "\\*");
|
|
|
|
d->handle = FindFirstFile(path, &d->fd);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* dir)
|
|
|
|
{
|
|
|
|
_DIR* d = (_DIR*)dir;
|
|
|
|
|
2004-07-12 16:25:39 +02:00
|
|
|
DWORD last_err = GetLastError();
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
again:
|
2004-03-03 00:56:51 +01:00
|
|
|
if(d->not_first)
|
|
|
|
if(!FindNextFile(d->handle, &d->fd))
|
2004-07-12 16:25:39 +02:00
|
|
|
{
|
|
|
|
// don't pass on the "error"
|
|
|
|
if(GetLastError() == ERROR_NO_MORE_FILES)
|
|
|
|
SetLastError(last_err);
|
|
|
|
else
|
|
|
|
debug_warn("FindNextFile failed");
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
2004-07-12 16:25:39 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
d->not_first = true;
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
// hidden or system entry - don't return it.
|
2004-12-09 21:17:09 +01:00
|
|
|
DWORD attr = d->fd.dwFileAttributes;
|
|
|
|
if(attr & hs)
|
2004-09-02 04:47:11 +02:00
|
|
|
goto again;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
d->ent.d_ino = 0;
|
|
|
|
d->ent.d_name = &d->fd.cFileName[0];
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
// non-standard fields - what stat returns.
|
|
|
|
// add them to speed up file_enum (Win32 specific, though)
|
|
|
|
|
|
|
|
d->ent.size = (off_t)((((u64)d->fd.nFileSizeHigh) << 32) | d->fd.nFileSizeLow);
|
|
|
|
d->ent.mode = (attr & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG;
|
|
|
|
|
|
|
|
SYSTEMTIME st = { 0 };
|
|
|
|
FileTimeToSystemTime(&d->fd.ftLastWriteTime, &st);
|
|
|
|
tm t;
|
|
|
|
t.tm_sec = st.wSecond;
|
|
|
|
t.tm_min = st.wMinute;
|
|
|
|
t.tm_hour = st.wHour;
|
|
|
|
t.tm_mday = st.wDay;
|
|
|
|
t.tm_mon = st.wMonth-1;
|
|
|
|
t.tm_year = st.wYear-1900;
|
|
|
|
t.tm_isdst = -1; // unknown; let CRT decide
|
|
|
|
d->ent.mtime = mktime(&t);
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
return &d->ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int closedir(DIR* dir)
|
|
|
|
{
|
|
|
|
_DIR* d = (_DIR*)dir;
|
|
|
|
|
|
|
|
FindClose(d->handle);
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// terminal
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
static HANDLE std_h[2] = { (HANDLE)((char*)0 + 3), (HANDLE)((char*)0 + 7) };
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
__declspec(naked) void _get_console()
|
|
|
|
{ __asm jmp dword ptr [AllocConsole] }
|
|
|
|
|
|
|
|
__declspec(naked) void _hide_console()
|
|
|
|
{ __asm jmp dword ptr [FreeConsole] }
|
|
|
|
|
|
|
|
|
|
|
|
int tcgetattr(int fd, struct termios* termios_p)
|
|
|
|
{
|
|
|
|
if(fd > 2)
|
|
|
|
return -1;
|
|
|
|
HANDLE h = std_h[fd];
|
|
|
|
|
|
|
|
DWORD mode;
|
|
|
|
GetConsoleMode(h, &mode);
|
|
|
|
termios_p->c_lflag = mode & (ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tcsetattr(int fd, int /* optional_actions */, const struct termios* termios_p)
|
|
|
|
{
|
|
|
|
if(fd > 2)
|
|
|
|
return -1;
|
|
|
|
HANDLE h = std_h[fd];
|
|
|
|
SetConsoleMode(h, (DWORD)termios_p->c_lflag);
|
|
|
|
FlushConsoleInputBuffer(h);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int poll(struct pollfd /* fds */[], int /* nfds */, int /* timeout */)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// thread
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
__declspec(naked) pthread_t pthread_self(void)
|
2004-05-30 02:46:58 +02:00
|
|
|
{ __asm jmp dword ptr [GetCurrentThread] }
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
int pthread_getschedparam(pthread_t thread, int* policy, struct sched_param* param)
|
|
|
|
{
|
|
|
|
if(policy)
|
|
|
|
{
|
|
|
|
DWORD pc = GetPriorityClass(GetCurrentProcess());
|
|
|
|
*policy = (pc >= HIGH_PRIORITY_CLASS)? SCHED_FIFO : SCHED_RR;
|
|
|
|
}
|
|
|
|
if(param)
|
|
|
|
{
|
2004-08-28 13:14:23 +02:00
|
|
|
const HANDLE hThread = cast_to_HANDLE((intptr_t)thread);
|
2004-03-03 00:56:51 +01:00
|
|
|
param->sched_priority = GetThreadPriority(hThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param* param)
|
|
|
|
{
|
2004-12-07 04:01:12 +01:00
|
|
|
const int pri = param->sched_priority;
|
|
|
|
|
|
|
|
// additional boost for policy == SCHED_FIFO
|
|
|
|
DWORD pri_class = NORMAL_PRIORITY_CLASS;
|
2004-03-03 00:56:51 +01:00
|
|
|
if(policy == SCHED_FIFO)
|
2004-12-07 04:01:12 +01:00
|
|
|
{
|
|
|
|
pri_class = HIGH_PRIORITY_CLASS;
|
|
|
|
if(pri == 2)
|
|
|
|
pri_class = REALTIME_PRIORITY_CLASS;
|
|
|
|
}
|
|
|
|
SetPriorityClass(GetCurrentProcess(), pri_class);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-12-07 04:01:12 +01:00
|
|
|
// choose fixed Windows values from pri
|
2004-08-28 13:14:23 +02:00
|
|
|
const HANDLE hThread = cast_to_HANDLE((intptr_t)thread);
|
2004-12-07 04:01:12 +01:00
|
|
|
SetThreadPriority(hThread, pri);
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 16:22:07 +02:00
|
|
|
struct ThreadParam
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-21 16:22:07 +02:00
|
|
|
void*(*func)(void*);
|
|
|
|
void* user_arg;
|
2004-09-02 04:47:11 +02:00
|
|
|
ThreadParam(void*(*_func)(void*), void* _user_arg)
|
|
|
|
: func(_func), user_arg(_user_arg) {}
|
2004-06-21 16:22:07 +02:00
|
|
|
};
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
|
|
|
|
// trampoline to switch calling convention.
|
|
|
|
// param points to a heap-allocated ThreadParam (see pthread_create).
|
|
|
|
static unsigned __stdcall thread_start(void* param)
|
2004-06-21 16:22:07 +02:00
|
|
|
{
|
2004-09-02 04:47:11 +02:00
|
|
|
ThreadParam* f = (ThreadParam*)param;
|
|
|
|
void*(*func)(void*) = f->func;
|
|
|
|
void* user_arg = f->user_arg;
|
|
|
|
delete f;
|
2004-06-21 16:22:07 +02:00
|
|
|
|
2004-09-19 19:27:03 +02:00
|
|
|
// workaround for stupid "void* -> unsigned cast" warning
|
|
|
|
union { void* p; unsigned u; } v;
|
|
|
|
v.p = func(user_arg);
|
|
|
|
return v.u;
|
2004-06-21 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pthread_create(pthread_t* thread, const void* attr, void*(*func)(void*), void* user_arg)
|
|
|
|
{
|
|
|
|
UNUSED(attr);
|
|
|
|
|
2004-09-02 04:47:11 +02:00
|
|
|
// notes:
|
|
|
|
// - don't call via asm: _beginthreadex might be a func ptr (if DLL CRT).
|
|
|
|
// - don't stack-allocate param: thread_start might not be called
|
|
|
|
// in the new thread before we exit this stack frame.
|
|
|
|
ThreadParam* param = new ThreadParam(func, user_arg);
|
2004-08-16 16:30:18 +02:00
|
|
|
*thread = (pthread_t)_beginthreadex(0, 0, thread_start, (void*)param, 0, 0);
|
2004-06-21 16:22:07 +02:00
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-24 19:27:51 +02:00
|
|
|
// DeleteCriticalSection currently doesn't complain if we double-free
|
|
|
|
// (e.g. user calls destroy() and static initializer atexit runs),
|
|
|
|
// and dox are ambiguous.
|
|
|
|
/*
|
|
|
|
struct CS
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION cs;
|
|
|
|
CS()
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&cs);
|
|
|
|
}
|
|
|
|
~CS()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&cs);
|
|
|
|
}
|
|
|
|
};*/
|
|
|
|
|
|
|
|
cassert(sizeof(CRITICAL_SECTION) == sizeof(pthread_mutex_t));
|
|
|
|
/*
|
|
|
|
static std::list<CS> mutexes;
|
|
|
|
|
|
|
|
static void destroy_mutexes()
|
|
|
|
{
|
|
|
|
mutexes.clear();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
pthread_mutex_t pthread_mutex_initializer()
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
CRITICAL_SECTION cs;
|
|
|
|
InitializeCriticalSection(&cs);
|
|
|
|
return *(pthread_mutex_t*)&cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_destroy(pthread_mutex_t* m)
|
|
|
|
{
|
|
|
|
DeleteCriticalSection((CRITICAL_SECTION*)m);
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
CS* cs = (CS*)m;
|
|
|
|
mutexes.erase(cs);
|
|
|
|
delete cs;
|
|
|
|
return 0;
|
|
|
|
*/
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t*)
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
InitializeCriticalSection((CRITICAL_SECTION*)m);
|
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_lock(pthread_mutex_t* m)
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
EnterCriticalSection((CRITICAL_SECTION*)m);
|
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_trylock(pthread_mutex_t* m)
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
BOOL got_it = TryEnterCriticalSection((CRITICAL_SECTION*)m);
|
|
|
|
return got_it? 0 : -1;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_unlock(pthread_mutex_t* m)
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
LeaveCriticalSection((CRITICAL_SECTION*)m);
|
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_timedlock(pthread_mutex_t* m, const struct timespec* abs_timeout)
|
|
|
|
{
|
2004-08-24 19:27:51 +02:00
|
|
|
return -ENOSYS;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// memory mapping
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-12-10 00:12:02 +01:00
|
|
|
void* mmap(void* user_start, size_t len, int prot, int flags, int fd, off_t offset)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-08-27 02:29:46 +02:00
|
|
|
{
|
2004-07-12 16:25:39 +02:00
|
|
|
WIN_SAVE_LAST_ERROR;
|
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
// assume fd = -1 (requesting mapping backed by page file),
|
|
|
|
// so that we notice invalid file handles below.
|
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
|
|
|
if(fd != -1)
|
|
|
|
{
|
2004-08-28 13:14:23 +02:00
|
|
|
hFile = cast_to_HANDLE(_get_osfhandle(fd));
|
2004-08-27 02:29:46 +02:00
|
|
|
if(hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
debug_warn("mmap: invalid file handle");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapView.. will choose start address unless MAP_FIXED was specified.
|
|
|
|
void* start = 0;
|
|
|
|
if(flags & MAP_FIXED)
|
|
|
|
{
|
|
|
|
start = user_start;
|
|
|
|
if(start == 0) // assert below would fire
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out access rights.
|
|
|
|
// note: reads are always allowed (Win32 limitation).
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
SECURITY_ATTRIBUTES sec = { sizeof(SECURITY_ATTRIBUTES), (void*)0, FALSE };
|
|
|
|
DWORD flProtect = PAGE_READONLY;
|
|
|
|
DWORD dwAccess = FILE_MAP_READ;
|
|
|
|
|
|
|
|
// .. no access: not possible on Win32.
|
|
|
|
if(prot == PROT_NONE)
|
|
|
|
goto fail;
|
|
|
|
// .. write or read/write (Win32 doesn't support write-only)
|
2004-03-03 00:56:51 +01:00
|
|
|
if(prot & PROT_WRITE)
|
|
|
|
{
|
|
|
|
flProtect = PAGE_READWRITE;
|
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
const bool shared = (flags & MAP_SHARED ) != 0;
|
|
|
|
const bool priv = (flags & MAP_PRIVATE) != 0;
|
|
|
|
// .. both aren't allowed
|
|
|
|
if(shared && priv)
|
|
|
|
goto fail;
|
|
|
|
// .. changes are shared & written to file
|
|
|
|
else if(shared)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-08-27 02:29:46 +02:00
|
|
|
sec.bInheritHandle = TRUE;
|
2004-03-03 00:56:51 +01:00
|
|
|
dwAccess = FILE_MAP_ALL_ACCESS;
|
|
|
|
}
|
2004-08-27 02:29:46 +02:00
|
|
|
// .. private copy-on-write mapping
|
|
|
|
else if(priv)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
|
|
|
flProtect = PAGE_WRITECOPY;
|
|
|
|
dwAccess = FILE_MAP_COPY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
// now actually map.
|
|
|
|
const DWORD len_hi = (DWORD)((u64)len >> 32);
|
|
|
|
// careful! language doesn't allow shifting 32-bit types by 32 bits.
|
|
|
|
const DWORD len_lo = (DWORD)len & 0xffffffff;
|
|
|
|
const HANDLE hMap = CreateFileMapping(hFile, &sec, flProtect, len_hi, len_lo, (LPCSTR)0);
|
|
|
|
if(hMap == INVALID_HANDLE_VALUE)
|
|
|
|
// bail now so that MapView.. doesn't overwrite the last error value.
|
|
|
|
goto fail;
|
2004-03-03 00:56:51 +01:00
|
|
|
void* ptr = MapViewOfFileEx(hMap, dwAccess, len_hi, offset, len_lo, start);
|
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
// free the mapping object now, so that we don't have to hold on to its
|
|
|
|
// handle until munmap(). it's not actually released yet due to the
|
|
|
|
// reference held by MapViewOfFileEx (if it succeeded).
|
|
|
|
if(hMap != INVALID_HANDLE_VALUE) // avoid "invalid handle" error
|
|
|
|
CloseHandle(hMap);
|
|
|
|
|
|
|
|
if(!ptr)
|
|
|
|
// bail now, before the last error value is restored,
|
|
|
|
// but after freeing the mapping object.
|
|
|
|
goto fail;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-08-27 02:29:46 +02:00
|
|
|
assert(!(flags & MAP_FIXED) || (ptr == start));
|
|
|
|
// fixed => ptr = start
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-07-12 16:25:39 +02:00
|
|
|
WIN_RESTORE_LAST_ERROR;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
return ptr;
|
2004-08-27 02:29:46 +02:00
|
|
|
}
|
|
|
|
fail:
|
|
|
|
return MAP_FAILED;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-10 00:12:02 +01:00
|
|
|
int munmap(void* start, size_t len)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-08-27 02:29:46 +02:00
|
|
|
UNUSED(len);
|
2004-07-12 16:25:39 +02:00
|
|
|
BOOL ok = UnmapViewOfFile(start);
|
|
|
|
return ok? 0 : -1;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int uname(struct utsname* un)
|
|
|
|
{
|
|
|
|
static OSVERSIONINFO vi;
|
|
|
|
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&vi);
|
|
|
|
|
|
|
|
// OS implementation name
|
|
|
|
const char* family = "??";
|
|
|
|
int ver = (vi.dwMajorVersion << 8) | vi.dwMinorVersion;
|
|
|
|
if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
|
|
|
|
family = (ver == 0x045a)? "ME" : "9x";
|
|
|
|
if(vi.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
|
|
|
{
|
|
|
|
if(ver == 0x0500)
|
|
|
|
family = "2k";
|
|
|
|
else if(ver == 0x0501)
|
|
|
|
family = "XP";
|
|
|
|
else
|
|
|
|
family = "NT";
|
|
|
|
}
|
|
|
|
sprintf(un->sysname, "Win%s", family);
|
|
|
|
|
|
|
|
// release info
|
|
|
|
const char* vs = vi.szCSDVersion;
|
|
|
|
int sp;
|
|
|
|
if(sscanf(vs, "Service Pack %d", &sp) == 1)
|
|
|
|
sprintf(un->release, "SP %d", sp);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char* release = "";
|
|
|
|
if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
|
|
|
|
{
|
|
|
|
if(!strcmp(vs, " C"))
|
|
|
|
release = "OSR2";
|
|
|
|
else if(!strcmp(vs, " A"))
|
|
|
|
release = "SE";
|
|
|
|
}
|
|
|
|
strcpy(un->release, release);
|
|
|
|
}
|
|
|
|
|
|
|
|
// version
|
|
|
|
sprintf(un->version, "%lu.%02lu.%lu", vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber & 0xffff);
|
|
|
|
|
|
|
|
// node name
|
|
|
|
DWORD buf_size = sizeof(un->nodename);
|
2004-07-12 16:25:39 +02:00
|
|
|
DWORD last_err = GetLastError();
|
|
|
|
BOOL ok = GetComputerName(un->nodename, &buf_size);
|
|
|
|
// GetComputerName sets last error even on success - suppress.
|
|
|
|
if(ok)
|
|
|
|
SetLastError(last_err);
|
|
|
|
else
|
|
|
|
debug_warn("GetComputerName failed");
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// hardware type
|
|
|
|
static SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
|
|
|
|
strcpy(un->machine, "AMD64");
|
|
|
|
else
|
|
|
|
strcpy(un->machine, "IA-32");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-18 02:38:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
long sysconf(int name)
|
|
|
|
{
|
|
|
|
// used by _SC_*_PAGES
|
|
|
|
static DWORD page_size;
|
2004-05-18 02:38:39 +02:00
|
|
|
static BOOL (WINAPI *pGlobalMemoryStatusEx)(MEMORYSTATUSEX*);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-18 02:38:39 +02:00
|
|
|
ONCE(
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-18 02:38:39 +02:00
|
|
|
// get page size
|
|
|
|
// (used by _SC_PAGESIZE and _SC_*_PAGES)
|
2004-03-03 00:56:51 +01:00
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si); // can't fail => page_size always > 0.
|
2004-05-18 02:38:39 +02:00
|
|
|
page_size = si.dwPageSize;
|
|
|
|
|
2004-12-01 22:37:01 +01:00
|
|
|
// import GlobalMemoryStatusEx - it's not defined by the VC6 PSDK.
|
|
|
|
// used by _SC_*_PAGES if available (provides better results).
|
2004-05-18 02:38:39 +02:00
|
|
|
const HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
|
|
|
*(void**)&pGlobalMemoryStatusEx = GetProcAddress(hKernel32Dll, "GlobalMemoryStatusEx");
|
|
|
|
FreeLibrary(hKernel32Dll);
|
|
|
|
// make sure the reference is released so BoundsChecker
|
|
|
|
// doesn't complain. it won't actually be unloaded anyway -
|
|
|
|
// there is at least one other reference.
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
switch(name)
|
|
|
|
{
|
|
|
|
case _SC_PAGESIZE:
|
|
|
|
return page_size;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
case _SC_PHYS_PAGES:
|
|
|
|
case _SC_AVPHYS_PAGES:
|
2004-05-08 03:11:51 +02:00
|
|
|
{
|
2004-05-18 02:38:39 +02:00
|
|
|
u64 total_phys_mem;
|
|
|
|
u64 avail_phys_mem;
|
|
|
|
|
|
|
|
// first try GlobalMemoryStatus - cannot fail.
|
|
|
|
// override its results if GlobalMemoryStatusEx is available.
|
|
|
|
MEMORYSTATUS ms;
|
|
|
|
GlobalMemoryStatus(&ms);
|
|
|
|
// can't fail.
|
|
|
|
total_phys_mem = ms.dwTotalPhys;
|
|
|
|
avail_phys_mem = ms.dwAvailPhys;
|
|
|
|
|
|
|
|
// newer API is available: use it to report correct results
|
|
|
|
// (no overflow or wraparound) on systems with > 4 GB of memory.
|
|
|
|
MEMORYSTATUSEX mse = { sizeof(mse) };
|
2004-12-01 22:37:01 +01:00
|
|
|
if(pGlobalMemoryStatusEx && pGlobalMemoryStatusEx(&mse))
|
2004-05-18 02:38:39 +02:00
|
|
|
{
|
|
|
|
total_phys_mem = mse.ullTotalPhys;
|
|
|
|
avail_phys_mem = mse.ullAvailPhys;
|
|
|
|
}
|
2004-12-01 22:37:01 +01:00
|
|
|
// else: not an error, since this isn't available before Win2k / XP.
|
|
|
|
// we have results from GlobalMemoryStatus anyway.
|
2004-05-18 02:38:39 +02:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
if(name == _SC_PHYS_PAGES)
|
2004-05-18 02:38:39 +02:00
|
|
|
return (long)(round_up((uintptr_t)total_phys_mem, 2*MB) / page_size);
|
2004-05-08 03:11:51 +02:00
|
|
|
// Richter, "Programming Applications for Windows":
|
|
|
|
// reported value doesn't include non-paged pool reserved
|
|
|
|
// during boot; it's not considered available to kernel.
|
|
|
|
// it's 528 KB on my 512 MB machine (WinXP and Win2k).
|
|
|
|
else
|
2004-05-18 02:38:39 +02:00
|
|
|
return (long)(avail_phys_mem / page_size);
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|