| 229 | #endif |
| 230 | |
| 231 | TRACY_API const char* GetThreadName( uint32_t id ) |
| 232 | { |
| 233 | static char buf[256]; |
| 234 | #ifdef TRACY_ENABLE |
| 235 | auto ptr = GetThreadNameData().load( std::memory_order_relaxed ); |
| 236 | while( ptr ) |
| 237 | { |
| 238 | if( ptr->id == id ) |
| 239 | { |
| 240 | return ptr->name; |
| 241 | } |
| 242 | ptr = ptr->next; |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #if defined _WIN32 |
| 247 | # ifdef TRACY_UWP |
| 248 | static auto _GetThreadDescription = &::GetThreadDescription; |
| 249 | # else |
| 250 | static auto _GetThreadDescription = (t_GetThreadDescription)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "GetThreadDescription" ); |
| 251 | # endif |
| 252 | if( _GetThreadDescription ) |
| 253 | { |
| 254 | auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)id ); |
| 255 | if( hnd != 0 ) |
| 256 | { |
| 257 | PWSTR tmp; |
| 258 | if( SUCCEEDED( _GetThreadDescription( hnd, &tmp ) ) ) |
| 259 | { |
| 260 | auto ret = wcstombs( buf, tmp, 256 ); |
| 261 | CloseHandle( hnd ); |
| 262 | LocalFree( tmp ); |
| 263 | if( ret != static_cast<size_t>( -1 ) ) |
| 264 | { |
| 265 | return buf; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | #elif defined __linux__ |
| 271 | int cs, fd; |
| 272 | char path[32]; |
| 273 | snprintf( path, sizeof( path ), "/proc/self/task/%d/comm", id ); |
| 274 | sprintf( buf, "%" PRIu32, id ); |
| 275 | # ifndef __ANDROID__ |
| 276 | pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &cs ); |
| 277 | # endif |
| 278 | if ( ( fd = open( path, O_RDONLY ) ) > 0) { |
| 279 | int len = read( fd, buf, 255 ); |
| 280 | if( len > 0 ) |
| 281 | { |
| 282 | buf[len] = 0; |
| 283 | if( len > 1 && buf[len-1] == '\n' ) |
| 284 | { |
| 285 | buf[len-1] = 0; |
| 286 | } |
| 287 | } |
| 288 | close( fd ); |
no test coverage detected