Fix intmap bug for maps with only one element
[bpt/guile.git] / libguile / threads.c
index 99582cc..3dc0f40 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004,
- *   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
- *   Free Software Foundation, Inc.
+ *   2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
+ *   2014 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 #endif
 
 #include "libguile/bdw-gc.h"
+#include <gc/gc_mark.h>
 #include "libguile/_scm.h"
 
 #include <stdlib.h>
-#if HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #include <stdio.h>
 
 #ifdef HAVE_STRING_H
 #include "libguile/fluids.h"
 #include "libguile/continuations.h"
 #include "libguile/gc.h"
+#include "libguile/gc-inline.h"
 #include "libguile/init.h"
 #include "libguile/scmsigs.h"
 #include "libguile/strings.h"
+#include "libguile/vm.h"
 
 #include <full-read.h>
 
 
 \f
 
-/* First some libgc shims. */
-
-/* Make sure GC_fn_type is defined; it is missing from the public
-   headers of GC 7.1 and earlier.  */
-#ifndef HAVE_GC_FN_TYPE
-typedef void * (* GC_fn_type) (void *);
-#endif
-
-
-#ifndef GC_SUCCESS
-#define GC_SUCCESS 0
-#endif
-
-#ifndef GC_UNIMPLEMENTED
-#define GC_UNIMPLEMENTED 3
-#endif
-
-/* Likewise struct GC_stack_base is missing before 7.1.  */
-#ifndef HAVE_GC_STACK_BASE
-struct GC_stack_base {
-  void * mem_base; /* Base of memory stack. */
-#ifdef __ia64__
-  void * reg_base; /* Base of separate register stack. */
-#endif
-};
-
-static int
-GC_register_my_thread (struct GC_stack_base *stack_base)
-{
-  return GC_UNIMPLEMENTED;
-}
-
-static void
-GC_unregister_my_thread ()
-{
-}
-
-#if !SCM_USE_PTHREAD_THREADS
-/* No threads; we can just use GC_stackbottom.  */
-static void *
-get_thread_stack_base ()
-{
-  return GC_stackbottom;
-}
-
-#elif defined HAVE_PTHREAD_ATTR_GETSTACK && defined HAVE_PTHREAD_GETATTR_NP \
-  && defined PTHREAD_ATTR_GETSTACK_WORKS
-/* This method for GNU/Linux and perhaps some other systems.
-   It's not for MacOS X or Solaris 10, since pthread_getattr_np is not
-   available on them.  */
-static void *
-get_thread_stack_base ()
-{
-  pthread_attr_t attr;
-  void *start, *end;
-  size_t size;
-
-  pthread_getattr_np (pthread_self (), &attr);
-  pthread_attr_getstack (&attr, &start, &size);
-  end = (char *)start + size;
-
-#if SCM_STACK_GROWS_UP
-  return start;
-#else
-  return end;
-#endif
-}
-
-#elif defined HAVE_PTHREAD_GET_STACKADDR_NP
-/* This method for MacOS X.
-   It'd be nice if there was some documentation on pthread_get_stackaddr_np,
-   but as of 2006 there's nothing obvious at apple.com.  */
-static void *
-get_thread_stack_base ()
-{
-  return pthread_get_stackaddr_np (pthread_self ());
-}
-
-#elif HAVE_PTHREAD_ATTR_GET_NP
-/* This one is for FreeBSD 9.  */
-static void *
-get_thread_stack_base ()
-{
-  pthread_attr_t attr;
-  void *start, *end;
-  size_t size;
-
-  pthread_attr_init (&attr);
-  pthread_attr_get_np (pthread_self (), &attr);
-  pthread_attr_getstack (&attr, &start, &size);
-  pthread_attr_destroy (&attr);
-
-  end = (char *)start + size;
-
-#if SCM_STACK_GROWS_UP
-  return start;
-#else
-  return end;
-#endif
-}
-
-#else 
-#error Threads enabled with old BDW-GC, but missing get_thread_stack_base impl.  Please upgrade to libgc >= 7.1.
-#endif
-
-static int
-GC_get_stack_base (struct GC_stack_base *stack_base)
-{
-  stack_base->mem_base = get_thread_stack_base ();
-#ifdef __ia64__
-  /* Calculate and store off the base of this thread's register
-     backing store (RBS).  Unfortunately our implementation(s) of
-     scm_ia64_register_backing_store_base are only reliable for the
-     main thread.  For other threads, therefore, find out the current
-     top of the RBS, and use that as a maximum. */
-  stack_base->reg_base = scm_ia64_register_backing_store_base ();
-  {
-    ucontext_t ctx;
-    void *bsp;
-    getcontext (&ctx);
-    bsp = scm_ia64_ar_bsp (&ctx);
-    if (stack_base->reg_base > bsp)
-      stack_base->reg_base = bsp;
-  }
-#endif
-  return GC_SUCCESS;
-}
-
-static void *
-GC_call_with_stack_base(void * (*fn) (struct GC_stack_base*, void*), void *arg)
-{
-  struct GC_stack_base stack_base;
-
-  stack_base.mem_base = (void*)&stack_base;
-#ifdef __ia64__
-  /* FIXME: Untested.  */
-  {
-    ucontext_t ctx;
-    getcontext (&ctx);
-    stack_base.reg_base = scm_ia64_ar_bsp (&ctx);
-  }
-#endif
-
-  return fn (&stack_base, arg);
-}
-#endif /* HAVE_GC_STACK_BASE */
-
-
-/* Now define with_gc_active and with_gc_inactive.  */
-
-#if (defined(HAVE_GC_DO_BLOCKING) && defined (HAVE_DECL_GC_DO_BLOCKING) && defined (HAVE_GC_CALL_WITH_GC_ACTIVE))
-
-/* We have a sufficiently new libgc (7.2 or newer).  */
+/* The GC "kind" for threads that allow them to mark their VM
+   stacks.  */
+static int thread_gc_kind;
+
+static struct GC_ms_entry *
+thread_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr,
+             struct GC_ms_entry *mark_stack_limit, GC_word env)
+{
+  int word;
+  const struct scm_i_thread *t = (struct scm_i_thread *) addr;
+
+  if (SCM_UNPACK (t->handle) == 0)
+    /* T must be on the free-list; ignore.  (See warning in
+       gc_mark.h.)  */
+    return mark_stack_ptr;
+
+  /* Mark T.  We could be more precise, but it doesn't matter.  */
+  for (word = 0; word * sizeof (*addr) < sizeof (*t); word++)
+    mark_stack_ptr = GC_MARK_AND_PUSH ((void *) addr[word],
+                                      mark_stack_ptr, mark_stack_limit,
+                                      NULL);
+
+  /* The pointerless freelists are threaded through their first word,
+     but GC doesn't know to trace them (as they are pointerless), so we
+     need to do that here.  See the comments at the top of libgc's
+     gc_inline.h.  */
+  if (t->pointerless_freelists)
+    {
+      size_t n;
+      for (n = 0; n < SCM_INLINE_GC_FREELIST_COUNT; n++)
+        {
+          void *chain = t->pointerless_freelists[n];
+          if (chain)
+            {
+              /* The first link is already marked by the freelist vector,
+                 so we just have to mark the tail.  */
+              while ((chain = *(void **)chain))
+                mark_stack_ptr = GC_mark_and_push (chain, mark_stack_ptr,
+                                                   mark_stack_limit, NULL);
+            }
+        }
+    }
 
-static void*
-with_gc_inactive (GC_fn_type func, void *data)
-{
-  return GC_do_blocking (func, data);
-}
+  if (t->vp)
+    mark_stack_ptr = scm_i_vm_mark_stack (t->vp, mark_stack_ptr,
+                                          mark_stack_limit);
 
-static void*
-with_gc_active (GC_fn_type func, void *data)
-{
-  return GC_call_with_gc_active (func, data);
+  return mark_stack_ptr;
 }
 
-#else
-
-/* libgc not new enough, so never actually deactivate GC.
-
-   Note that though GC 7.1 does have a GC_do_blocking, it doesn't have
-   GC_call_with_gc_active.  */
-
-static void*
-with_gc_inactive (GC_fn_type func, void *data)
-{
-  return func (data);
-}
-
-static void*
-with_gc_active (GC_fn_type func, void *data)
-{
-  return func (data);
-}
-
-#endif /* HAVE_GC_DO_BLOCKING */
-
 
 \f
 static void
@@ -278,6 +142,7 @@ to_timespec (SCM t, scm_t_timespec *waittime)
     }
 }
 
