Make profiling more robust with many tasks (#42978)
This patch includes two sets of changes.
(1) `jl_thread_suspend_and_get_state` uses `pthread_cond_timedwait` to
recover from the case where the request is not received by the signal
handler. This is required because `usr2_handler` contains some paths
for the case where it is not possible to obtain `ptls`.
(2) `ctx_switch` now makes sure to null out `ptls` of the last task
(`lastt->ptls = NULL`) after changing the current task by updating
pgcstack (`jl_set_pgcstack(&t->gcstack)`). This closes the gap in
which `usr2_handler` can observe the null `ptls`.
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
(cherry picked from commit 81315807a5bb0c581d85d4fbaa4f09cf5dccfca4)