unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
To: guile-devel@gnu.org
Subject: Re: port-for-each vs lazy sweep
Date: Thu, 23 Aug 2007 10:32:32 +1000	[thread overview]
Message-ID: <87ps1f850f.fsf@zip.com.au> (raw)
In-Reply-To: <87zm0lu3tr.fsf@zip.com.au> (Kevin Ryde's message of "Tue, 21 Aug 2007 10:30:24 +1000")

[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

Actually I see the flush func of a soft port is entirely unused, it's
never called by a force-output because nothing is ever put in the port
buffer as such.  The manual could be clearer about what it's supposed to
be for :-(.

At any rate, I put in the failing test below for port-for-each, and I
think flush-all could benefit from the rewrite below, just on general
principles.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ports.test.diff --]
[-- Type: text/x-diff, Size: 1397 bytes --]

--- ports.test	27 Jan 2007 11:06:20 +1100	1.33.2.5
+++ ports.test	22 Aug 2007 16:43:39 +1000	
@@ -550,6 +550,38 @@
       (eqv? n (port-line port)))))
 
 ;;;
+;;; port-for-each
+;;;
+
+(with-test-prefix "port-for-each"
+
+  ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
+  ;; its iterator func if a port was inaccessible in the last gc mark but
+  ;; the lazy sweeping has not yet reached it to remove it from the port
+  ;; table (scm_i_port_table).  Provoking those gc conditions is a little
+  ;; tricky, but the following code made it happen in 1.8.2.
+  (pass-if "passing freed cell"
+    (throw 'unresolved)
+    (let ((lst '()))
+      ;; clear out the heap
+      (gc) (gc) (gc)
+      ;; allocate cells so the opened ports aren't at the start of the heap
+      (make-list 1000)
+      (open-input-file "/dev/null")
+      (make-list 1000)
+      (open-input-file "/dev/null")
+      ;; this gc leaves the above ports unmarked, ie. inaccessible
+      (gc)
+      ;; but they're still in the port table, so this sees them
+      (port-for-each (lambda (port)
+		       (set! lst (cons port lst))))
+      ;; this forces completion of the sweeping
+      (gc) (gc) (gc)
+      ;; and (if the bug is present) the cells accumulated in LST are now
+      ;; freed cells, which give #f from `port?'
+      (not (memq #f (map port? lst))))))
+
+;;;
 ;;; seek
 ;;;
 

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: ports.c.flush-all.diff --]
[-- Type: text/x-diff, Size: 1040 bytes --]

--- ports.c	27 Jan 2007 10:51:48 +1100	1.204.2.14
+++ ports.c	21 Aug 2007 17:02:53 +1000	
@@ -929,25 +929,26 @@
 }
 #undef FUNC_NAME
 
+
+static void
+scm_flush_all_ports_one (void *dummy, SCM port)
+{
+  if (SCM_OPOUTPORTP (port))
+    scm_flush (port);
+}
+
 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
             (),
 	    "Equivalent to calling @code{force-output} on\n"
 	    "all open output ports.  The return value is unspecified.")
 #define FUNC_NAME s_scm_flush_all_ports
 {
-  size_t i;
-
-  scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
-  for (i = 0; i < scm_i_port_table_size; i++)
-    {
-      if (SCM_OPOUTPORTP (scm_i_port_table[i]->port))
-	scm_flush (scm_i_port_table[i]->port);
-    }
-  scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
+  scm_c_port_for_each (scm_flush_all_ports_one);
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
+
 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
            (SCM port),
 	    "Return the next character available from @var{port}, updating\n"

[-- Attachment #4: Type: text/plain, Size: 143 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel

  reply	other threads:[~2007-08-23  0:32 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-19  1:22 port-for-each vs lazy sweep Kevin Ryde
2007-08-20  8:42 ` Ludovic Courtès
2007-08-21  0:30   ` Kevin Ryde
2007-08-23  0:32     ` Kevin Ryde [this message]
2007-08-23  7:25       ` Ludovic Courtès
2007-08-25 18:57 ` Han-Wen Nienhuys
2007-08-25 22:20 ` Han-Wen Nienhuys
2007-08-26 17:04   ` Ludovic Courtès
2007-08-26 17:16     ` Han-Wen Nienhuys
2007-08-26 17:36       ` Ludovic Courtès
2007-08-26 18:06     ` Han-Wen Nienhuys
2007-09-03 17:20       ` Ludovic Courtès
2007-09-03 19:34         ` Han-Wen Nienhuys
2007-09-04  9:08         ` Ludovic Courtès
2007-10-01 20:23         ` Ludovic Courtès
2007-10-27 15:07           ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ps1f850f.fsf@zip.com.au \
    --to=user42@zip.com.au \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).