Browse Source

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.
Jens Gustedt 10 years ago
parent
commit
df7d0dfb9c

+ 3 - 1
src/mman/mprotect.c

@@ -2,10 +2,12 @@
 #include "libc.h"
 #include "libc.h"
 #include "syscall.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;
 	size_t start, end;
 	start = (size_t)addr & -PAGE_SIZE;
 	start = (size_t)addr & -PAGE_SIZE;
 	end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
 	end = (size_t)((char *)addr + len + PAGE_SIZE-1) & -PAGE_SIZE;
 	return syscall(SYS_mprotect, start, end-start, prot);
 	return syscall(SYS_mprotect, start, end-start, prot);
 }
 }
+
+weak_alias(__mprotect, mprotect);

+ 6 - 3
src/thread/__timedwait.c

@@ -4,6 +4,9 @@
 #include "futex.h"
 #include "futex.h"
 #include "syscall.h"
 #include "syscall.h"
 
 
+int __pthread_setcancelstate(int, int *);
+int __clock_gettime(clockid_t, struct timespec *);
+
 int __timedwait(volatile int *addr, int val,
 int __timedwait(volatile int *addr, int val,
 	clockid_t clk, const struct timespec *at,
 	clockid_t clk, const struct timespec *at,
 	void (*cleanup)(void *), void *arg, int priv)
 	void (*cleanup)(void *), void *arg, int priv)
@@ -15,7 +18,7 @@ int __timedwait(volatile int *addr, int val,
 
 
 	if (at) {
 	if (at) {
 		if (at->tv_nsec >= 1000000000UL) return EINVAL;
 		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;
 		to.tv_sec = at->tv_sec - to.tv_sec;
 		if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
 		if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
 			to.tv_sec--;
 			to.tv_sec--;
@@ -25,7 +28,7 @@ int __timedwait(volatile int *addr, int val,
 		top = &to;
 		top = &to;
 	}
 	}
 
 
-	if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+	if (!cleanup) __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	pthread_cleanup_push(cleanup, arg);
 	pthread_cleanup_push(cleanup, arg);
 
 
 	r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
 	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;
 	if (r != EINTR && r != ETIMEDOUT) r = 0;
 
 
 	pthread_cleanup_pop(0);
 	pthread_cleanup_pop(0);
-	if (!cleanup) pthread_setcancelstate(cs, 0);
+	if (!cleanup) __pthread_setcancelstate(cs, 0);
 
 
 	return r;
 	return r;
 }
 }

+ 9 - 3
src/thread/pthread_cond_timedwait.c

@@ -1,5 +1,9 @@
 #include "pthread_impl.h"
 #include "pthread_impl.h"
 
 
