Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more win32 code

git-svn-id: http://kazuho.31tools.com/svn/incline/trunk@265 4d3e2a30-9d6d-0410-bc8c-dac56cff10b3
  • Loading branch information...
commit e2dbead39517d2a3b75822c8626ca6ce7dc00ee5 1 parent 3571353
kazuho authored
View
7 src/incline_driver_async_qtable.cc
@@ -1,3 +1,10 @@
+extern "C" {
+#ifdef WIN32
+# include "incline_win32.h"
+#else
+# include <pthread.h>
+#endif
+}
#include "incline_dbms.h"
#include "incline_def_async_qtable.h"
#include "incline_driver_async_qtable.h"
View
14 src/incline_driver_sharded.cc
@@ -1,13 +1,15 @@
+#include <cassert>
+#include <fstream>
+#include <sstream>
+extern "C" {
+#include <time.h>
#ifdef WIN32
-# include <windows.h>
+# include "incline_win32.h"
#else
-extern "C" {
+# include <pthread.h>
# include <sys/stat.h>
-}
#endif
-#include <cassert>
-#include <fstream>
-#include <sstream>
+}
#include "start_thread.h"
#include "incline_dbms.h"
#include "incline_def_sharded.h"
View
1  src/incline_driver_sharded.h
@@ -1,7 +1,6 @@
#ifndef incline_driver_sharded_h
#define incline_driver_sharded_h
-#include "interthr_call.h"
#include "incline_driver_async_qtable.h"
class incline_def_sharded;
View
10 src/incline_fw_replicator.cc
@@ -1,12 +1,16 @@
+extern "C" {
+#ifdef WIN32
+# include "incline_win32.h"
+#else
+# include <pthread.h>
+#endif
+}
#include "incline_dbms.h"
#include "incline_def_sharded.h"
#include "incline_driver_sharded.h"
#include "incline_fw_replicator.h"
#include "incline_mgr.h"
#include "start_thread.h"
-#ifdef WIN32
-# include "incline_win32.h"
-#endif
using namespace std;
View
3  src/incline_fw_sharded.cc
@@ -1,3 +1,6 @@
+extern "C" {
+#include <time.h>
+}
#include "incline_dbms.h"
#include "incline_def_sharded.h"
#include "incline_driver_sharded.h"
View
8 src/incline_fw_sharded.h
@@ -1,8 +1,16 @@
#ifndef incline_fw_sharded_h
#define incline_fw_sharded_h
+extern "C" {
+#ifdef WIN32
+# include "incline_win32.h"
+#else
+# include <pthread.h>
+#endif
+}
#include "incline_driver_sharded.h"
#include "incline_fw_async_qtable.h"
+#include "interthr_call.h"
class incline_fw_sharded : public incline_fw_async_qtable {
public:
View
13 src/interthr_call.h
@@ -33,7 +33,6 @@ extern "C" {
#else
# include <alloca.h>
#endif
-#include <pthread.h>
}
#include <algorithm>
#include <cassert>
@@ -42,6 +41,14 @@ extern "C" {
#include <map>
#include "cac/cac_mutex.h"
+ // we need a wrapper class, since win32 critital section does not have an equiv. of PTHREAD_MUTEX_INITIALIZER
+struct interthr_call_mutex_t {
+ pthread_mutex_t m_;
+ interthr_call_mutex_t() { pthread_mutex_init(&m_, NULL); }
+ ~interthr_call_mutex_t() { /* intentionally left blank */ }
+ pthread_mutex_t* operator&() { return &m_; }
+};
+
template <typename Handler, typename Request>
class interthr_call_t {
public:
@@ -153,7 +160,7 @@ class interthr_call_t {
return *handle_slot;
}
protected:
- static pthread_mutex_t multi_mutex_;
+ static interthr_call_mutex_t multi_mutex_;
public:
template <typename Iter> static void call(const Iter& first, const Iter& last) {
size_t remain = std::distance(first, last);
@@ -197,7 +204,7 @@ class interthr_call_t {
};
template<typename Handler, typename Request>
-pthread_mutex_t interthr_call_t<Handler, Request>::multi_mutex_ = PTHREAD_MUTEX_INITIALIZER;
+interthr_call_mutex_t interthr_call_t<Handler, Request>::multi_mutex_;
#ifdef TEST_INTERTHR_CALL
View
3  src/start_thread.h
@@ -1,9 +1,6 @@
#ifndef start_thread_h
#define start_thread_h
-extern "C" {
-#include <pthread.h>
-}
#include <memory>
template <typename T> struct start_thread_t {
View
124 win32/src/incline_win32.cc
@@ -1,8 +1,128 @@
-#include <windows.h>
+extern "C" {
+#include <assert.h>
+}
#include "incline_win32.h"
-void sleep(unsigned int seconds)
+void iw32_sleep(unsigned int seconds)
{
// bare minimum :-p
Sleep(seconds * 1000);
}
+
+struct iw32_thread_start {
+ void (*start_routine)(void*);
+ void* arg;
+ iw32_thread_start(void (*r)(void*), void* a) : start_routine(r), arg(a) {}
+};
+
+static unsigned __stdcall start_cb(void* _ts)
+{
+ iw32_thread_start* tsp = static_cast<iw32_thread_start*>(_ts);
+ iw32_thread_start ts(*tsp);
+ delete tsp;
+ ts.start_routine(ts.arg);
+ return 0;
+}
+
+int iw32_pthread_create(HANDLE* thread, const void* _unused, void (*start_routine)(void*), void* arg)
+{
+ assert(_unused == NULL);
+ iw32_thread_start* ts = new iw32_thread_start(start_routine, arg);
+ *thread = (HANDLE)_beginthreadex(NULL, 0, start_cb, ts, 0, NULL);
+ if (*thread == INVALID_HANDLE_VALUE) {
+ delete ts;
+ return -1;
+ }
+ return 0;
+}
+
+int iw32_pthread_join(HANDLE thread, void** _unused)
+{
+ assert(_unused == NULL);
+ WaitForSingleObject(thread, INFINITE);
+ return 0;
+}
+
+int iw32_pthread_key_create(pthread_key_t* key, void (*_unused)(void*))
+{
+ assert(_unused == NULL);
+ *key = TlsAlloc();
+ assert(*key != 0xffffffff);
+ return 0;
+}
+
+int iw32_pthread_key_delete(pthread_key_t key)
+{
+ TlsFree(key);
+ return 0;
+}
+
+int iw32_pthread_mutex_init(pthread_mutex_t* mutex, const void* _unused)
+{
+ assert(_unused == NULL);
+ InitializeCriticalSection(mutex);
+ return 0;
+}
+
+int iw32_pthread_cond_init(pthread_cond_t* cond, const void* _unused)
+{
+ assert(_unused = NULL);
+ InitializeCriticalSection(&cond->lock);
+ cond->ev[0] = CreateEvent(NULL, TRUE, FALSE, NULL);
+ assert(cond->ev[0] != INVALID_HANDLE_VALUE);
+ cond->ev[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
+ assert(cond->ev[1] != INVALID_HANDLE_VALUE);
+ cond->nr_waiting = 0;
+ return 0;
+}
+
+int iw32_pthread_cond_destroy(pthread_cond_t* cond)
+{
+ assert(cond->nr_waiting == 0);
+ CloseHandle(cond->ev[1]);
+ CloseHandle(cond->ev[0]);
+ DeleteCriticalSection(&cond->lock);
+ return 0;
+}
+
+int iw32_pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
+{
+ // incr nr_waiting
+ EnterCriticalSection(&cond->lock);
+ cond->nr_waiting++;
+ LeaveCriticalSection(&cond->lock);
+
+ pthread_mutex_unlock(mutex);
+
+ int r = WaitForMultipleObjects(2, cond->ev, FALSE, INFINITE);
+
+ EnterCriticalSection(&cond->lock);
+ --cond->nr_waiting;
+ if (r == WAIT_OBJECT_0 && cond->nr_waiting == 0) {
+ ResetEvent(cond->ev[0]);
+ }
+ LeaveCriticalSection(&cond->lock);
+
+ pthread_mutex_lock(mutex);
+ return 0;
+}
+
+int iw32_pthread_cond_signal(pthread_cond_t* cond)
+{
+ EnterCriticalSection(&cond->lock);
+ if (cond->nr_waiting != 0) {
+ SetEvent(cond->ev[1]);
+ }
+ LeaveCriticalSection(&cond->lock);
+ return 0;
+}
+
+int iw32_pthread_cond_broadcast(pthread_cond_t* cond)
+{
+ EnterCriticalSection(&cond->lock);
+ if (cond->nr_waiting != 0) {
+ SetEvent(cond->ev[0]);
+ }
+ LeaveCriticalSection(&cond->lock);
+ return 0;
+}
View
57 win32/src/incline_win32.h
@@ -1,11 +1,66 @@
#ifndef incline_win32_h
#define incline_win32_h
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <windows.h>
+#include <process.h>
+
#ifdef _MSC_VER
# pragma warning (disable : 4996)
# define snprintf _snprintf_s
#endif
-void sleep(unsigned int seconds);
+#define sleep iw32_sleep
+void iw32_sleep(unsigned int seconds);
+
+#define pthread_t HANDLE
+#define pthread_create iw32_pthread_create
+#define pthread_join iw32_pthread_join
+int iw32_pthread_create(pthread_t* thread, const void* _unused, void* (*start_routine)(void*), void* arg);
+int iw32_pthread_join(pthread_t thread, void** _unused);
+
+#define pthread_key_t DWORD
+#define pthread_key_create iw32_pthread_key_create
+#define pthread_key_delete iw32_pthread_key_delete
+#define pthread_getspecific TlsGetValue
+#define pthread_setspecific TlsSetValue
+int iw32_pthread_key_create(pthread_key_t* key, void (*_unused)(void*));
+int iw32_pthread_key_delete(pthread_key_t key);
+
+#define pthread_mutex_t CRITICAL_SECTION
+#define pthread_mutexattr_t void
+#define pthread_mutex_init iw32_pthread_mutex_init
+#define pthread_mutex_destroy DeleteCriticalSection
+#define pthread_mutex_lock EnterCriticalSection
+#define pthread_mutex_unlock LeaveCriticalSection
+int iw32_pthread_mutex_init(pthread_mutex_t* mutex, const void* _unused);
+int iw32_pthread_mutex_destroy(pthread_mutex_t* mutex);
+int iw32_pthread_mutex_lock(pthread_mutex_t* mutex);
+int iw32_pthread_mutex_unlock(pthread_mutex_t* mutex);
+
+#define pthread_cond_t iw32_pthread_cond_t
+#define pthread_condattr_t void
+#define pthread_cond_init iw32_pthread_cond_init
+#define pthread_cond_destroy iw32_pthread_cond_destroy
+#define pthread_cond_wait iw32_pthread_cond_wait
+#define pthread_cond_signal iw32_pthread_cond_signal
+#define pthread_cond_broadcast iw32_pthread_cond_broadcast
+typedef struct iw32_pthread_cond_t {
+ CRITICAL_SECTION lock;
+ HANDLE ev[2]; /* 0 == broadcast, 1 == signal */
+ size_t nr_waiting;
+} iw32_pthread_cond_t;
+int iw32_pthread_cond_init(pthread_cond_t* cond, const void* _unused);
+int iw32_pthread_cond_destroy(pthread_cond_t* cond);
+int iw32_pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
+int iw32_pthread_cond_signal(pthread_cond_t* cond);
+int iw32_pthread_cond_broadcast(pthread_cond_t* cond);
+
+#ifdef __cplusplus
+}
+#endif
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.