Mercurial > emacs
annotate src/atimer.c @ 37268:0f5de719ab07
(align): If the argument SIZE would overflow
__malloc_ptrdiff_t, fail right away.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Mon, 09 Apr 2001 10:52:43 +0000 |
parents | 2722b6b5400d |
children | 7ca787d18982 |
rev | line source |
---|---|
27433 | 1 /* Asynchronous timers. |
2 Copyright (C) 2000 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 #include <config.h> | |
22 #include <lisp.h> | |
23 #include <signal.h> | |
24 #include <syssignal.h> | |
25 #include <systime.h> | |
26 #include <blockinput.h> | |
27 #include <atimer.h> | |
28 #include <stdio.h> | |
29 | |
30 #ifdef HAVE_UNISTD_H | |
31 #include <unistd.h> | |
32 #endif | |
33 | |
34 #ifdef HAVE_SYS_TIME_H | |
35 #include <sys/time.h> | |
36 #endif | |
37 | |
38 /* The ubiquitous min/max macros. */ | |
39 | |
40 #define max(X, Y) ((X) > (Y) ? (X) : (Y)) | |
41 #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
42 | |
43 /* Free-list of atimer structures. */ | |
44 | |
45 static struct atimer *free_atimers; | |
46 | |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
47 /* List of currently not running timers due to a call to |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
48 lock_atimer. */ |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
49 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
50 static struct atimer *stopped_atimers; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
51 |
27433 | 52 /* List of active atimers, sorted by expiration time. The timer that |
53 will become ripe next is always at the front of this list. */ | |
54 | |
55 static struct atimer *atimers; | |
56 | |
57 /* Non-zero means alarm_signal_handler has found ripe timers but | |
58 interrupt_input_blocked was non-zero. In this case, timer | |
59 functions are not called until the next UNBLOCK_INPUT because timer | |
60 functions are expected to call X, and X cannot be assumed to be | |
61 reentrant. */ | |
62 | |
63 int pending_atimers; | |
64 | |
65 /* Block/unblock SIGALRM.. */ | |
66 | |
67 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM)) | |
68 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM)) | |
69 | |
70 /* Function prototypes. */ | |
71 | |
72 static void set_alarm P_ ((void)); | |
73 static void schedule_atimer P_ ((struct atimer *)); | |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
74 static struct atimer *append_atimer_lists P_ ((struct atimer *, |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
75 struct atimer *)); |
29672
2722b6b5400d
(alarm_signal_handler): Add forward declaration.
Gerd Moellmann <gerd@gnu.org>
parents:
28119
diff
changeset
|
76 SIGTYPE alarm_signal_handler (); |
27433 | 77 |
78 | |
79 /* Start a new atimer of type TYPE. TIME specifies when the timer is | |
80 ripe. FN is the function to call when the timer fires. | |
81 CLIENT_DATA is stored in the client_data member of the atimer | |
82 structure returned and so made available to FN when it is called. | |
83 | |
84 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the | |
85 timer fires. | |
86 | |
87 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the | |
88 future. | |
89 | |
90 In both cases, the timer is automatically freed after it has fired. | |
91 | |
92 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us. | |
93 | |
94 Value is a pointer to the atimer started. It can be used in calls | |
95 to cancel_atimer; don't free it yourself. */ | |
96 | |
97 struct atimer * | |
98 start_atimer (type, time, fn, client_data) | |
99 enum atimer_type type; | |
100 EMACS_TIME time; | |
101 atimer_callback fn; | |
102 void *client_data; | |
103 { | |
104 struct atimer *t; | |
105 | |
106 /* Round TIME up to the next full second if we don't have | |
107 itimers. */ | |
108 #ifndef HAVE_SETITIMER | |
109 if (EMACS_USECS (time) != 0) | |
110 { | |
27452
7580a16f676c
(start_atimer) [!HAVE_SETITIMER]: Use EMACS_SET_SECS
Eli Zaretskii <eliz@gnu.org>
parents:
27433
diff
changeset
|
111 EMACS_SET_USECS (time, 0); |
7580a16f676c
(start_atimer) [!HAVE_SETITIMER]: Use EMACS_SET_SECS
Eli Zaretskii <eliz@gnu.org>
parents:
27433
diff
changeset
|
112 EMACS_SET_SECS (time, EMACS_SECS (time) + 1); |
27433 | 113 } |
114 #endif /* not HAVE_SETITIMER */ | |
115 | |
116 /* Get an atimer structure from the free-list, or allocate | |
117 a new one. */ | |
118 if (free_atimers) | |
119 { | |
120 t = free_atimers; | |
121 free_atimers = t->next; | |
122 } | |
123 else | |
124 t = (struct atimer *) xmalloc (sizeof *t); | |
125 | |
126 /* Fill the atimer structure. */ | |
127 bzero (t, sizeof *t); | |
128 t->type = type; | |
129 t->fn = fn; | |
130 t->client_data = client_data; | |
131 | |
132 BLOCK_ATIMERS; | |
133 | |
134 /* Compute the timer's expiration time. */ | |
135 switch (type) | |
136 { | |
137 case ATIMER_ABSOLUTE: | |
138 t->expiration = time; | |
139 break; | |
140 | |
141 case ATIMER_RELATIVE: | |
142 EMACS_GET_TIME (t->expiration); | |
143 EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
144 break; | |
145 | |
146 case ATIMER_CONTINUOUS: | |
147 EMACS_GET_TIME (t->expiration); | |
148 EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
149 t->interval = time; | |
150 break; | |
151 } | |
152 | |
153 /* Insert the timer in the list of active atimers. */ | |
154 schedule_atimer (t); | |
155 UNBLOCK_ATIMERS; | |
156 | |
157 /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | |
158 set_alarm (); | |
159 | |
160 return t; | |
161 } | |
162 | |
163 | |
164 /* Cancel and free atimer TIMER. */ | |
165 | |
166 void | |
167 cancel_atimer (timer) | |
168 struct atimer *timer; | |
169 { | |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
170 int i; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
171 |
27433 | 172 BLOCK_ATIMERS; |
173 | |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
174 for (i = 0; i < 2; ++i) |
27903
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
175 { |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
176 struct atimer *t, *prev; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
177 struct atimer **list = i ? &stopped_atimers : &atimers; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
178 |
27903
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
179 /* See if TIMER is active or stopped. */ |
28119
98625ad8a015
(cancel_atimer): Break out of the loop as soon as timer
Gerd Moellmann <gerd@gnu.org>
parents:
27913
diff
changeset
|
180 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next) |
27903
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
181 ; |
27433 | 182 |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
183 /* If it is, take it off the its list, and put in on the |
27903
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
184 free-list. We don't bother to arrange for setting a |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
185 different alarm time, since a too early one doesn't hurt. */ |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
186 if (t) |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
187 { |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
188 if (prev) |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
189 prev->next = t->next; |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
190 else |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
191 *list = t->next; |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
192 |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
193 t->next = free_atimers; |
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
194 free_atimers = t; |
28119
98625ad8a015
(cancel_atimer): Break out of the loop as soon as timer
Gerd Moellmann <gerd@gnu.org>
parents:
27913
diff
changeset
|
195 break; |
27903
cc3d4c12e03b
(cancel_atimer): Handle canceling an atimer when
Gerd Moellmann <gerd@gnu.org>
parents:
27734
diff
changeset
|
196 } |
27433 | 197 } |
198 | |
199 UNBLOCK_ATIMERS; | |
200 } | |
201 | |
202 | |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
203 /* Append two lists of atimers LIST1 and LIST2 and return the |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
204 result list. */ |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
205 |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
206 static struct atimer * |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
207 append_atimer_lists (list1, list2) |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
208 struct atimer *list1, *list2; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
209 { |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
210 if (list1 == NULL) |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
211 return list2; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
212 else if (list2 == NULL) |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
213 return list1; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
214 else |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
215 { |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
216 struct atimer *p; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
217 |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
218 for (p = list1; p->next; p = p->next) |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
219 ; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
220 p->next = list2; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
221 return list1; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
222 } |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
223 } |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
224 |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
225 |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
226 /* Stop all timers except timer T. T null means stop all timers. */ |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
227 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
228 void |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
229 stop_other_atimers (t) |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
230 struct atimer *t; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
231 { |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
232 BLOCK_ATIMERS; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
233 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
234 if (t) |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
235 { |
27734
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
236 struct atimer *p, *prev; |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
237 |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
238 /* See if T is active. */ |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
239 for (p = atimers, prev = 0; p && p != t; p = p->next) |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
240 ; |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
241 |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
242 if (p == t) |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
243 { |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
244 if (prev) |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
245 prev->next = t->next; |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
246 else |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
247 atimers = t->next; |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
248 t->next = NULL; |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
249 } |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
250 else |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
251 /* T is not active. Let's handle this like T == 0. */ |
5c49b0be3b7b
(stop_other_atimers): Don't call cancel_atimer because
Gerd Moellmann <gerd@gnu.org>
parents:
27670
diff
changeset
|
252 t = NULL; |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
253 } |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
254 |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
255 stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
256 atimers = t; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
257 UNBLOCK_ATIMERS; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
258 } |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
259 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
260 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
261 /* Run all timers again, if some have been stopped with a call to |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
262 stop_other_atimers. */ |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
263 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
264 void |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
265 run_all_atimers () |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
266 { |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
267 if (stopped_atimers) |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
268 { |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
269 struct atimer *t = atimers; |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
270 struct atimer *next; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
271 |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
272 BLOCK_ATIMERS; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
273 atimers = stopped_atimers; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
274 stopped_atimers = NULL; |
27913
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
275 |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
276 while (t) |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
277 { |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
278 next = t->next; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
279 schedule_atimer (t); |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
280 t = next; |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
281 } |
81d5641c8b04
(start_atimer): Don't abort when timers are stopped.
Gerd Moellmann <gerd@gnu.org>
parents:
27903
diff
changeset
|
282 |
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
283 UNBLOCK_ATIMERS; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
284 } |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
285 } |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
286 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
287 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
288 /* A version of run_all_timers suitable for a record_unwind_protect. */ |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
289 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
290 Lisp_Object |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
291 unwind_stop_other_atimers (dummy) |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
292 Lisp_Object dummy; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
293 { |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
294 run_all_atimers (); |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
295 return Qnil; |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
296 } |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
297 |
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
298 |
27433 | 299 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
300 | |
301 static void | |
302 set_alarm () | |
303 { | |
304 #if defined (USG) && !defined (POSIX_SIGNALS) | |
305 /* USG systems forget handlers when they are used; | |
306 must reestablish each time. */ | |
307 signal (SIGALRM, alarm_signal_handler); | |
308 #endif /* USG */ | |
309 | |
310 if (atimers) | |
311 { | |
312 EMACS_TIME now, time; | |
313 #ifdef HAVE_SETITIMER | |
314 struct itimerval it; | |
315 #endif | |
316 | |
317 /* Determine s/us till the next timer is ripe. */ | |
318 EMACS_GET_TIME (now); | |
319 EMACS_SUB_TIME (time, atimers->expiration, now); | |
320 | |
321 #ifdef HAVE_SETITIMER | |
322 /* Don't set the interval to 0; this disables the timer. */ | |
323 if (EMACS_TIME_LE (atimers->expiration, now)) | |
324 { | |
325 EMACS_SET_SECS (time, 0); | |
326 EMACS_SET_USECS (time, 1000); | |
327 } | |
328 | |
329 bzero (&it, sizeof it); | |
330 it.it_value = time; | |
331 setitimer (ITIMER_REAL, &it, 0); | |
332 #else /* not HAVE_SETITIMER */ | |
333 alarm (max (EMACS_SECS (time), 1)); | |
334 #endif /* not HAVE_SETITIMER */ | |
335 } | |
336 } | |
337 | |
338 | |
339 /* Insert timer T into the list of active atimers `atimers', keeping | |
340 the list sorted by expiration time. T must not be in this list | |
341 already. */ | |
342 | |
343 static void | |
344 schedule_atimer (t) | |
345 struct atimer *t; | |
346 { | |
347 struct atimer *a = atimers, *prev = NULL; | |
348 | |
349 /* Look for the first atimer that is ripe after T. */ | |
350 while (a && EMACS_TIME_GT (t->expiration, a->expiration)) | |
351 prev = a, a = a->next; | |
352 | |
353 /* Insert T in front of the atimer found, if any. */ | |
354 if (prev) | |
355 prev->next = t; | |
356 else | |
357 atimers = t; | |
358 | |
359 t->next = a; | |
360 } | |
361 | |
362 | |
363 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. | |
364 SIGALRM. */ | |
365 | |
366 SIGTYPE | |
367 alarm_signal_handler (signo) | |
368 int signo; | |
369 { | |
370 EMACS_TIME now; | |
371 | |
372 EMACS_GET_TIME (now); | |
373 pending_atimers = 0; | |
374 | |
375 while (atimers | |
376 && (pending_atimers = interrupt_input_blocked) == 0 | |
377 && EMACS_TIME_LE (atimers->expiration, now)) | |
378 { | |
379 struct atimer *t; | |
380 | |
381 t = atimers; | |
382 atimers = atimers->next; | |
383 t->fn (t); | |
384 | |
385 if (t->type == ATIMER_CONTINUOUS) | |
386 { | |
387 EMACS_ADD_TIME (t->expiration, now, t->interval); | |
388 schedule_atimer (t); | |
389 } | |
390 else | |
391 { | |
392 t->next = free_atimers; | |
393 free_atimers = t; | |
394 } | |
395 | |
396 EMACS_GET_TIME (now); | |
397 } | |
398 | |
399 #if defined (USG) && !defined (POSIX_SIGNALS) | |
400 /* USG systems forget handlers when they are used; | |
401 must reestablish each time. */ | |
402 signal (SIGALRM, alarm_signal_handler); | |
403 #endif /* USG */ | |
404 | |
405 set_alarm (); | |
406 } | |
407 | |
408 | |
409 /* Call alarm_signal_handler for pending timers. */ | |
410 | |
411 void | |
412 do_pending_atimers () | |
413 { | |
414 if (pending_atimers) | |
415 { | |
416 BLOCK_ATIMERS; | |
417 alarm_signal_handler (SIGALRM); | |
418 UNBLOCK_ATIMERS; | |
419 } | |
420 } | |
421 | |
422 | |
423 /* Turn alarms on/off. This seems to be temporarily necessary on | |
424 some systems like HPUX (see process.c). */ | |
425 | |
426 void | |
427 turn_on_atimers (on) | |
428 int on; | |
429 { | |
430 if (on) | |
431 { | |
432 signal (SIGALRM, alarm_signal_handler); | |
433 set_alarm (); | |
434 } | |
435 else | |
436 alarm (0); | |
437 } | |
438 | |
439 | |
440 void | |
441 init_atimer () | |
442 { | |
443 free_atimers = atimers = NULL; | |
444 pending_atimers = 0; | |
445 signal (SIGALRM, alarm_signal_handler); | |
446 } |