Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread_POSIX.cpp shouldn't convert thread IDs to long #3857

Closed
arrowd opened this issue Nov 3, 2022 · 7 comments
Closed

Thread_POSIX.cpp shouldn't convert thread IDs to long #3857

arrowd opened this issue Nov 3, 2022 · 7 comments
Assignees

Comments

@arrowd
Copy link
Contributor

arrowd commented Nov 3, 2022

According to the POSIX standard returning value of pthread_self() is opaque and can't be casted to long. See NOTES section of pthread_self(3) and pthread_equal(3) man pages.

The code of ThreadImpl::currentOsTidImpl() causes compilation to fail on FreeBSD with

poco/Foundation/src/Thread_POSIX.cpp:326:12: error: cannot initialize return object of type 'long' with an rvalue of type 'pthread_t' (aka 'pthread *')
    return ::pthread_self();
           ^~~~~~~~~~~~~~~~

On the other hand, the method is called currentOsTidImpl and on Linux it calls gettid() syscall. If this method is intended to return kernel thread identifier, then the #else case shouldn't call pthread_self() and instead have #error Implement this for your OS. I'm happy to prepare a pull request if my analysis is right.

@aleks-f aleks-f added the bug label Nov 5, 2022
@aleks-f
Copy link
Member

aleks-f commented Nov 5, 2022

@arrowd I think there should be a warning and return 0; discarding any OS except Linux and MacOS over this is a too harsh measure
/cc @obiltschnig

@neotron
Copy link
Contributor

neotron commented Mar 29, 2023

On FreeBSD specifically you can add a case using this:

return ::pthread_getthreadid_np();

defined in <pthread_np.h>

@arrowd
Copy link
Contributor Author

arrowd commented Mar 29, 2023

The point is that an ID returned from pthread_* isn't the same thing as an ID returned by gettid(). So if the Linux case uses gettid, then all other cases can't use phtread_*.

@neotron
Copy link
Contributor

neotron commented Mar 29, 2023

I do agree pthread_self() shouldn't be used as a numerical thread id since it's not intended to be used that way. pthread_getthreadid_np() on the other hand does return the unique numerical id of the thread.

@arrowd
Copy link
Contributor Author

arrowd commented Mar 29, 2023

It is still a different thing compared to gettid. FreeBSD case should use thr_self() here.

@neotron
Copy link
Contributor

neotron commented Mar 29, 2023

I was not aware of the thr_self() method. That said when testing both they seem to return the same value, at least in my test case:

*** thr_self=101298, getthreadid=101298

thr_self() seems like the better option given that it better matches the Linux version, and returns a long rather than int (in the unlikely case thread id would exceed a signed int).

https://github.com/freebsd/freebsd-src/blob/d4a80d21b3d32a2de02d1820cc1f38dba1f127cb/sys/kern/kern_thr.c#L297
https://github.com/freebsd/freebsd-src/blob/d4a80d21b3d32a2de02d1820cc1f38dba1f127cb/lib/libthr/thread/thr_getthreadid_np.c#L51

Copy link

This issue is stale because it has been open for 365 days with no activity.

@github-actions github-actions bot added the stale label Mar 29, 2024
@aleks-f aleks-f removed the stale label Mar 30, 2024
@aleks-f aleks-f added this to the Release 1.13.3 milestone Mar 30, 2024
aleks-f added a commit that referenced this issue Mar 30, 2024
@aleks-f aleks-f self-assigned this Mar 30, 2024
@aleks-f aleks-f mentioned this issue Apr 2, 2024
aleks-f added a commit that referenced this issue Apr 2, 2024
* fix(Thread_POSIX): Thread_POSIX.cpp shouldn't convert thread IDs to long #3857;
FreeBSD build errors fixes

* chore(StreamTokenizerTest): fix warnings

* fix(build): FreeBSD config

* fix(ThreadTest): some tests checking nothing; temporatily comment FreeBSD threadStackSize test (segfaults on pthread_join)

* fix(Thread_POSIX): handle emscripten in currentOsTidImpl

* chore: fix emscripten define

* chore: fix some clang warnings; add sanitizer flag to FreeBSD linux compat build config
aleks-f added a commit that referenced this issue Apr 2, 2024
* fix(Thread_POSIX): Thread_POSIX.cpp shouldn't convert thread IDs to long #3857;
FreeBSD build errors fixes

* chore(StreamTokenizerTest): fix warnings

* fix(build): FreeBSD config

* fix(ThreadTest): some tests checking nothing; temporatily comment FreeBSD threadStackSize test (segfaults on pthread_join)

* fix(Thread_POSIX): handle emscripten in currentOsTidImpl

* chore: fix emscripten define

* chore: fix some clang warnings; add sanitizer flag to FreeBSD linux compat build config
@aleks-f aleks-f added the fixed label Apr 2, 2024
@aleks-f aleks-f added this to 1.13 Apr 2, 2024
@aleks-f aleks-f moved this to Done in 1.13 Apr 2, 2024
@matejk matejk closed this as completed Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants