提交 df7d0dfb 编写于 作者: J Jens Gustedt 提交者: Rich Felker

use weak symbols for the POSIX functions that will be used by C threads

The intent of this is to avoid name space pollution of the C threads
implementation.

This has two sides to it. First we have to provide symbols that wouldn't
pollute the name space for the C threads implementation. Second we have
to clean up some internal uses of POSIX functions such that they don't
implicitly drag in such symbols.
上级 6eb19505
......@@ -2,10 +2,12 @@
#include "libc.h"
#include "syscall.h"
int mprotect(void *addr, size_t len, int prot)
int __mprotect(void *addr, size_t len, int prot)
{
size_t start, end;
start = (size_t)addr & -PAGE_SIZE;
end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
return syscall(SYS_mprotect, start, end-start, prot);
}
weak_alias(__mprotect, mprotect);
......@@ -4,6 +4,9 @@
#include "futex.h"
#include "syscall.h"
int __pthread_setcancelstate(int, int *);
int __clock_gettime(clockid_t, struct timespec *);
int __timedwait(volatile int *addr, int val,
clockid_t clk, const struct timespec *at,
void (*cleanup)(void *), void *arg, int priv)
......@@ -15,7 +18,7 @@ int __timedwait(volatile int *addr, int val,
if (at) {
if (at->tv_nsec >= 1000000000UL) return EINVAL;
if (clock_gettime(clk, &to)) return EINVAL;
if (__clock_gettime(clk, &to)) return EINVAL;
to.tv_sec = at->tv_sec - to.tv_sec;
if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
to.tv_sec--;
......@@ -25,7 +28,7 @@ int __timedwait(volatile int *addr, int val,
top = &to;
}
if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
if (!cleanup) __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
pthread_cleanup_push(cleanup, arg);
r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
......@@ -33,7 +36,7 @@ int __timedwait(volatile int *addr, int val,
if (r != EINTR && r != ETIMEDOUT) r = 0;
pthread_cleanup_pop(0);
if (!cleanup) pthread_setcancelstate(cs, 0);
if (!cleanup) __pthread_setcancelstate(cs, 0);
return r;
}
#include "pthread_impl.h"
void __pthread_testcancel(void);
int __pthread_mutex_lock(pthread_mutex_t *);
int __pthread_mutex_unlock(pthread_mutex_t *);
/*
* struct waiter
*
......@@ -119,7 +123,7 @@ static void unwait(void *arg)
}
}
int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
{
struct waiter node = { .cond = c, .mutex = m };
int e, seq, *fut, clock = c->_c_clock;
......@@ -130,7 +134,7 @@ int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict
if (ts && ts->tv_nsec >= 1000000000UL)
return EINVAL;
pthread_testcancel();
__pthread_testcancel();
if (c->_c_shared) {
node.shared = 1;
......@@ -151,7 +155,7 @@ int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict
unlock(&c->_c_lock);
}
pthread_mutex_unlock(m);
__pthread_mutex_unlock(m);
do e = __timedwait(fut, seq, clock, ts, unwait, &node, !node.shared);
while (*fut==seq && (!e || e==EINTR));
......@@ -197,3 +201,5 @@ int __private_cond_signal(pthread_cond_t *c, int n)
return 0;
}
weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
......@@ -5,6 +5,10 @@
#include <sys/mman.h>
#include <string.h>
void *__mmap(void *, size_t, int, int, int, off_t);
int __munmap(void *, size_t);
int __mprotect(void *, size_t, int);
static void dummy_0()
{
}
......@@ -14,7 +18,7 @@ weak_alias(dummy_0, __pthread_tsd_run_dtors);
weak_alias(dummy_0, __do_private_robust_list);
weak_alias(dummy_0, __do_orphaned_stdio_locks);
_Noreturn void pthread_exit(void *result)
_Noreturn void __pthread_exit(void *result)
{
pthread_t self = __pthread_self();
sigset_t set;
......@@ -139,7 +143,7 @@ static void init_file_lock(FILE *f)
void *__copy_tls(unsigned char *);
int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
{
int ret;
size_t size, guard;
......@@ -191,14 +195,14 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
if (!tsd) {
if (guard) {
map = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
if (map == MAP_FAILED) goto fail;
if (mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
munmap(map, size);
if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
__munmap(map, size);
goto fail;
}
} else {
map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
if (map == MAP_FAILED) goto fail;
}
tsd = map + size - __pthread_tsd_size;
......@@ -240,7 +244,7 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
if (ret < 0) {
a_dec(&libc.threads_minus_1);
if (map) munmap(map, size);
if (map) __munmap(map, size);
return EAGAIN;
}
......@@ -258,3 +262,6 @@ fail:
__release_ptc();
return EAGAIN;
}
weak_alias(__pthread_exit, pthread_exit);
weak_alias(__pthread_create, pthread_create);
#include "pthread_impl.h"
int pthread_detach(pthread_t t)
int __pthread_join(pthread_t, void **);
int __pthread_detach(pthread_t t)
{
/* Cannot detach a thread that's already exiting */
if (a_swap(t->exitlock, 1))
return pthread_join(t, 0);
return __pthread_join(t, 0);
t->detached = 2;
__unlock(t->exitlock);
return 0;
}
weak_alias(__pthread_detach, pthread_detach);
#include "pthread_impl.h"
void *pthread_getspecific(pthread_key_t k)
static void *__pthread_getspecific(pthread_key_t k)
{
struct pthread *self = __pthread_self();
return self->tsd[k];
}
weak_alias(__pthread_getspecific, pthread_getspecific);
#include "pthread_impl.h"
#include <sys/mman.h>
int __munmap(void *, size_t);
static void dummy(void *p)
{
}
int pthread_join(pthread_t t, void **res)
int __pthread_join(pthread_t t, void **res)
{
int tmp;
pthread_testcancel();
while ((tmp = t->tid)) __timedwait(&t->tid, tmp, 0, 0, dummy, 0, 0);
if (res) *res = t->result;
if (t->map_base) munmap(t->map_base, t->map_size);
if (t->map_base) __munmap(t->map_base, t->map_size);
return 0;
}
weak_alias(__pthread_join, pthread_join);
......@@ -9,7 +9,7 @@ static void nodtor(void *dummy)
{
}
int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
{
unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
unsigned j = i;
......@@ -31,7 +31,7 @@ int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
return EAGAIN;
}
int pthread_key_delete(pthread_key_t k)
int __pthread_key_delete(pthread_key_t k)
{
keys[k] = 0;
return 0;
......@@ -53,3 +53,6 @@ void __pthread_tsd_run_dtors()
}
}
}
weak_alias(__pthread_key_delete, pthread_key_delete);
weak_alias(__pthread_key_create, pthread_key_create);
#include "pthread_impl.h"
int pthread_mutex_lock(pthread_mutex_t *m)
int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict);
int __pthread_mutex_lock(pthread_mutex_t *m)
{
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
&& !a_cas(&m->_m_lock, 0, EBUSY))
return 0;
return pthread_mutex_timedlock(m, 0);
return __pthread_mutex_timedlock(m, 0);
}
weak_alias(__pthread_mutex_lock, pthread_mutex_lock);
#include "pthread_impl.h"
int pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
{
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
&& !a_cas(&m->_m_lock, 0, EBUSY))
......@@ -30,3 +30,5 @@ int pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *
}
return r;
}
weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
......@@ -50,9 +50,11 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m)
return 0;
}
int pthread_mutex_trylock(pthread_mutex_t *m)
int __pthread_mutex_trylock(pthread_mutex_t *m)
{
if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL)
return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
return __pthread_mutex_trylock_owner(m);
}
weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
......@@ -3,7 +3,7 @@
void __vm_lock_impl(int);
void __vm_unlock_impl(void);
int pthread_mutex_unlock(pthread_mutex_t *m)
int __pthread_mutex_unlock(pthread_mutex_t *m)
{
pthread_t self;
int waiters = m->_m_waiters;
......@@ -36,3 +36,5 @@ int pthread_mutex_unlock(pthread_mutex_t *m)
__wake(&m->_m_lock, 1, priv);
return 0;
}
weak_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
......@@ -6,7 +6,7 @@ static void undo(void *control)
__wake(control, 1, 1);
}
int pthread_once(pthread_once_t *control, void (*init)(void))
int __pthread_once(pthread_once_t *control, void (*init)(void))
{
static int waiters;
......@@ -34,3 +34,5 @@ int pthread_once(pthread_once_t *control, void (*init)(void))
return 0;
}
}
weak_alias(__pthread_once, pthread_once);
#include "pthread_impl.h"
int pthread_setcancelstate(int new, int *old)
int __pthread_setcancelstate(int new, int *old)
{
if (new > 1U) return EINVAL;
if (!libc.has_thread_pointer) return ENOSYS;
......@@ -9,3 +9,5 @@ int pthread_setcancelstate(int new, int *old)
self->canceldisable = new;
return 0;
}
weak_alias(__pthread_setcancelstate, pthread_setcancelstate);
......@@ -7,7 +7,9 @@ static void dummy()
weak_alias(dummy, __testcancel);
void pthread_testcancel()
void __pthread_testcancel()
{
__testcancel();
}
weak_alias(__pthread_testcancel, pthread_testcancel);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册