From 2d13cf7415844534da544a70672018be002264e7 Mon Sep 17 00:00:00 2001 From: astrangeguy Date: Wed, 3 Jun 2020 18:33:16 +0200 Subject: [PATCH] make reserve_tls_slots() more lenient (fixes #331) --- lisp-kernel/windows-calls.c | 41 +++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lisp-kernel/windows-calls.c b/lisp-kernel/windows-calls.c index b2a10d554..ee73bef72 100644 --- a/lisp-kernel/windows-calls.c +++ b/lisp-kernel/windows-calls.c @@ -1028,30 +1028,31 @@ init_winsock() void reserve_tls_slots() { - unsigned int first_available, n, i; + unsigned int last_slot, i, cnt_pad; + unsigned int pad_slots[30]; + + /* Since there is no way to reserve specific TLS indices, we reserve + * indices upto 30, hope to reserve 30 to 63 in one continuous block + * and then free those below 30. + */ + cnt_pad = 0; + do { + last_slot = TlsAlloc(); + pad_slots[cnt_pad++] = last_slot; + } while (last_slot < 30 && cnt_pad < 30); - first_available = TlsAlloc(); - if (first_available > 30) { - fprintf(dbgout, "Can't allocate required TLS indexes.\n"); - fprintf(dbgout, "First available index value was %u\n", first_available); + if (last_slot != 30) { + fprintf(dbgout, "could not reserve TLS slots from 30 to 63: Slot %u already reserved\n", 30); exit(1); } - TlsFree(first_available); - - for (i = first_available; i < 30; i++) { - n = TlsAlloc(); - if (n != i) { - fprintf(dbgout, "unexpected TLS index value: wanted %u, got %u\n", i, n); - exit(1); - } - } - for (i = 30; i < 64; i++) { - n = TlsAlloc(); - if (n != i) { - fprintf(dbgout, "unexpected TLS index value: wanted %u, got %u\n", i, n); + for (i = 31; i < 64; i++) { + last_slot = TlsAlloc(); + if (last_slot != i) { + fprintf(dbgout, "could not reserve TLS slots from 30 to 63: Slot %u already reserved\n", i); exit(1); } } - for (i = first_available; i < 30; i++) - TlsFree(i); + for (i = 0; i < cnt_pad; i++) + if (pad_slots[i] < 30) + TlsFree(pad_slots[i]); }