Returns the number of threads running in the process, or 0 to indicate that we cannot detect it.
| 7883 | // Returns the number of threads running in the process, or 0 to indicate that |
| 7884 | // we cannot detect it. |
| 7885 | size_t GetThreadCount() { |
| 7886 | const task_t task = mach_task_self(); |
| 7887 | mach_msg_type_number_t thread_count; |
| 7888 | thread_act_array_t thread_list; |
| 7889 | const kern_return_t status = task_threads(task, &thread_list, &thread_count); |
| 7890 | if (status == KERN_SUCCESS) { |
| 7891 | // task_threads allocates resources in thread_list and we need to free them |
| 7892 | // to avoid leaks. |
| 7893 | vm_deallocate(task, |
| 7894 | reinterpret_cast<vm_address_t>(thread_list), |
| 7895 | sizeof(thread_t) * thread_count); |
| 7896 | return static_cast<size_t>(thread_count); |
| 7897 | } else { |
| 7898 | return 0; |
| 7899 | } |
| 7900 | } |
| 7901 | |
| 7902 | #else |
| 7903 |