aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac12
-rw-r--r--src/blogc-runserver/main.c4
-rw-r--r--src/common/thread_pthread.c2
-rw-r--r--src/common/thread_windows.c111
4 files changed, 121 insertions, 8 deletions
diff --git a/configure.ac b/configure.ac
index fc6c2ad..21e6549 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,12 +134,12 @@ RUNSERVER="disabled"
AC_ARG_ENABLE([runserver], AS_HELP_STRING([--enable-runserver],
[build blogc-runserver tool]))
AS_IF([test "x$enable_runserver" = "xyes"], [
- AC_CHECK_HEADERS([signal.h limits.h fcntl.h unistd.h sys/stat.h sys/types.h sys/socket.h netinet/in.h arpa/inet.h],, [
- AC_MSG_ERROR([blogc-runserver tool requested but required headers not found])
- ])
- AX_PTHREAD([], [
- AC_MSG_ERROR([blogc-runserver tool requested but pthread is not supported])
- ])
+ #AC_CHECK_HEADERS([signal.h limits.h fcntl.h unistd.h sys/stat.h sys/types.h sys/socket.h netinet/in.h arpa/inet.h],, [
+ # AC_MSG_ERROR([blogc-runserver tool requested but required headers not found])
+ #])
+ #AX_PTHREAD([], [
+ # AC_MSG_ERROR([blogc-runserver tool requested but pthread is not supported])
+ #])
RUNSERVER="enabled"
have_runserver=yes
])
diff --git a/src/blogc-runserver/main.c b/src/blogc-runserver/main.c
index df0305e..deb8118 100644
--- a/src/blogc-runserver/main.c
+++ b/src/blogc-runserver/main.c
@@ -48,11 +48,13 @@ print_usage(void)
int
main(int argc, char **argv)
{
+#if !defined(WIN32) && !defined(_WIN32)
struct sigaction new_action;
new_action.sa_handler = SIG_IGN;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGPIPE, &new_action, NULL);
+#endif
int rv = 0;
char *host = NULL;
@@ -98,7 +100,7 @@ main(int argc, char **argv)
max_threads = strtoul(ptr, &endptr, 10);
if (*ptr != '\0' && *endptr != '\0')
fprintf(stderr, "blogc-runserver: warning: invalid value "
- "for -m argument: %s. using %zu instead\n", ptr, max_threads);
+ "for -m argument: %s. using %d instead\n", ptr, (int)max_threads);
break;
default:
print_usage();
diff --git a/src/common/thread_pthread.c b/src/common/thread_pthread.c
index a85a0c7..274c1a7 100644
--- a/src/common/thread_pthread.c
+++ b/src/common/thread_pthread.c
@@ -40,7 +40,7 @@ bc_thread_create(bc_thread_func_t func, void *arg, bool detached, bc_error_t **e
if (0 != (r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
*err = bc_error_new_printf(BC_ERROR_THREAD,
- "Failed to mark thread as detached: %s", strerror(r));
+ "Failed to detach thread: %s", strerror(r));
return NULL;
}
diff --git a/src/common/thread_windows.c b/src/common/thread_windows.c
new file mode 100644
index 0000000..aae2da8
--- /dev/null
+++ b/src/common/thread_windows.c
@@ -0,0 +1,111 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2014-2018 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <stdbool.h>
+#include <windows.h>
+#include "error.h"
+#include "utils.h"
+#include "thread.h"
+
+struct _bc_thread_t {
+ HANDLE thread;
+};
+
+struct wrapper_arg {
+ bc_thread_func_t func;
+ void *arg;
+};
+
+
+static DWORD WINAPI
+wrapper(LPVOID arg)
+{
+ if (arg != NULL) {
+ struct wrapper_arg *r = arg;
+ r->func(r->arg);
+ free(r);
+ }
+ return 0;
+}
+
+
+bc_thread_t*
+bc_thread_create(bc_thread_func_t func, void *arg, bool detached, bc_error_t **err)
+{
+ if (err == NULL || *err != NULL)
+ return NULL;
+
+ struct wrapper_arg *a = bc_malloc(sizeof(struct wrapper_arg));
+ a->func = func;
+ a->arg = arg;
+
+ bc_thread_t *rv = bc_malloc(sizeof(bc_thread_t));
+ rv->thread = CreateThread(NULL, 0, wrapper, a, 0, NULL);
+ if (rv->thread == NULL) {
+ LPTSTR buf;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)&buf, 0, NULL);
+ *err = bc_error_new_printf(BC_ERROR_THREAD,
+ "Failed to create thread: %s", buf);
+ LocalFree(buf);
+ free(rv);
+ free(a);
+ return NULL;
+ }
+
+ if (detached && !CloseHandle(rv->thread)) {
+ LPTSTR buf;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)&buf, 0, NULL);
+ *err = bc_error_new_printf(BC_ERROR_THREAD,
+ "Failed to detach thread: %s", buf);
+ LocalFree(buf);
+ free(rv);
+ free(a);
+ return NULL;
+ }
+
+ return rv;
+}
+
+
+void
+bc_thread_join(bc_thread_t *thread, bc_error_t **err)
+{
+ if (thread == NULL || err == NULL || *err != NULL)
+ return;
+
+ DWORD r = WaitForSingleObject(thread->thread, INFINITE);
+ switch (r) {
+ case WAIT_OBJECT_0:
+ break;
+ case WAIT_FAILED:
+ {
+ LPTSTR buf;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)&buf, 0, NULL);
+ *err = bc_error_new_printf(BC_ERROR_THREAD,
+ "Failed to join thread: %s", buf);
+ LocalFree(buf);
+ break;
+ }
+ default:
+ *err = bc_error_new_printf(BC_ERROR_THREAD, "Failed to join thread");
+ break;
+ }
+}
+
+
+void
+bc_thread_free(bc_thread_t *thread)
+{
+ free(thread);
+}