|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +#if defined(__linux__) |
| 18 | +#define _BSD_SOURCE |
| 19 | +#define _GNU_SOURCE |
| 20 | +#endif |
| 21 | + |
17 | 22 | #include "concurrent/aeron_thread.h"
|
18 | 23 |
|
19 | 24 | void aeron_nano_sleep(uint64_t nanoseconds)
|
@@ -42,6 +47,16 @@ void aeron_nano_sleep(uint64_t nanoseconds)
|
42 | 47 | }
|
43 | 48 |
|
44 | 49 | #if defined(AERON_COMPILER_GCC)
|
| 50 | + |
| 51 | +void aeron_thread_set_name(const char* role_name) |
| 52 | +{ |
| 53 | +#if defined(Darwin) |
| 54 | + pthread_setname_np(role_name); |
| 55 | +#else |
| 56 | + pthread_setname_np(pthread_self(), role_name); |
| 57 | +#endif |
| 58 | +} |
| 59 | + |
45 | 60 | #elif defined(AERON_COMPILER_MSVC) && defined(AERON_CPU_X64)
|
46 | 61 |
|
47 | 62 | static BOOL aeron_thread_once_callback(PINIT_ONCE init_once, void (*callback)(void), void** context)
|
@@ -75,38 +90,62 @@ int aeron_thread_attr_init(pthread_attr_t* attr)
|
75 | 90 | return 0;
|
76 | 91 | }
|
77 | 92 |
|
| 93 | +static DWORD WINAPI aeron_thread_proc(LPVOID parameter) |
| 94 | +{ |
| 95 | + aeron_thread_t* thread = (aeron_thread_t*)parameter; |
| 96 | + thread->result = thread->callback(thread->arg0); |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
78 | 100 | int aeron_thread_create(aeron_thread_t* thread, void* attr, void*(*callback)(void*), void* arg0)
|
79 | 101 | {
|
| 102 | + thread->callback = callback; |
| 103 | + thread->arg0 = arg0; |
| 104 | + |
80 | 105 | DWORD id;
|
81 |
| - *thread = CreateThread( |
| 106 | + thread->handle = CreateThread( |
82 | 107 | NULL, // default security attributes
|
83 | 108 | 0, // use default stack size
|
84 |
| - callback, // thread function name |
85 |
| - arg0, // argument to thread function |
| 109 | + aeron_thread_proc, // thread function name |
| 110 | + thread, // argument to thread function |
86 | 111 | 0, // use default creation flags
|
87 | 112 | &id); // returns the thread identifier
|
88 | 113 |
|
89 |
| - return 0; |
| 114 | + return thread->handle ? 0 : -1; |
90 | 115 | }
|
91 | 116 |
|
92 |
| -void aeron_thread_set_name(aeron_thread_t self, const char* role_name) |
| 117 | +void aeron_thread_set_name(const char* role_name) |
93 | 118 | {
|
94 | 119 | size_t wn = mbstowcs(NULL, role_name, 0);
|
95 | 120 | wchar_t * buf = malloc(sizeof(wchar_t) * (wn + 1)); // value-initialize to 0 (see below)
|
| 121 | + if (!buf) |
| 122 | + { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
96 | 126 | mbstowcs(buf, role_name, wn + 1);
|
97 |
| - SetThreadDescription(self, buf); |
| 127 | + SetThreadDescription(GetCurrentThread(), buf); |
98 | 128 |
|
99 | 129 | free(buf);
|
100 | 130 | }
|
101 | 131 |
|
102 |
| -aeron_thread_t aeron_thread_self() |
| 132 | +int aeron_thread_join(aeron_thread_t thread, void **value_ptr) |
103 | 133 | {
|
104 |
| - return GetCurrentThread(); |
105 |
| -} |
| 134 | + if (thread.handle) |
| 135 | + { |
| 136 | + WaitForSingleObject(thread.handle, INFINITE); |
| 137 | + CloseHandle(thread.handle); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + return EINVAL; |
| 142 | + } |
| 143 | + |
| 144 | + if (value_ptr) |
| 145 | + { |
| 146 | + *value_ptr = thread.result; |
| 147 | + } |
106 | 148 |
|
107 |
| -DWORD aeron_thread_join(aeron_thread_t thread, void **value_ptr) |
108 |
| -{ |
109 |
| - WaitForSingleObject(thread, INFINITE); |
110 | 149 | return 0;
|
111 | 150 | }
|
112 | 151 |
|
|
0 commit comments