+void __pthread_testcancel(void);
+int __pthread_mutex_lock(pthread_mutex_t *);
+int __pthread_mutex_unlock(pthread_mutex_t *);
+
 /*
 /*
  * struct waiter
  * 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 };
 	struct waiter node = { .cond = c, .mutex = m };
 	int e, seq, *fut, clock = c->_c_clock;
 	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)
 	if (ts && ts->tv_nsec >= 1000000000UL)
 		return EINVAL;
 		return EINVAL;
 
 
-	pthread_testcancel();
+	__pthread_testcancel();
 
 
 	if (c->_c_shared) {
 	if (c->_c_shared) {
 		node.shared = 1;
 		node.shared = 1;
@@ -151,7 +155,7 @@ int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict
 		unlock(&c->_c_lock);
 		unlock(&c->_c_lock);
 	}
 	}
 
 
-	pthread_mutex_unlock(m);
+	__pthread_mutex_unlock(m);
 
 
 	do e = __timedwait(fut, seq, clock, ts, unwait, &node, !node.shared);
 	do e = __timedwait(fut, seq, clock, ts, unwait, &node, !node.shared);
 	while (*fut==seq && (!e || e==EINTR));
 	while (*fut==seq && (!e || e==EINTR));
@@ -197,3 +201,5 @@ int __private_cond_signal(pthread_cond_t *c, int n)
 
 
 	return 0;
 	return 0;
 }
 }
+
+weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);

+ 14 - 7
src/thread/pthread_create.c

@@ -5,6 +5,10 @@
 #include <sys/mman.h>
 #include <sys/mman.h>
 #include <string.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()
 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_private_robust_list);
 weak_alias(dummy_0, __do_orphaned_stdio_locks);
 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();
 	pthread_t self = __pthread_self();
 	sigset_t set;
 	sigset_t set;
@@ -139,7 +143,7 @@ static void init_file_lock(FILE *f)
 
 
 void *__copy_tls(unsigned char *);
 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;
 	int ret;
 	size_t size, guard;
 	size_t size, guard;
@@ -191,14 +195,14 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
 
 
 	if (!tsd) {
 	if (!tsd) {
 		if (guard) {
 		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 (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;
 				goto fail;
 			}
 			}
 		} else {
 		} 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;
 			if (map == MAP_FAILED) goto fail;
 		}
 		}
 		tsd = map + size - __pthread_tsd_size;
 		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) {
 	if (ret < 0) {
 		a_dec(&libc.threads_minus_1);
 		a_dec(&libc.threads_minus_1);
-		if (map) munmap(map, size);
+		if (map) __munmap(map, size);
 		return EAGAIN;
 		return EAGAIN;
 	}
 	}
 
 
@@ -258,3 +262,6 @@ fail:
 	__release_ptc();
 	__release_ptc();
 	return EAGAIN;
 	return EAGAIN;
 }
 }
+
+weak_alias(__pthread_exit, pthread_exit);
+weak_alias(__pthread_create, pthread_create);

+ 6 - 2
src/thread/pthread_detach.c

@@ -1,11 +1,15 @@
 #include "pthread_impl.h"
 #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 */
 	/* Cannot detach a thread that's already exiting */
 	if (a_swap(t->exitlock, 1))
 	if (a_swap(t->exitlock, 1))
-		return pthread_join(t, 0);
+		return __pthread_join(t, 0);
 	t->detached = 2;
 	t->detached = 2;
 	__unlock(t->exitlock);
 	__unlock(t->exitlock);
 	return 0;
 	return 0;
 }
 }
+
+weak_alias(__pthread_detach, pthread_detach);

+ 3 - 1
src/thread/pthread_getspecific.c

@@ -1,7 +1,9 @@
 #include "pthread_impl.h"
 #include "pthread_impl.h"
 
 
-void *pthread_getspecific(pthread_key_t k)
+static void *__pthread_getspecific(pthread_key_t k)
 {
 {
 	struct pthread *self = __pthread_self();
 	struct pthread *self = __pthread_self();
 	return self->tsd[k];
 	return self->tsd[k];
 }
 }
+
+weak_alias(__pthread_getspecific, pthread_getspecific);

+ 6 - 2
src/thread/pthread_join.c

@@ -1,16 +1,20 @@
 #include "pthread_impl.h"
 #include "pthread_impl.h"
 #include <sys/mman.h>
 #include <sys/mman.h>
 
 
+int __munmap(void *, size_t);
+
 static void dummy(void *p)
 static void dummy(void *p)
 {
 {
 }
 }
 
 
-int pthread_join(pthread_t t, void **res)
+int __pthread_join(pthread_t t, void **res)
 {
 {
 	int tmp;
 	int tmp;
 	pthread_testcancel();
 	pthread_testcancel();
 	while ((tmp = t->tid)) __timedwait(&t->tid, tmp, 0, 0, dummy, 0, 0);
 	while ((tmp = t->tid)) __timedwait(&t->tid, tmp, 0, 0, dummy, 0, 0);
 	if (res) *res = t->result;
 	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;
 	return 0;
 }
 }
+
+weak_alias(__pthread_join, pthread_join);

+ 5 - 2
src/thread/pthread_key_create.c

@@ -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 i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
 	unsigned j = i;
 	unsigned j = i;
@@ -31,7 +31,7 @@ int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
 	return EAGAIN;
 	return EAGAIN;
 }
 }
 
 
