drop use of pthread_once for aio thread stack size init
authorRich Felker <dalias@aerifal.cx>
Tue, 8 Dec 2020 23:02:39 +0000 (18:02 -0500)
committerRich Felker <dalias@aerifal.cx>
Tue, 8 Dec 2020 23:02:39 +0000 (18:02 -0500)
pthread_once is not compatible with MT-fork constraints (commit
167390f05564e0a4d3fcb4329377fd7743267560) and is not needed here
anyway; we already have a lock suitable for initialization.

while changing this, fix a corner case where AT_MINSIGSTKSZ gives a
value that's more than MINSIGSTKSZ but by a margin of less than
2048, thereby causing the size to be reduced. it shouldn't matter but
the intent was to be the larger of a 2048-byte margin over the legacy
fixed minimum stack requirement or a 512-byte margin over the minimum
the kernel reports at runtime.

src/aio/aio.c

index e004f98..a1a3e79 100644 (file)
@@ -76,6 +76,10 @@ static struct aio_queue *****map;
 static volatile int aio_fd_cnt;
 volatile int __aio_fut;
 
+static size_t io_thread_stack_size;
+
+#define MAX(a,b) ((a)>(b) ? (a) : (b))
+
 static struct aio_queue *__aio_get_queue(int fd, int need)
 {
        if (fd < 0) {
@@ -90,6 +94,10 @@ static struct aio_queue *__aio_get_queue(int fd, int need)
                pthread_rwlock_unlock(&maplock);
                if (fcntl(fd, F_GETFD) < 0) return 0;
                pthread_rwlock_wrlock(&maplock);
+               if (!io_thread_stack_size) {
+                       unsigned long val = __getauxval(AT_MINSIGSTKSZ);
+                       io_thread_stack_size = MAX(MINSIGSTKSZ+2048, val+512);
+               }
                if (!map) map = calloc(sizeof *map, (-1U/2+1)>>24);
                if (!map) goto out;
                if (!map[a]) map[a] = calloc(sizeof **map, 256);
@@ -265,15 +273,6 @@ static void *io_thread_func(void *ctx)
        return 0;
 }
 
-static size_t io_thread_stack_size = MINSIGSTKSZ+2048;
-static pthread_once_t init_stack_size_once;
-
-static void init_stack_size()
-{
-       unsigned long val = __getauxval(AT_MINSIGSTKSZ);
-       if (val > MINSIGSTKSZ) io_thread_stack_size = val + 512;
-}
-
 static int submit(struct aiocb *cb, int op)
 {
        int ret = 0;
@@ -299,7 +298,6 @@ static int submit(struct aiocb *cb, int op)
                else
                        pthread_attr_init(&a);
        } else {
-               pthread_once(&init_stack_size_once, init_stack_size);
                pthread_attr_init(&a);
                pthread_attr_setstacksize(&a, io_thread_stack_size);
                pthread_attr_setguardsize(&a, 0);