unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Chong Yidong <cyd@stupidchicken.com>
Cc: emacs-devel@gnu.org
Subject: Re: Nested sit-for's
Date: Wed, 16 Aug 2006 15:08:36 -0400	[thread overview]
Message-ID: <87wt988pmz.fsf@stupidchicken.com> (raw)
In-Reply-To: <m3y7tp85c8.fsf_-_@kfs-l.imdomain.dk> (Kim F. Storm's message of "Wed, 16 Aug 2006 10:14:47 +0200")

storm@cua.dk (Kim F. Storm) writes:

> (defun st1 ()
>   (with-current-buffer (get-buffer-create "*st1*")
>     (goto-char (point-max))
>     (insert "<")
>     (sit-for 30)
>     (insert ">")))
>
> (run-with-timer 1 2 'st1)
>
> (progn
>   (message "sit-for...")
>   (sit-for 5)
>   (message "sit-for...done"))
>
> Now, the sit-for...done message is shown after 30-35 seconds,
> not after 5 seconds...
>
> The call to sit-for in the timer is probably "bad practice", but it
> could just as well have happened in a process filter or some other
> async handler.

How about the following patch?  The preemption code takes place
read_filtered_event, so it affects nested `sit-for's while leaving
unchanged other uses of wait_reading_process_output, such as the
sit_for() function called in a handful of places in the C code.  I am
unsure whether it is important/correct/desirable to preempt those
cases.  Opinions welcome.

There's one part of the patch that I think may be incorrect.  It saves
the seconds and microseconds components of the end time (originally an
EMACS_TIME) in two Lisp integers, which is probably not big enough.  I
think the solution is to introduce a few utility functions to convert
between EMACS_TIMEs and the Lisp-level (HIGH LOW USECS) time
representation.  (This may let us reimplement `timer-relative-time'
more straightforwardly too).

*** emacs/src/lread.c.~1.362.~	2006-07-26 13:45:54.000000000 -0400
--- emacs/src/lread.c	2006-08-16 14:46:50.000000000 -0400
***************
*** 439,444 ****
--- 439,457 ----
  
  extern Lisp_Object read_char ();
  
+ static EMACS_TIME saved_end_time;
+ 
+ static Lisp_Object
+ read_filtered_event_unwind (data)
+      Lisp_Object data;
+ {
+   if (!NILP (data))
+     EMACS_SET_SECS_USECS (saved_end_time,
+ 			  XINT (XCAR (data)),
+ 			  XINT (XCDR (data)));
+   return Qnil;
+ }
+ 
  /* Read input events until we get one that's acceptable for our purposes.
  
     If NO_SWITCH_FRAME is non-zero, switch-frame events are stashed
***************
*** 468,473 ****
--- 481,487 ----
  {
    Lisp_Object val, delayed_switch_frame;
    EMACS_TIME end_time;
+   int count = SPECPDL_INDEX ();
  
  #ifdef HAVE_WINDOW_SYSTEM
    if (display_hourglass_p)
***************
*** 488,493 ****
--- 502,522 ----
        EMACS_GET_TIME (end_time);
        EMACS_SET_SECS_USECS (wait_time, sec, usec);
        EMACS_ADD_TIME (end_time, end_time, wait_time);
+ 
+       if (!EMACS_TIME_NEG_P (saved_end_time)
+ 	  && EMACS_TIME_GE (end_time, saved_end_time))
+ 	{
+ 	  EMACS_SET_SECS  (end_time, EMACS_SECS  (saved_end_time));
+ 	  EMACS_SET_USECS (end_time, EMACS_USECS (saved_end_time));
+ 	}
+       else
+ 	{
+ 	  record_unwind_protect (read_filtered_event_unwind, 
+ 				 Fcons (make_number (EMACS_SECS (saved_end_time)),
+ 					make_number (EMACS_USECS (saved_end_time))));
+ 	  EMACS_SET_SECS  (saved_end_time, EMACS_SECS  (end_time));
+ 	  EMACS_SET_USECS (saved_end_time, EMACS_USECS (end_time));
+ 	}
      }
  
    /* Read until we get an acceptable event.  */
***************
*** 553,558 ****
--- 582,588 ----
  
  #endif
  
+   unbind_to (count, Qnil);
    return val;
  }
  
***************
*** 4230,4235 ****
--- 4260,4267 ----
  
    Vloads_in_progress = Qnil;
    staticpro (&Vloads_in_progress);
+ 
+   EMACS_SET_SECS_USECS (saved_end_time, -1, -1);
  }
  
  /* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d

  reply	other threads:[~2006-08-16 19:08 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <E1GD2sr-0005PH-UP@savannah.gnu.org>
     [not found] ` <m3bqqlka7e.fsf@kfs-l.imdomain.dk>
     [not found]   ` <87y7tp90i1.fsf@stupidchicken.com>
2006-08-16  8:14     ` Nested sit-for's Kim F. Storm
2006-08-16 19:08       ` Chong Yidong [this message]
2006-08-17  6:02       ` Richard Stallman
2006-08-17 11:15         ` Kim F. Storm
2006-08-17 14:02           ` David Kastrup
2006-08-18 15:47             ` Richard Stallman
2006-08-17 14:14           ` Chong Yidong
2006-08-17 15:09             ` Kim F. Storm
2006-08-17 17:21               ` Chong Yidong
2006-08-17 21:28                 ` Kim F. Storm
2006-08-17 22:42                   ` Chong Yidong
2006-08-17 16:05             ` martin rudalics
2006-08-17 21:33               ` Kim F. Storm
2006-08-18  9:03                 ` martin rudalics
2006-08-18  9:26                   ` Kim F. Storm
2006-08-20 13:54                     ` Chong Yidong
2006-08-20 21:05                       ` Kim F. Storm
2006-08-20 21:52                         ` martin rudalics
2006-08-20 22:05                           ` Kim F. Storm
2006-08-21 11:13                         ` Richard Stallman
2006-08-21 11:45                           ` Kim F. Storm
2006-08-21 16:14                             ` Stefan Monnier
2006-08-21 17:18                               ` martin rudalics
2006-08-22  1:40                                 ` Stefan Monnier
2006-08-22  7:42                               ` Richard Stallman
2006-08-17 14:21         ` Chong Yidong
2006-08-18 15:47           ` Richard Stallman
2007-10-17 14:41             ` Juanma Barranquero
2007-10-18  5:02               ` Richard Stallman
2007-10-18  7:40                 ` Juanma Barranquero
2007-10-23  7:13                   ` Richard Stallman
2006-08-17 15:33         ` Drew Adams

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/emacs/

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

  git send-email \
    --in-reply-to=87wt988pmz.fsf@stupidchicken.com \
    --to=cyd@stupidchicken.com \
    --cc=emacs-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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).