previously, stdio used spinlocks, which would be unacceptable if we ever add support for thread priorities, and which yielded pathologically bad performance if an application attempted to use flockfile on a key file as a major/primary locking mechanism. i had held off on making this change for fear that it would hurt performance in the non-threaded case, but actually support for recursive locking had already inflicted that cost. by having the internal locking functions store a flag indicating whether they need to perform unlocking, rather than using the actual recursive lock counter, i was able to combine the conditionals at unlock time, eliminating any additional cost, and also avoid a nasty corner case where a huge number of calls to ftrylockfile could cause deadlock later at the point of internal locking. this commit also fixes some issues with usage of pthread_self conflicting with __attribute__((const)) which resulted in crashes with some compiler versions/optimizations, mainly in flockfile prior to pthread_create.
40 lines
924 B
C
40 lines
924 B
C
#include "pthread_impl.h"
|
|
|
|
static struct pthread main_thread;
|
|
|
|
/* pthread_key_create.c overrides this */
|
|
static const void *dummy[1] = { 0 };
|
|
weak_alias(dummy, __pthread_tsd_main);
|
|
|
|
static int *errno_location()
|
|
{
|
|
return __pthread_self()->errno_ptr;
|
|
}
|
|
|
|
static int init_main_thread()
|
|
{
|
|
if (__set_thread_area(&main_thread) < 0) return -1;
|
|
main_thread.canceldisable = libc.canceldisable;
|
|
main_thread.tsd = (void **)__pthread_tsd_main;
|
|
main_thread.self = libc.main_thread = &main_thread;
|
|
main_thread.errno_ptr = __errno_location();
|
|
libc.errno_location = errno_location;
|
|
main_thread.tid = main_thread.pid =
|
|
__syscall(SYS_set_tid_address, &main_thread.tid);
|
|
return 0;
|
|
}
|
|
|
|
pthread_t pthread_self()
|
|
{
|
|
static int init, failed;
|
|
if (!init) {
|
|
if (failed) return 0;
|
|
if (init_main_thread() < 0) failed = 1;
|
|
if (failed) return 0;
|
|
init = 1;
|
|
}
|
|
return __pthread_self();
|
|
}
|
|
|
|
weak_alias(pthread_self, __pthread_self_init);
|