commit f1ffd2e889c9c263659f7df80bf61c5bb4b175fd
parent 7c2493d59c5d1d54f9aa0a2739fe6a539273edf8
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Wed, 24 Dec 2014 19:00:33 +0000
[w32] Set thread name
Diffstat:
3 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/src/include/w32functions.h b/src/include/w32functions.h
@@ -194,6 +194,19 @@ int MHD_W32_random_(void);
/* Emulate snprintf function on W32 */
int W32_snprintf(char *__restrict s, size_t n, const char *__restrict format, ...);
+#ifndef _MSC_FULL_VER
+/* Thread name available only for VC-compiler */
+static void W32_SetThreadName(const DWORD thread_id, const char *thread_name)
+{ }
+#else /* _MSC_FULL_VER */
+/**
+ * Set thread name
+ * @param thread_id ID of thread, -1 for current thread
+ * @param thread_name name to set
+ */
+void W32_SetThreadName(const DWORD thread_id, const char *thread_name);
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -1110,9 +1110,15 @@ create_thread (MHD_thread_handle_ *thread,
errno = EINVAL;
return ret;
#elif defined(MHD_USE_W32_THREADS)
+ DWORD threadID;
*thread = CreateThread(NULL, daemon->thread_stack_size, start_routine,
- arg, 0, NULL);
- return (NULL != (*thread)) ? 0 : 1;
+ arg, 0, &threadID);
+ if (NULL == (*thread))
+ return EINVAL;
+
+ W32_SetThreadName(threadID, "libmicrohttpd");
+
+ return 0;
#endif
}
diff --git a/src/platform/w32functions.c b/src/platform/w32functions.c
@@ -666,3 +666,39 @@ int W32_snprintf(char *__restrict s, size_t n, const char *__restrict format, ..
return ret;
}
+
+#ifdef _MSC_FULL_VER
+/**
+ * Set thread name
+ * @param thread_id ID of thread, -1 for current thread
+ * @param thread_name name to set
+ */
+void W32_SetThreadName(const DWORD thread_id, const char *thread_name)
+{
+ static const DWORD VC_SETNAME_EXC = 0x406D1388;
+#pragma pack(push,8)
+ struct thread_info_struct
+ {
+ DWORD type; // Must be 0x1000.
+ LPCSTR name; // Pointer to name (in user address space).
+ DWORD ID; // Thread ID (-1=caller thread).
+ DWORD flags; // Reserved for future use, must be zero.
+ } thread_info;
+#pragma pack(pop)
+
+ if (NULL == thread_name)
+ return;
+
+ thread_info.type = 0x1000;
+ thread_info.name = thread_name;
+ thread_info.ID = thread_id;
+ thread_info.flags = 0;
+
+ __try
+ { /* This exception is intercepted by debugger */
+ RaiseException(VC_SETNAME_EXC, 0, sizeof(thread_info) / sizeof(ULONG_PTR), (ULONG_PTR*)&thread_info);
+ }
+ __except (EXCEPTION_EXECUTE_HANDLER)
+ {}
+}
+#endif /* _MSC_FULL_VER */