diff options
author | Hoa V. Dinh <dinh.viet.hoa@gmail.com> | 2014-11-14 23:34:53 -0800 |
---|---|---|
committer | Hoa V. Dinh <dinh.viet.hoa@gmail.com> | 2014-11-14 23:34:53 -0800 |
commit | 56884dfe33cf0e35b8eda105d28c428023782696 (patch) | |
tree | 8cf4d1c0dce5439e603ecf42ef5c6cc5eb1032d9 /src | |
parent | e0ad62c04a0883e389ac31587f4553a5ca983408 (diff) |
Fixed win32 port. Added MCMainThread implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/core/basetypes/MCMainThreadWin32.cpp | 136 | ||||
-rw-r--r-- | src/core/basetypes/MCWin32.cpp | 14 | ||||
-rw-r--r-- | src/core/basetypes/MCWin32.h | 20 |
3 files changed, 164 insertions, 6 deletions
diff --git a/src/core/basetypes/MCMainThreadWin32.cpp b/src/core/basetypes/MCMainThreadWin32.cpp new file mode 100644 index 00000000..8e379a8b --- /dev/null +++ b/src/core/basetypes/MCMainThreadWin32.cpp @@ -0,0 +1,136 @@ +// +// MCMainThreadWin32.cpp +// mailcore2 +// +// Created by Hoa Dinh on 11/11/14. +// Copyright (c) 2014 MailCore. All rights reserved. +// +#include "MCWin32.h" + +#include "MCMainThread.h" + +#include <stdlib.h> +#include <libetpan/libetpan.h> + +#include "MCDefines.h" + +static HWND threadingWindowHandle; +static UINT threadingFiredMessage; +static const LPCWSTR kThreadingWindowClassName = L"ThreadingWindowClass"; + +struct call_after_delay_data { + void (* function)(void *); + void * context; + UINT_PTR timer_id; +}; + +struct main_thread_call_data { + void (* function)(void *); + void * context; + struct mailsem * sem; +}; + +static void main_thread_wrapper(struct main_thread_call_data * data); +static void main_thread_wait_wrapper(struct main_thread_call_data * data); +static void call_after_delay_wrapper(struct call_after_delay_data * data); + +LRESULT CALLBACK ThreadingWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + if (message == threadingFiredMessage) { + struct main_thread_call_data * data = (struct main_thread_call_data *) wParam; + if (data->sem != NULL) { + main_thread_wait_wrapper(data); + } + else { + main_thread_wrapper(data); + } + return 0; + } + else if (message == WM_TIMER) { + struct call_after_delay_data * data = (struct call_after_delay_data *) wParam; + call_after_delay_wrapper(data); + return 0; + } + else { + return DefWindowProc(hWnd, message, wParam, lParam); + } +} + +static void main_thread_wrapper(struct main_thread_call_data * data) +{ + data->function(data->context); + free(data); +} + +void mailcore::callOnMainThread(void (* function)(void *), void * context) +{ + struct main_thread_call_data * data = (struct main_thread_call_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + data->sem = NULL; + PostMessage(threadingWindowHandle, threadingFiredMessage, (WPARAM) data, 0); +} + +static void main_thread_wait_wrapper(struct main_thread_call_data * data) +{ + data->function(data->context); + mailsem_up(data->sem); +} + +void mailcore::callOnMainThreadAndWait(void (* function)(void *), void * context) +{ + struct main_thread_call_data * data = (struct main_thread_call_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + data->sem = mailsem_new(); + PostMessage(threadingWindowHandle, threadingFiredMessage, (WPARAM) data, 0); + + // Wait. + mailsem_down(data->sem); + + mailsem_free(data->sem); + free(data); +} + +static void call_after_delay_wrapper(struct call_after_delay_data * data) +{ + data->function(data->context); + KillTimer(threadingWindowHandle, data->timer_id); + free(data); +} + +static uint64_t generate_timer_id() +{ + static uint64_t uniqueTimerID = 1; + return uniqueTimerID++; +} + +void * mailcore::callAfterDelay(void (* function)(void *), void * context, double time) +{ + struct call_after_delay_data * data = (struct call_after_delay_data *) malloc(sizeof(* data)); + data->function = function; + data->context = context; + UINT_PTR timer_id = (UINT_PTR) generate_timer_id(); + data->timer_id = SetTimer(threadingWindowHandle, timer_id, (int)(time * 1000), NULL); + return data; +} + +void mailcore::cancelDelayedCall(void * delayedCall) +{ + struct call_after_delay_data * data = (struct call_after_delay_data *) delayedCall; + KillTimer(threadingWindowHandle, data->timer_id); + free(data); +} + +INITIALIZE(MainThreadWin32) +{ + WNDCLASSW wcex; + memset(&wcex, 0, sizeof(wcex)); + wcex.lpfnWndProc = ThreadingWindowWndProc; + wcex.lpszClassName = kThreadingWindowClassName; + RegisterClassW(&wcex); + + threadingWindowHandle = CreateWindowW(kThreadingWindowClassName, 0, 0, + CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, 0, 0, 0); + threadingFiredMessage = RegisterWindowMessageW(L"com.libmailcore.mailcore2.MainThreadFired"); +} diff --git a/src/core/basetypes/MCWin32.cpp b/src/core/basetypes/MCWin32.cpp index 3ff085ec..0b9b6ea0 100644 --- a/src/core/basetypes/MCWin32.cpp +++ b/src/core/basetypes/MCWin32.cpp @@ -3,7 +3,7 @@ FILE * mailcore::win32_fopen(const char * filename, const char * mode) { FILE * f = NULL; - int r = fopen_s(&f, filename->fileSystemRepresentation(), "rb"); + int r = fopen_s(&f, filename, "rb"); if (r != 0) { return NULL; } @@ -66,12 +66,14 @@ time_t mailcore::win32_timegm(struct tm *tm) { struct tm * mailcore::win32_gmtime_r(const time_t *clock, struct tm *result) { - return gmtime_s(result, clock); + gmtime_s(result, clock); + return result; } struct tm * mailcore::win32_localtime_r(const time_t *clock, struct tm *result) { - return localtime_s(result, clock); + localtime_s(result, clock); + return result; } char * mailcore::win32_strcasestr(const char * s, const char * find) @@ -141,10 +143,10 @@ pid_t mailcore::win32_getpid(void) static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -char * mailcore::win32_mkdtemp(char *name_template) +char * mailcore::win32_mkdtemp(char *path) { register char *start, *trv, *suffp; - char *pad; + const char *pad; struct stat sbuf; int rval; @@ -189,7 +191,7 @@ char * mailcore::win32_mkdtemp(char *name_template) } for (;;) { - if (mkdir(path, 0700) == 0) + if (mkdir(path) == 0) return path; if (errno != EEXIST) return NULL; diff --git a/src/core/basetypes/MCWin32.h b/src/core/basetypes/MCWin32.h index ceebbe7d..95ed1a6e 100644 --- a/src/core/basetypes/MCWin32.h +++ b/src/core/basetypes/MCWin32.h @@ -6,6 +6,21 @@ #ifdef _MSC_VER +/* +#if WINAPI_FAMILY == WINAPI_FAMILY_PC_APP +#error pc app +#elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP +#error phone app +#elif WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP +#error desktop app +#else +#error other +#endif +*/ + +#undef WINAPI_FAMILY +#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP + #define _CRT_RAND_S #include <stdlib.h> @@ -16,6 +31,10 @@ #include <Winsock2.h> #include <windows.h> #include <rpc.h> +#include <time.h> +#include <sys/stat.h> +#include <errno.h> +#include <direct.h> #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) @@ -25,6 +44,7 @@ #define strdup _strdup #define fileno _fileno #define unlink _unlink +#define mkdir _mkdir #define fopen mailcore::win32_fopen #define gmtime_r mailcore::win32_gmtime_r #define localtime_r mailcore::win32_localtime_r |