diff options
| author | nathanw <nathanw@NetBSD.org> | 2003-04-23 19:35:47 +0000 |
|---|---|---|
| committer | nathanw <nathanw@NetBSD.org> | 2003-04-23 19:35:47 +0000 |
| commit | df2772713ec09d50ccd8e888c49c3efd2e1ed132 (patch) | |
| tree | d7b7702c1832d66bf14a93f4ce2cd7b53c323125 /lib/libpthread/pthread.c | |
| parent | 275eeea6c928509284dd267fdcc9646d65a5352d (diff) | |
Introduce a pthread__error() macro, for detected application errors as
opposed to internal errors. The setting of the PTHREAD_ERRORMODE
environment variable determines the runtime behavior. Valid settings
are "ignore", "abort", and "print". The default is currently "abort".
Diffstat (limited to 'lib/libpthread/pthread.c')
| -rw-r--r-- | lib/libpthread/pthread.c | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/lib/libpthread/pthread.c b/lib/libpthread/pthread.c index da863a43564..482c6169e5d 100644 --- a/lib/libpthread/pthread.c +++ b/lib/libpthread/pthread.c @@ -1,4 +1,4 @@ -/* $NetBSD: pthread.c,v 1.16 2003/04/07 21:29:48 nathanw Exp $ */ +/* $NetBSD: pthread.c,v 1.17 2003/04/23 19:35:47 nathanw Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pthread.c,v 1.16 2003/04/07 21:29:48 nathanw Exp $"); +__RCSID("$NetBSD: pthread.c,v 1.17 2003/04/23 19:35:47 nathanw Exp $"); #include <err.h> #include <errno.h> @@ -75,6 +75,12 @@ static int nextthread; static pthread_spin_t nextthread_lock; static pthread_attr_t pthread_default_attr; +#define PTHREAD_ERRORMODE_ABORT 1 +#define PTHREAD_ERRORMODE_PRINT 2 +#define PTHREAD_ERRORMODE_IGNORE 3 + +static int pthread__errormode; + pthread_spin_t pthread__runqueue_lock; struct pthread_queue_t pthread__runqueue; struct pthread_queue_t pthread__idlequeue; @@ -119,6 +125,7 @@ void pthread_init(void) { pthread_t first; + char *mode; extern int __isthreaded; /* Initialize locks first; they're needed elsewhere. */ @@ -147,6 +154,16 @@ pthread_init(void) pthread__debug_init(); #endif + pthread__errormode = PTHREAD_ERRORMODE_ABORT; + if ((mode = getenv("PTHREAD_ERRORMODE")) != NULL) { + if (strcasecmp(mode, "ignore") == 0) + pthread__errormode = PTHREAD_ERRORMODE_IGNORE; + else if (strcasecmp(mode, "print") == 0) + pthread__errormode = PTHREAD_ERRORMODE_PRINT; + else if (strcasecmp(mode, "abort") == 0) + pthread__errormode = PTHREAD_ERRORMODE_ABORT; + } + /* Tell libc that we're here and it should role-play accordingly. */ __isthreaded = 1; } @@ -1051,3 +1068,33 @@ pthread__assertfunc(char *file, int line, char *function, char *expr) _exit(1); } + + +void +pthread__errorfunc(char *file, int line, char *function, char *msg) +{ + char buf[1024]; + int len; + + if (pthread__errormode == PTHREAD_ERRORMODE_IGNORE) + return; + + /* + * snprintf should not acquire any locks, or we could + * end up deadlocked if the assert caller held locks. + */ + len = snprintf(buf, 1024, + "Error detected, file \"%s\", line %d%s%s%s: %s.\n", + file, line, + function ? ", function \"" : "", + function ? function : "", + function ? "\"" : "", + msg); + + write(STDERR_FILENO, buf, len); + if (pthread__errormode == PTHREAD_ERRORMODE_ABORT) { + (void)kill(getpid(), SIGABRT); + + _exit(1); + } +} |
