Index: /trunk/source/lisp-kernel/pmcl-kernel.c
===================================================================
--- /trunk/source/lisp-kernel/pmcl-kernel.c	(revision 14582)
+++ /trunk/source/lisp-kernel/pmcl-kernel.c	(revision 14583)
@@ -1710,4 +1710,5 @@
     extern void init_winsock(void);
     extern void init_windows_io(void);
+    extern void reserve_tls_slots(void);
 
     _fmode = O_BINARY;
@@ -1717,4 +1718,5 @@
     init_winsock();
     init_windows_io();
+    reserve_tls_slots();
     utf_16_argv = CommandLineToArgvW(GetCommandLineW(),&wide_argc);
   }
Index: /trunk/source/lisp-kernel/windows-calls.c
===================================================================
--- /trunk/source/lisp-kernel/windows-calls.c	(revision 14582)
+++ /trunk/source/lisp-kernel/windows-calls.c	(revision 14583)
@@ -1011,2 +1011,40 @@
 }
 
+/*
+ * Reserve TLS slots 32 through 63 in the TEB for (part of) the TCR.
+ *
+ * On Windows 7 x64, #_TlsAlloc returns 23 in a fresh lisp.  On
+ * Windows XP, it returns 11.  With any luck, this will leave enough
+ * wiggle room for the C runtime or whatever to use a few more TLS
+ * slots, and still leave 32 through 63 free for us.
+ */
+void
+reserve_tls_slots()
+{
+  unsigned int first_available, n, i;
+
+  first_available = TlsAlloc();
+  if (first_available > 32) {
+    fprintf(dbgout, "Can't allocate required TLS indexes.\n");
+    fprintf(dbgout, "First available index value was %u\n", first_available);
+    exit(1);
+  }
+  TlsFree(first_available);
+
+  for (i = first_available; i < 32; i++) {
+    n = TlsAlloc();
+    if (n != i) {
+      fprintf(dbgout, "unexpected TLS index value: wanted %u, got %u\n", i, n);
+      exit(1);
+    }
+  }
+  for (i = 32; i < 64; i++) {
+    n = TlsAlloc();
+    if (n != i) {
+      fprintf(dbgout, "unexpected TLS index value: wanted %u, got %u\n", i, n);
+      exit(1);
+    }
+  }
+  for (i = first_available; i < 32; i++)
+    TlsFree(i);
+}
