about summary refs log tree commit diff
path: root/src/stdio/__stdio_write.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-05-25 22:44:34 -0400
committerRich Felker <dalias@aerifal.cx>2012-05-25 22:44:34 -0400
commit3f25354e624361f40011b242c492c2118184cc44 (patch)
treefbfebdbc6dd45cd6f7fb4307ff553c6b41567647 /src/stdio/__stdio_write.c
parent8cce15af83c04d8965561439f0a80a451a9b1af0 (diff)
downloadmusl-3f25354e624361f40011b242c492c2118184cc44.tar.gz
musl-3f25354e624361f40011b242c492c2118184cc44.tar.xz
musl-3f25354e624361f40011b242c492c2118184cc44.zip
avoid using pthread cleanup push/pop in stdio when not needed
unfortunately in dynamic-linked programs, these macros cause
pthread_self to be initialized, which costs a couple syscalls, and
(much worse) would necessarily fail, crash, and burn on ancient (2.4
and earlier) kernels where setting up a thread pointer does not work.

i'd like to do this in a more generic way that avoids all use of
cleanup push/pop before pthread_self has been successfully called and
avoids ugly if/else constructs like the one in this commit, but for
now, this will suffice.
Diffstat (limited to 'src/stdio/__stdio_write.c')
-rw-r--r--src/stdio/__stdio_write.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
index da45673f..cef7bbdc 100644
--- a/src/stdio/__stdio_write.c
+++ b/src/stdio/__stdio_write.c
@@ -18,9 +18,13 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
 	int iovcnt = 2;
 	ssize_t cnt;
 	for (;;) {
-		pthread_cleanup_push(cleanup, f);
-		cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
-		pthread_cleanup_pop(0);
+		if (libc.main_thread) {
+			pthread_cleanup_push(cleanup, f);
+			cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
+			pthread_cleanup_pop(0);
+		} else {
+			cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
+		}
 		if (cnt == rem) {
 			f->wend = f->buf + f->buf_size;
 			f->wpos = f->wbase = f->buf;