11// SPDX-License-Identifier: Apache-2.0
22// ----------------------------------------------------------------------------
3- // Copyright 2011-2023 Arm Limited
3+ // Copyright 2011-2024 Arm Limited
44//
55// Licensed under the Apache License, Version 2.0 (the "License"); you may not
66// use this file except in compliance with the License. You may obtain a copy
3939
4040#define WIN32_LEAN_AND_MEAN
4141#include < windows.h>
42+ #include < Processthreadsapi.h>
43+ #include < algorithm>
44+ #include < cstring>
4245
4346/* * @brief Alias pthread_t to one of the internal Windows types. */
4447typedef HANDLE pthread_t ;
@@ -58,7 +61,7 @@ static int pthread_create(
5861 static_cast <void >(attribs);
5962 LPTHREAD_START_ROUTINE func = reinterpret_cast <LPTHREAD_START_ROUTINE>(threadfunc);
6063 *thread = CreateThread (nullptr , 0 , func, thread_arg, 0 , nullptr );
61-
64+
6265 // Ensure we return 0 on success, non-zero on error
6366 if (*thread == NULL )
6467 {
@@ -142,6 +145,24 @@ double get_time()
142145 return static_cast <double >(ticks) / 1.0e7 ;
143146}
144147
148+ /* See header for documentation */
149+ void set_thread_name (
150+ const char * name
151+ ) {
152+ // Names are limited to 16 characters
153+ wchar_t wname [17 ] { 0 };
154+ size_t name_len = std::strlen (name);
155+ size_t clamp_len = std::min (name_len, 16 );
156+
157+ // We know we only have basic 7-bit ASCII so just widen
158+ for (size_t i = 0 ; i < clamp_len; i++)
159+ {
160+ wname = static_cast <wchar_t >(name[i]);
161+ }
162+
163+ SetThreadDescription (GetCurrentThread (), wname);
164+ }
165+
145166/* ============================================================================
146167 Platform code for an platform using POSIX APIs.
147168============================================================================ */
@@ -165,6 +186,13 @@ double get_time()
165186 return static_cast <double >(tv.tv_sec ) + static_cast <double >(tv.tv_usec ) * 1.0e-6 ;
166187}
167188
189+ /* See header for documentation */
190+ void set_thread_name (
191+ const char * name
192+ ) {
193+ pthread_setname_np (pthread_self (), name);
194+ }
195+
168196#endif
169197
170198/* *
@@ -215,9 +243,9 @@ void launch_threads(
215243 }
216244
217245 // Otherwise spawn worker threads
218- launch_desc *thread_descs = new launch_desc[thread_count];
246+ launch_desc *thread_descs = new launch_desc[thread_count];
219247 int actual_thread_count { 0 };
220-
248+
221249 for (int i = 0 ; i < thread_count; i++)
222250 {
223251 thread_descs[actual_thread_count].thread_count = thread_count;
@@ -230,7 +258,7 @@ void launch_threads(
230258 &(thread_descs[actual_thread_count].thread_handle ),
231259 nullptr ,
232260 launch_threads_helper,
233- reinterpret_cast <void *>(thread_descs + actual_thread_count));
261+ reinterpret_cast <void *>(thread_descs + actual_thread_count));
234262
235263 // Track how many threads we actually created
236264 if (!error)
@@ -248,7 +276,7 @@ void launch_threads(
248276
249277 // If we did not create thread_count threads then emit a warning
250278 if (actual_thread_count != thread_count)
251- {
279+ {
252280 int log_count = actual_thread_count == 0 ? 1 : actual_thread_count;
253281 const char * log_s = log_count == 1 ? " " : " s" ;
254282 printf (" WARNING: %s using %d thread%s due to thread creation error\n\n " ,
0 commit comments