-int pthread_key_delete(pthread_key_t k)
+int __pthread_key_delete(pthread_key_t k)
 {
 {
 	keys[k] = 0;
 	keys[k] = 0;
 	return 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);

+ 6 - 2
src/thread/pthread_mutex_lock.c

@@ -1,10 +1,14 @@
 #include "pthread_impl.h"
 #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
 	if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
 	    && !a_cas(&m->_m_lock, 0, EBUSY))
 	    && !a_cas(&m->_m_lock, 0, EBUSY))
 		return 0;
 		return 0;
 
 
-	return pthread_mutex_timedlock(m, 0);
+	return __pthread_mutex_timedlock(m, 0);
 }
 }
+
+weak_alias(__pthread_mutex_lock, pthread_mutex_lock);

+ 3 - 1
src/thread/pthread_mutex_timedlock.c

@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 #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
 	if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
 	    && !a_cas(&m->_m_lock, 0, EBUSY))
 	    && !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;
 	return r;
 }
 }
+
+weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);

+ 3 - 1
src/thread/pthread_mutex_trylock.c

@@ -50,9 +50,11 @@ int __pthread_mutex_trylock_owner(pthread_mutex_t *m)
 	return 0;
 	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)
 	if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL)
 		return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
 		return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
 	return __pthread_mutex_trylock_owner(m);
 	return __pthread_mutex_trylock_owner(m);
 }
 }
+
+weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);

+ 3 - 1
src/thread/pthread_mutex_unlock.c

@@ -3,7 +3,7 @@
 void __vm_lock_impl(int);
 void __vm_lock_impl(int);
 void __vm_unlock_impl(void);
 void __vm_unlock_impl(void);
 
 
-int pthread_mutex_unlock(pthread_mutex_t *m)
+int __pthread_mutex_unlock(pthread_mutex_t *m)
 {
 {
 	pthread_t self;
 	pthread_t self;
 	int waiters = m->_m_waiters;
 	int waiters = m->_m_waiters;
@@ -36,3 +36,5 @@ int pthread_mutex_unlock(pthread_mutex_t *m)
 		__wake(&m->_m_lock, 1, priv);
 		__wake(&m->_m_lock, 1, priv);
 	return 0;
 	return 0;
 }
 }
+
+weak_alias(__pthread_mutex_unlock, pthread_mutex_unlock);

+ 3 - 1
src/thread/pthread_once.c

@@ -6,7 +6,7 @@ static void undo(void *control)
 	__wake(control, 1, 1);
 	__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;
 	static int waiters;
 
 
@@ -34,3 +34,5 @@ int pthread_once(pthread_once_t *control, void (*init)(void))
 		return 0;
 		return 0;
 	}
 	}
 }
 }
+
+weak_alias(__pthread_once, pthread_once);

+ 3 - 1
src/thread/pthread_setcancelstate.c

@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 #include "pthread_impl.h"
 
 
-int pthread_setcancelstate(int new, int *old)
+int __pthread_setcancelstate(int new, int *old)
 {
 {
 	if (new > 1U) return EINVAL;
 	if (new > 1U) return EINVAL;
 	if (!libc.has_thread_pointer) return ENOSYS;
 	if (!libc.has_thread_pointer) return ENOSYS;
@@ -9,3 +9,5 @@ int pthread_setcancelstate(int new, int *old)
 	self->canceldisable = new;
 	self->canceldisable = new;
 	return 0;
 	return 0;
 }
 }
+
+weak_alias(__pthread_setcancelstate, pthread_setcancelstate);

+ 3 - 1
src/thread/pthread_testcancel.c

@@ -7,7 +7,9 @@ static void dummy()
 
 
 weak_alias(dummy, __testcancel);
 weak_alias(dummy, __testcancel);
 
 
-void pthread_testcancel()
+void __pthread_testcancel()
 {
 {
 	__testcancel();
 	__testcancel();
 }
 }
+
+weak_alias(__pthread_testcancel, pthread_testcancel);