[PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS

Philippe Mathieu-Daudé posted 1 patch 1 day, 3 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261126220001.58932-1-philmd@oss.qualcomm.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>
util/oslib-posix.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
Posted by Philippe Mathieu-Daudé 1 day, 3 hours ago
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


Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
Posted by Daniel P. Berrangé 15 hours ago
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 :|


Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
Posted by Philippe Mathieu-Daudé 8 hours ago
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


Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
Posted by Pierrick Bouvier 1 day, 2 hours ago
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>

Re: [PATCH] util/oslib-posix: Have qemu_get_thread_id return the thread id on macOS
Posted by Mohamed Mediouni 1 day, 2 hours ago

> 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
>