*** empty log message ***
[gnus] / lisp / run-at-time.el
1 ;;; run-at-time.el --- A non-buggy version of the run-at-time function
2
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Katsumi Yamaoka  <yamaoka@jpl.org>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; XEmacs has a buggy version of run-at-time.  This file defines a
27 ;; non-buggy version of the same.
28
29 (defvar run-at-time-saved (symbol-function 'run-at-time))
30
31 (require 'itimer)
32
33 (eval-and-compile
34   (when (featurep 'xemacs)
35     (defalias
36       'run-at-time
37       (if (condition-case nil
38               (progn
39                 (unless (or itimer-process itimer-timer)
40                   (itimer-driver-start))
41                 ;; Check whether there is a bug to which the difference of
42                 ;; the present time and the time when the itimer driver was
43                 ;; woken up is subtracted from the initial itimer value.
44                 (let* ((inhibit-quit t)
45                        (ctime (current-time))
46                        (itimer-timer-last-wakeup
47                         (prog1
48                             ctime
49                           (setcar ctime (1- (car ctime)))))
50                        (itimer-list nil)
51                        (itimer (start-itimer "fixed-run-at-time" 'ignore 5)))
52                   (sleep-for 0.1) ;; Accept the timeout interrupt.
53                   (prog1
54                       (> (itimer-value itimer) 0)
55                     (delete-itimer itimer))))
56             (error nil))
57           (lambda (time repeat function &rest args)
58             "Emulating function run as `run-at-time'.
59 TIME should be nil meaning now, or a number of seconds from now.
60 Return an itimer object which can be used in either `delete-itimer'
61 or `cancel-timer'."
62             (apply #'start-itimer "fixed-run-at-time"
63                    function (if time (max time 1e-9) 1e-9)
64                    repeat nil t args))
65         (lambda (time repeat function &rest args)
66           "Emulating function run as `run-at-time' in the right way.
67 TIME should be nil meaning now, or a number of seconds from now.
68 Return an itimer object which can be used in either `delete-itimer'
69 or `cancel-timer'."
70           (let ((itimers (list nil)))
71             (setcar
72              itimers
73              (apply #'start-itimer "fixed-run-at-time"
74                     (lambda (itimers repeat function &rest args)
75                       (let ((itimer (car itimers)))
76                         (if repeat
77                             (progn
78                               (set-itimer-function
79                                itimer
80                                (lambda (itimer repeat function &rest args)
81                                  (set-itimer-restart itimer repeat)
82                                  (set-itimer-function itimer function)
83                                  (set-itimer-function-arguments itimer args)
84                                  (apply function args)))
85                               (set-itimer-function-arguments
86                                itimer
87                                (append (list itimer repeat function) args)))
88                           (set-itimer-function
89                            itimer
90                            (lambda (itimer function &rest args)
91                              (delete-itimer itimer)
92                              (apply function args)))
93                           (set-itimer-function-arguments
94                            itimer
95                            (append (list itimer function) args)))))
96                     1e-9 (if time (max time 1e-9) 1e-9)
97                     nil t itimers repeat function args))))))))
98
99 (provide 'run-at-time)
100
101 ;;; run-at-time.el ends here