+
 \f
 /*** Queues */
 
@@ -544,6 +409,8 @@ guilify_self_1 (struct GC_stack_base *base)
   t.mutexes = SCM_EOL;
   t.held_mutex = NULL;
   t.join_queue = SCM_EOL;
+  t.freelists = NULL;
+  t.pointerless_freelists = NULL;
   t.dynamic_state = SCM_BOOL_F;
   t.dynstack.base = NULL;
   t.dynstack.top = NULL;
@@ -562,6 +429,7 @@ guilify_self_1 (struct GC_stack_base *base)
   t.sleep_mutex = NULL;
   t.sleep_object = SCM_BOOL_F;
   t.sleep_fd = -1;
+  t.vp = NULL;
 
   if (pipe2 (t.sleep_pipe, O_CLOEXEC) != 0)
     /* FIXME: Error conditions during the initialization phase are handled
@@ -570,8 +438,6 @@ guilify_self_1 (struct GC_stack_base *base)
     abort ();
 
   scm_i_pthread_mutex_init (&t.admin_mutex, NULL);
-  t.current_mark_stack_ptr = NULL;
-  t.current_mark_stack_limit = NULL;
   t.canceled = 0;
   t.exited = 0;
   t.guile_mode = 0;
@@ -581,7 +447,7 @@ guilify_self_1 (struct GC_stack_base *base)
     scm_i_thread *t_ptr = &t;
     
     GC_disable ();
-    t_ptr = GC_malloc (sizeof (scm_i_thread));
+    t_ptr = GC_generic_malloc (sizeof (*t_ptr), thread_gc_kind);
     memcpy (t_ptr, &t, sizeof t);
 
     scm_i_pthread_setspecific (scm_i_thread_key, t_ptr);
@@ -614,7 +480,12 @@ guilify_self_2 (SCM parent)
 
   t->continuation_root = scm_cons (t->handle, SCM_EOL);
   t->continuation_base = t->base;
-  t->vm = SCM_BOOL_F;
+
+  {
+    size_t size = SCM_INLINE_GC_FREELIST_COUNT * sizeof (void *);
+    t->freelists = scm_gc_malloc (size, "freelists");
+    t->pointerless_freelists = scm_gc_malloc (size, "atomic freelists");
+  }
 
   if (scm_is_true (parent))
     t->dynamic_state = scm_make_dynamic_state (parent);
@@ -781,6 +652,12 @@ on_thread_exit (void *v)
 
   scm_i_pthread_setspecific (scm_i_thread_key, NULL);
 
+  if (t->vp)
+    {
+      scm_i_vm_free_stack (t->vp);
+      t->vp = NULL;
+    }
+
 #if SCM_USE_PTHREAD_THREADS
   GC_unregister_my_thread ();
 #endif
@@ -835,7 +712,7 @@ scm_i_init_thread_for_guile (struct GC_stack_base *base, SCM parent)
          */
          scm_i_init_guile (base);
 
