| 340 | /* ── Per-thread CPU time ──────────────────────────────────────── */ |
| 341 | |
| 342 | uint64_t cbm_thread_cpu_time_ns(void) { |
| 343 | #ifdef _WIN32 |
| 344 | FILETIME creation, exit_time, kernel, user; |
| 345 | if (!GetThreadTimes(GetCurrentThread(), &creation, &exit_time, &kernel, &user)) { |
| 346 | return 0; |
| 347 | } |
| 348 | /* kernel/user each carry a 64-bit count of 100-ns ticks split across a |
| 349 | * FILETIME's two DWORDs; recombine, sum, and scale to nanoseconds. The |
| 350 | * shift-combine avoids casting to ULARGE_INTEGER (alignment-unsafe per |
| 351 | * MSDN, and its union members trip cppcheck's unreadVariable). */ |
| 352 | uint64_t kernel_ticks = ((uint64_t)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime; |
| 353 | uint64_t user_ticks = ((uint64_t)user.dwHighDateTime << 32) | user.dwLowDateTime; |
| 354 | return (kernel_ticks + user_ticks) * 100ULL; |
| 355 | #else |
| 356 | struct timespec ts; |
| 357 | if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) { |
| 358 | return 0; |
| 359 | } |
| 360 | return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | /* ── getline (Windows lacks it) ───────────────────────────────── */ |
| 365 |
no outgoing calls
no test coverage detected