qemu_get_thread_id() fell back to getpid() on macOS, so
every thread, including each vCPU reported by query-cpus-fast,
shared the process id. Use pthread_threadid_np(), keeping the
value positive.
For example running '-M virt -smp 2 -accel tcg,thread=multi',
QMP 'query-cpus-fast' returned before:
{"return": [{"thread-id": 58412, "cpu-index": 0, ...},
{"thread-id": 58412, "cpu-index": 1, ...}]}
and now:
{"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
{"thread-id": 75188617, "cpu-index": 1, ...}]}
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
util/oslib-posix.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index dc001da66dd4..71fd369e42f1 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
return _lwp_self();
#elif defined(__OpenBSD__)
return getthrid();
+#elif defined(__APPLE__)
+ uint64_t tid;
+ pthread_threadid_np(NULL, &tid);
+ return tid & INT_MAX;
#else
return getpid();
#endif
--
2.55.0
On Thu, Nov 26, 2026 at 11:00:01PM +0100, Philippe Mathieu-Daudé wrote:
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
>
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
>
> {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
> {"thread-id": 58412, "cpu-index": 1, ...}]}
>
> and now:
>
> {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
> {"thread-id": 75188617, "cpu-index": 1, ...}]}
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
> util/oslib-posix.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index dc001da66dd4..71fd369e42f1 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
> return _lwp_self();
> #elif defined(__OpenBSD__)
> return getthrid();
> +#elif defined(__APPLE__)
> + uint64_t tid;
> + pthread_threadid_np(NULL, &tid);
> + return tid & INT_MAX;
> #else
I wonder how likely it is that a thread ID would exceed 'int' ?
IOW, do we need to consider changing return type to uint64_t
> return getpid();
Separately, it is probably worth also adding
warn_report_once("No qemu_get_thread_id() impl on this platform")
so that we nag people to fix remaining platform gaps they
might care about.
With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange :|
|: https://libvirt.org ~~ https://entangle-photo.org :|
|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
On 27/11/26 10:25, Daniel P. Berrangé wrote:
> On Thu, Nov 26, 2026 at 11:00:01PM +0100, Philippe Mathieu-Daudé wrote:
>> qemu_get_thread_id() fell back to getpid() on macOS, so
>> every thread, including each vCPU reported by query-cpus-fast,
>> shared the process id. Use pthread_threadid_np(), keeping the
>> value positive.
>>
>> For example running '-M virt -smp 2 -accel tcg,thread=multi',
>> QMP 'query-cpus-fast' returned before:
>>
>> {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
>> {"thread-id": 58412, "cpu-index": 1, ...}]}
>>
>> and now:
>>
>> {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
>> {"thread-id": 75188617, "cpu-index": 1, ...}]}
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
>> ---
>> util/oslib-posix.c | 4 ++++
>> 1 file changed, 4 insertions(+)
>>
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index dc001da66dd4..71fd369e42f1 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
>> return _lwp_self();
>> #elif defined(__OpenBSD__)
>> return getthrid();
>> +#elif defined(__APPLE__)
>> + uint64_t tid;
>> + pthread_threadid_np(NULL, &tid);
>> + return tid & INT_MAX;
>> #else
>
> I wonder how likely it is that a thread ID would exceed 'int' ?
IIUC macOS thread ids come from one system-wide 64-bit counter that
started at boot, and it never reuses a value. A long-running host that
creates many threads could get there. Going past INT_MAX is possible
but still rare.
> IOW, do we need to consider changing return type to uint64_t
Yeah I think so. The good news, QAPI thread-id is 'int', which is
int64 on the wire, so QMP needs no change.
Widening to int64_t is a separate cleanup across those structs, and
the mask is enough for now.
>
>> return getpid();
>
> Separately, it is probably worth also adding
>
> warn_report_once("No qemu_get_thread_id() impl on this platform")
>
> so that we nag people to fix remaining platform gaps they
> might care about.
OK.
>
> With regards,
> Daniel
On 11/26/2026 2:00 PM, Philippe Mathieu-Daudé wrote:
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
>
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
>
> {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
> {"thread-id": 58412, "cpu-index": 1, ...}]}
>
> and now:
>
> {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
> {"thread-id": 75188617, "cpu-index": 1, ...}]}
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
> util/oslib-posix.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
> On 26. Nov 2026, at 23:00, Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> wrote:
>
> qemu_get_thread_id() fell back to getpid() on macOS, so
> every thread, including each vCPU reported by query-cpus-fast,
> shared the process id. Use pthread_threadid_np(), keeping the
> value positive.
>
> For example running '-M virt -smp 2 -accel tcg,thread=multi',
> QMP 'query-cpus-fast' returned before:
>
> {"return": [{"thread-id": 58412, "cpu-index": 0, ...},
> {"thread-id": 58412, "cpu-index": 1, ...}]}
>
> and now:
>
> {"return": [{"thread-id": 75188616, "cpu-index": 0, ...},
> {"thread-id": 75188617, "cpu-index": 1, ...}]}
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Reviewed-by: Mohamed Mediouni <mohamed.mediouni@linaro.org>
> ---
> util/oslib-posix.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index dc001da66dd4..71fd369e42f1 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -107,6 +107,10 @@ int qemu_get_thread_id(void)
> return _lwp_self();
> #elif defined(__OpenBSD__)
> return getthrid();
> +#elif defined(__APPLE__)
> + uint64_t tid;
> + pthread_threadid_np(NULL, &tid);
> + return tid & INT_MAX;
> #else
> return getpid();
> #endif
> --
> 2.55.0
>
© 2016 - 2026 Red Hat, Inc.