-#if defined (HAVE_GC_ALLOW_REGISTER_THREADS) && SCM_USE_PTHREAD_THREADS
+#if SCM_USE_PTHREAD_THREADS
           /* Allow other threads to come in later.  */
           GC_allow_register_threads ();
 #endif
@@ -934,7 +811,7 @@ with_guile_and_parent (struct GC_stack_base *base, void *data)
 #endif
 
       t->guile_mode = 1;
-      res = with_gc_active (with_guile_trampoline, args);
+      res = GC_call_with_gc_active (with_guile_trampoline, args);
       t->guile_mode = 0;
     }
   return res;
@@ -968,7 +845,7 @@ scm_without_guile (void *(*func)(void *), void *data)
   if (t->guile_mode)
     {
       SCM_I_CURRENT_THREAD->guile_mode = 0;
-      result = with_gc_inactive (func, data);
+      result = GC_do_blocking (func, data);
       SCM_I_CURRENT_THREAD->guile_mode = 1;
     }
   else
@@ -1159,6 +1036,11 @@ SCM_DEFINE (scm_yield, "yield", 0, 0, 0,
 }
 #undef FUNC_NAME
 
+/* Some systems, notably Android, lack 'pthread_cancel'.  Don't provide
+   'cancel-thread' on these systems.  */
+
+#if !SCM_USE_PTHREAD_THREADS || defined HAVE_PTHREAD_CANCEL
+
 SCM_DEFINE (scm_cancel_thread, "cancel-thread", 1, 0, 0,
            (SCM thread),
 "Asynchronously force the target @var{thread} to terminate. @var{thread} "
@@ -1184,6 +1066,8 @@ SCM_DEFINE (scm_cancel_thread, "cancel-thread", 1, 0, 0,
 }
 #undef FUNC_NAME
 
+#endif
+
 SCM_DEFINE (scm_set_thread_cleanup_x, "set-thread-cleanup!", 2, 0, 0,
            (SCM thread, SCM proc),
 "Set the thunk @var{proc} as the cleanup handler for the thread @var{thread}. "
@@ -1900,14 +1784,6 @@ do_std_select (void *args)
   return NULL;
 }
 
-#if !SCM_HAVE_SYS_SELECT_H
-static int scm_std_select (int nfds,
-                           fd_set *readfds,
-                           fd_set *writefds,
-                           fd_set *exceptfds,
-                           struct timeval *timeout);
-#endif
-
 int
 scm_std_select (int nfds,
                fd_set *readfds,
@@ -2026,6 +1902,22 @@ scm_pthread_cond_timedwait (scm_i_pthread_cond_t *cond,
 
 #endif
 
+static void
+do_unlock_with_asyncs (void *data)
+{
+  scm_i_pthread_mutex_unlock ((scm_i_pthread_mutex_t *)data);
+  SCM_I_CURRENT_THREAD->block_asyncs--;
+}
+
+void
+scm_i_dynwind_pthread_mutex_lock_block_asyncs (scm_i_pthread_mutex_t *mutex)
+{
+  SCM_I_CURRENT_THREAD->block_asyncs++;
+  scm_i_scm_pthread_mutex_lock (mutex);
+  scm_dynwind_unwind_handler (do_unlock_with_asyncs, mutex,
+                              SCM_F_WIND_EXPLICITLY);
+}
+
 unsigned long
 scm_std_usleep (unsigned long usecs)
 {
@@ -2189,6 +2081,11 @@ scm_threads_prehistory (void *base)
   scm_i_pthread_mutex_init (&scm_i_misc_mutex, NULL);
   scm_i_pthread_cond_init (&wake_up_cond, NULL);
 
+  thread_gc_kind =
+    GC_new_kind (GC_new_free_list (),
+                GC_MAKE_PROC (GC_new_proc (thread_mark), 0),
+                0, 1);
+
   guilify_self_1 ((struct GC_stack_base *) base);
 }