degenerate let forms
[bpt/guile.git] / libguile / extensions.c
index e92560f..1c3d28f 100644 (file)
@@ -1,23 +1,24 @@
 /* extensions.c - registering and loading extensions.
  *
- * Copyright (C) 2001 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2006, 2009, 2010, 2011 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 as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -40,7 +41,8 @@ typedef struct extension_t
   void *data;
 } extension_t;
 
-static extension_t *registered_extensions;
+static extension_t *registered_extensions = NULL;
+static scm_i_pthread_mutex_t ext_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
 
 /* Register a LIB/INIT pair for use by `scm_load_extension'.  LIB is
    allowed to be NULL and then only INIT is used to identify the
@@ -64,39 +66,58 @@ scm_c_register_extension (const char *lib, const char *init,
   ext->func = func;
   ext->data = data;
 
+  scm_i_pthread_mutex_lock (&ext_lock);
   ext->next = registered_extensions;
   registered_extensions = ext;
+  scm_i_pthread_mutex_unlock (&ext_lock);
 }
 
 static void
 load_extension (SCM lib, SCM init)
 {
+  extension_t *head;
+
+  scm_i_pthread_mutex_lock (&ext_lock);
+  head = registered_extensions;
+  scm_i_pthread_mutex_unlock (&ext_lock);
+
   /* Search the registry. */
-  if (registered_extensions != NULL)
+  if (head != NULL)
     {
       extension_t *ext;
       char *clib, *cinit;
+      int found = 0;
 
-      scm_frame_begin (0);
+      scm_dynwind_begin (0);
 
       clib = scm_to_locale_string (lib);
-      scm_frame_free (clib);
+      scm_dynwind_free (clib);
       cinit = scm_to_locale_string (init);
-      scm_frame_free (cinit);
+      scm_dynwind_free (cinit);
 
-      for (ext = registered_extensions; ext; ext = ext->next)
+      for (ext = head; ext; ext = ext->next)
        if ((ext->lib == NULL || !strcmp (ext->lib, clib))
            && !strcmp (ext->init, cinit))
          {
            ext->func (ext->data);
+            found = 1;
            break;
          }
 
-      scm_frame_end ();
+      scm_dynwind_end ();
+
+      if (found)
+        return;
     }
 
   /* Dynamically link the library. */
+#if HAVE_MODULES
   scm_dynamic_call (init, scm_dynamic_link (lib));
+#else
+  scm_misc_error ("load-extension",
+                  "extension ~S:~S not registered and dynamic-link disabled",
+                  scm_list_2 (init, lib));
+#endif
 }
 
 void
@@ -151,7 +172,6 @@ SCM_DEFINE (scm_load_extension, "load-extension", 2, 0, 0,
 void
 scm_init_extensions ()
 {
-  registered_extensions = NULL;
 #include "libguile/extensions.x"
 }