clean up handling of thread/nothread mode, locking

This commit is contained in:
Rich Felker
2011-04-17 16:53:54 -04:00
parent eb0e8fa0b1
commit 9080cc153c
8 changed files with 16 additions and 27 deletions

View File

@ -7,8 +7,8 @@
struct __libc { struct __libc {
int *(*errno_location)(void); int *(*errno_location)(void);
void (*testcancel)(void); void (*testcancel)(void);
void (*lock)(volatile int *); int threaded;
void (*lockfile)(FILE *); int canceldisable;
void (*fork_handler)(int); void (*fork_handler)(int);
int (*atexit)(void (*)(void)); int (*atexit)(void (*)(void));
void (*fini)(void); void (*fini)(void);
@ -16,7 +16,6 @@ struct __libc {
volatile int threads_minus_1; volatile int threads_minus_1;
int ofl_lock; int ofl_lock;
FILE *ofl_head; FILE *ofl_head;
int canceldisable;
}; };
@ -40,7 +39,7 @@ extern struct __libc *__libc_loc(void) __attribute__((const));
void __lock(volatile int *); void __lock(volatile int *);
void __lockfile(FILE *); void __lockfile(FILE *);
#define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1)) #define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1))
#define UNLOCK(x) (*(x)=0) #define UNLOCK(x) (*(volatile int *)(x)=0)
int __rsyscall(int, long, long, long, long, long, long); int __rsyscall(int, long, long, long, long, long, long);

View File

@ -24,7 +24,7 @@
#define UNGET 8 #define UNGET 8
#define FLOCK(f) ((libc.lockfile && (f)->lock>=0) ? (libc.lockfile((f)),0) : 0) #define FLOCK(f) ((libc.threads_minus_1 && (f)->lock>=0) ? (__lockfile((f)),0) : 0)
#define FUNLOCK(f) ((f)->lockcount && (--(f)->lockcount || ((f)->lock=0))) #define FUNLOCK(f) ((f)->lockcount && (--(f)->lockcount || ((f)->lock=0)))
#define F_PERM 1 #define F_PERM 1

View File

@ -8,7 +8,7 @@ pid_t fork(void)
pid_t ret; pid_t ret;
if (libc.fork_handler) libc.fork_handler(-1); if (libc.fork_handler) libc.fork_handler(-1);
ret = syscall(SYS_fork); ret = syscall(SYS_fork);
if (libc.lock && !ret) { if (libc.threaded && !ret) {
pthread_t self = __pthread_self(); pthread_t self = __pthread_self();
self->tid = self->pid = syscall(SYS_getpid); self->tid = self->pid = syscall(SYS_getpid);
libc.threads_minus_1 = 0; libc.threads_minus_1 = 0;

View File

@ -3,9 +3,6 @@
void flockfile(FILE *f) void flockfile(FILE *f)
{ {
if (!libc.lockfile) { if (!libc.threaded) pthread_self();
pthread_self();
libc.lockfile = __lockfile;
}
__lockfile(f); __lockfile(f);
} }

View File

@ -4,7 +4,6 @@
int ftrylockfile(FILE *f) int ftrylockfile(FILE *f)
{ {
int tid = pthread_self()->tid; int tid = pthread_self()->tid;
if (!libc.lockfile) libc.lockfile = __lockfile;
if (f->lock == tid) { if (f->lock == tid) {
if (f->lockcount == INT_MAX) if (f->lockcount == INT_MAX)
return -1; return -1;

View File

@ -17,7 +17,7 @@ long (__syscall_cp)(long nr, long u, long v, long w, long x, long y, long z)
uintptr_t old_sp, old_ip; uintptr_t old_sp, old_ip;
long r; long r;
if (!libc.lock || (self = __pthread_self())->canceldisable) if (!libc.threaded || (self = __pthread_self())->canceldisable)
return __syscall(nr, u, v, w, x, y, z); return __syscall(nr, u, v, w, x, y, z);
old_sp = self->cp_sp; old_sp = self->cp_sp;

View File

@ -44,18 +44,6 @@ void __pthread_unwind_next(struct __ptcb *cb)
__syscall(SYS_exit, 0); __syscall(SYS_exit, 0);
} }
static void init_threads()
{
sigset_t set;
libc.lock = __lock;
libc.lockfile = __lockfile;
sigemptyset(&set);
sigaddset(&set, SIGSYSCALL);
sigaddset(&set, SIGCANCEL);
__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
}
static int start(void *p) static int start(void *p)
{ {
struct pthread *self = p; struct pthread *self = p;
@ -79,7 +67,6 @@ weak_alias(dummy, __pthread_tsd_size);
int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg) int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
{ {
static int init;
int ret; int ret;
size_t size, guard; size_t size, guard;
struct pthread *self = pthread_self(), *new; struct pthread *self = pthread_self(), *new;
@ -87,7 +74,14 @@ int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(vo
const pthread_attr_t default_attr = { 0 }; const pthread_attr_t default_attr = { 0 };
if (!self) return ENOSYS; if (!self) return ENOSYS;
if (!init && ++init) init_threads(); if (!libc.threaded) {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGSYSCALL);
sigaddset(&set, SIGCANCEL);
__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
libc.threaded = 1;
}
if (!attr) attr = &default_attr; if (!attr) attr = &default_attr;
guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE); guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);

View File

@ -3,7 +3,7 @@
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.lock) { if (libc.threaded) {
struct pthread *self = __pthread_self(); struct pthread *self = __pthread_self();
if (old) *old = self->canceldisable; if (old) *old = self->canceldisable;
self->canceldisable = new; self->canceldisable = new;