(scm_random_solid_sphere_x): Use scm_c_generalized_vector_length
[bpt/guile.git] / libguile / async.c
index 48f54c5..35e8a5a 100644 (file)
@@ -1,46 +1,25 @@
-/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004 Free Software Foundation, Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
+ * 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.
  *
- * This program is distributed in the hope that it will be useful,
+ * 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 General Public License for more details.
+ * 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 General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
- *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
 
 
 \f
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include <signal.h>
 #include "libguile/_scm.h"
 #include <unistd.h>
 #endif
 
-/* This is not used for anything except checking that DEFER_INTS and
-   ALLOW_INTS are used properly.
- */
-int scm_ints_disabled = 1;
-
 \f
 /* {Asynchronous Events}
  *
@@ -167,21 +141,33 @@ SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
 void
 scm_async_click ()
 {
-  SCM asyncs;
+  /* Reset pending_asyncs even when asyncs are blocked and not really
+     executed.
+  */
 
+  scm_root->pending_asyncs = 0;
   if (scm_root->block_asyncs == 0)
     {
-      while (!SCM_NULLP(asyncs = scm_root->active_asyncs))
+      SCM asyncs;
+      while (!scm_is_null(asyncs = scm_root->active_asyncs))
        {
          scm_root->active_asyncs = SCM_EOL;
          do
            {
-             SCM c = SCM_CDR (asyncs);
-             SCM_SETCDR (asyncs, SCM_BOOL_F);
              scm_call_0 (SCM_CAR (asyncs));
-             asyncs = c;
+             asyncs = SCM_CDR (asyncs);
+           }
+         while (!scm_is_null(asyncs));
+       }
+      for (asyncs = scm_root->signal_asyncs; !scm_is_null(asyncs);
+          asyncs = SCM_CDR (asyncs))
+       {
+         if (scm_is_true (SCM_CAR (asyncs)))
+           {
+             SCM proc = SCM_CAR (asyncs);
+             SCM_SETCAR (asyncs, SCM_BOOL_F);
+             scm_call_0 (proc);
            }
-         while (!SCM_NULLP(asyncs));
        }
     }
 }
@@ -206,24 +192,22 @@ SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
 void
 scm_i_queue_async_cell (SCM c, scm_root_state *root)
 {
-  if (SCM_CDR (c) == SCM_BOOL_F)
+  SCM p = root->active_asyncs;
+  SCM_SETCDR (c, SCM_EOL);
+  if (p == SCM_EOL)
+    root->active_asyncs = c;
+  else
     {
-      SCM p = root->active_asyncs;
-      SCM_SETCDR (c, SCM_EOL);
-      if (p == SCM_EOL)
-       root->active_asyncs = c;
-      else
+      SCM pp;
+      while ((pp = SCM_CDR(p)) != SCM_EOL)
        {
-         SCM pp;
-         while ((pp = SCM_CDR(p)) != SCM_EOL)
-           {
-             if (SCM_CAR (p) == SCM_CAR (c))
-               return;
-             p = pp;
-           }
-         SCM_SETCDR (p, c);
+         if (SCM_CAR (p) == SCM_CAR (c))
+           return;
+         p = pp;
        }
+      SCM_SETCDR (p, c);
     }
+  root->pending_asyncs = 1;
 }
 
 SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
@@ -238,17 +222,16 @@ SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
            "signal handlers.")
 #define FUNC_NAME s_scm_system_async_mark_for_thread
 {
-#ifdef USE_THREADS
   if (SCM_UNBNDP (thread))
     thread = scm_current_thread ();
   else
-    SCM_VALIDATE_THREAD (2, thread);
-      
+    {
+      SCM_VALIDATE_THREAD (2, thread);
+      if (scm_c_thread_exited_p (thread))
+       SCM_MISC_ERROR ("thread has already exited", SCM_EOL);
+    }
   scm_i_queue_async_cell (scm_cons (proc, SCM_BOOL_F),
                          scm_i_thread_root (thread));
-#else
-  scm_i_queue_async_cell (scm_cons (proc, SCM_BOOL_F), scm_root);
-#endif
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -292,6 +275,7 @@ SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
   if (scm_root->block_asyncs == 0)
     SCM_MISC_ERROR ("signals already unmasked", SCM_EOL);
   scm_root->block_asyncs = 0;
+  scm_async_click ();
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -324,6 +308,8 @@ static void
 decrease_block (void *unused)
 {
   scm_root->block_asyncs--;
+  if (scm_root->block_asyncs == 0)
+    scm_async_click ();
 }
 
 SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
@@ -336,17 +322,17 @@ SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
   return scm_internal_dynamic_wind (increase_block,
                                    (scm_t_inner) scm_call_0,
                                    decrease_block,
-                                   proc, NULL);
+                                   (void *)proc, NULL);
 }
 #undef FUNC_NAME
 
 void *
 scm_c_call_with_blocked_asyncs (void *(*proc) (void *data), void *data)
 {
-  return scm_internal_dynamic_wind (increase_block,
-                                   (scm_t_inner) proc,
-                                   decrease_block,
-                                   data, NULL);
+  return (void *)scm_internal_dynamic_wind (increase_block,
+                                           (scm_t_inner) proc,
+                                           decrease_block,
+                                           data, NULL);
 }
 
 
@@ -362,7 +348,7 @@ SCM_DEFINE (scm_call_with_unblocked_asyncs, "call-with-unblocked-asyncs", 1, 0,
   return scm_internal_dynamic_wind (decrease_block,
                                    (scm_t_inner) scm_call_0,
                                    increase_block,
-                                   proc, NULL);
+                                   (void *)proc, NULL);
 }
 #undef FUNC_NAME
 
@@ -372,12 +358,30 @@ scm_c_call_with_unblocked_asyncs (void *(*proc) (void *data), void *data)
   if (scm_root->block_asyncs == 0)
     scm_misc_error ("scm_c_call_with_unblocked_asyncs",
                    "asyncs already unblocked", SCM_EOL);
-  return scm_internal_dynamic_wind (decrease_block,
-                                    (scm_t_inner) proc,
-                                   increase_block,
-                                   data, NULL);
+  return (void *)scm_internal_dynamic_wind (decrease_block,
+                                           (scm_t_inner) proc,
+                                           increase_block,
+                                           data, NULL);
 }
 
+void
+scm_frame_block_asyncs ()
+{
+  scm_frame_rewind_handler (increase_block, NULL, SCM_F_WIND_EXPLICITLY);
+  scm_frame_unwind_handler (decrease_block, NULL, SCM_F_WIND_EXPLICITLY);
+}
+
+void
+scm_frame_unblock_asyncs ()
+{
+  if (scm_root->block_asyncs == 0)
+    scm_misc_error ("scm_with_unblocked_asyncs", 
+                   "asyncs already unblocked", SCM_EOL);
+  scm_frame_rewind_handler (decrease_block, NULL, SCM_F_WIND_EXPLICITLY);
+  scm_frame_unwind_handler (increase_block, NULL, SCM_F_WIND_EXPLICITLY);
+}
+
+
 \f
 
 void