47298e9b4cd093b98428ec92679d685a1477c9b6
[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 (defalias
30   'run-at-time
31   (if (condition-case nil
32           (progn
33             (unless (or itimer-process itimer-timer)
34               (itimer-driver-start))
35             ;; Check whether there is a bug to which the difference of
36             ;; the present time and the time when the itimer driver was
37             ;; woken up is subtracted from the initial itimer value.
38             (let* ((inhibit-quit t)
39                    (ctime (current-time))
40                    (itimer-timer-last-wakeup
41                     (prog1
42                         ctime
43                       (setcar ctime (1- (car ctime)))))
44                    (itimer-list nil)
45                    (itimer (start-itimer "run-at-time" 'ignore 5)))
46               (sleep-for 0.1) ;; Accept the timeout interrupt.
47               (prog1
48                   (> (itimer-value itimer) 0)
49                 (delete-itimer itimer))))
50         (error nil))
51       (lambda (time repeat function &rest args)
52         "Function emulating the function of the same name of Emacs.
53 TIME should be nil meaning now, or a number of seconds from now.
54 Return an itimer object which can be used in either `delete-itimer'
55 or `cancel-timer'."
56         (apply #'start-itimer "run-at-time"
57                function (if time (max time 1e-9) 1e-9)
58                repeat nil t args))
59     (lambda (time repeat function &rest args)
60       "Function emulating the function of the same name of Emacs.
61 It works correctly for TIME even if there is a bug in the XEmacs core.
62 TIME should be nil meaning now, or a number of seconds from now.
63 Return an itimer object which can be used in either `delete-itimer'
64 or `cancel-timer'."
65       (let ((itimers (list nil)))
66         (setcar
67          itimers
68          (apply #'start-itimer "fixed-run-at-time"
69                 (lambda (itimers repeat function &rest args)
70                   (let ((itimer (car itimers)))
71                     (if repeat
72                         (progn
73                           (set-itimer-function
74                            itimer
75                            (lambda (itimer repeat function &rest args)
76                              (set-itimer-restart itimer repeat)
77                              (set-itimer-function itimer function)
78                              (set-itimer-function-arguments itimer args)
79                              (apply function args)))
80                           (set-itimer-function-arguments
81                            itimer
82                            (append (list itimer repeat function) args)))
83                       (set-itimer-function
84                        itimer
85                        (lambda (itimer function &rest args)
86                          (delete-itimer itimer)
87                          (apply function args)))
88                       (set-itimer-function-arguments
89                        itimer
90                        (append (list itimer function) args)))))
91                 1e-9 (if time (max time 1e-9) 1e-9)
92                 nil t itimers repeat function args))))))
93
94 (provide 'run-at-time)
95
96 ;;; run-at-time.el ends here