Merge branch 'master' of github.com:hrydgard/native

This commit is contained in:
Henrik Rydgard 2012-08-13 20:11:43 +02:00
commit 2b65089df6
6 changed files with 118 additions and 9 deletions

View File

@ -87,4 +87,7 @@ inline uint64 swap64(const uint8* _pData) {return swap64(*(const uint64*)_pData)
#define __THREAD __thread
#endif
// For really basic windows code compat
typedef char TCHAR;
#endif // _BASE_BASICTYPES

View File

@ -2,6 +2,7 @@
int dp_xres;
int dp_yres;
int pixel_xres;
int pixel_yres;

View File

@ -12,11 +12,18 @@
// Logging
#ifdef _WIN32
#undef Crash
#ifdef _M_X64
inline void Crash() { DebugBreak(); }
#else
inline void Crash() { __asm { int 3 }; }
#endif
#else
#ifdef ANDROID
#undef Crash
inline void Crash() {
char *p = (char *)1337;

View File

@ -6,6 +6,17 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// Zap stupid windows defines
// Should move these somewhere clever.
#undef p
#undef MIN
#undef MAX
#undef min
#undef max
#undef DrawText
#undef itoa
#else
#include <pthread.h>
#include <errno.h>
@ -57,6 +68,10 @@ public:
#endif
}
mutexType &native_handle() {
return mut_;
}
private:
mutexType mut_;
DISALLOW_COPY_AND_ASSIGN(recursive_mutex);
@ -71,10 +86,55 @@ private:
recursive_mutex &mtx_;
};
#undef p
#undef MIN
#undef MAX
#undef min
#undef max
#undef DrawText
#undef itoa
// Like a Windows event, or a modern condition variable.
class event {
public:
#ifdef _WIN32
#else
#endif
event() {
#ifdef _WIN32
event_ = CreateEvent(0, FALSE, FALSE, 0);
#else
pthread_cond_init(&event_, NULL);
#endif
}
~event() {
#ifdef _WIN32
CloseHandle(event_);
#else
pthread_cond_destroy(&event_);
#endif
}
void notify_one() {
#ifdef _WIN32
SetEvent(event_);
#else
pthread_cond_signal(&event_);
#endif
}
// notify_all is not really possible to implement with win32 events?
void wait(recursive_mutex &mtx) {
// broken
#ifdef _WIN32
//mtx.unlock();
WaitForSingleObject(event_, INFINITE);
ResetEvent(event_); // necessary?
// mtx.lock();
#else
pthread_cond_wait(&event_, &mtx.native_handle());
#endif
}
private:
#ifdef _WIN32
HANDLE event_;
#else
pthread_cond_t event_;
#endif
};

View File

@ -1,3 +1,38 @@
#pragma once
void setCurrentThreadName(const char *name);
#include <functional>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#ifndef _POSIX_THREADS
#error unsupported platform (no pthreads?)
#endif
#include <pthread.h>
#endif
void setCurrentThreadName(const char *name);
/*
class thread {
private:
#ifdef _WIN32
typedef HANDLE thread_;
#else
typedef pthread_t thread_;
#endif
public:
//void run(std::function<void()> threadFunc) {
// func_ =
//}
void wait() {
}
};*/

View File

@ -1,4 +1,4 @@
#include "net/resolve.h"
#include "net/resolve.h"
#include <stdio.h>
#include <stdlib.h>
@ -8,9 +8,12 @@
#ifndef _WIN32
#include <arpa/inet.h>
#include <netdb.h> // gethostbyname
#include <sys/socket.h>
#else
#include <WinSock2.h>
#include <Ws2tcpip.h>
#undef min
#undef max
#endif