aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/legacy/erl_timeout.h
blob: 17d45915f538a57f5d4f4026315ebfba48bf81e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1997-2009. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */
#ifndef _ERL_TIMEOUT_H
#define _ERL_TIMEOUT_H

#if !defined (__WIN32__) && !defined (VXWORKS)

#include <setjmp.h>

/*
  use timeout like this (delay in ms):

  if (timeout(delay,fun(a,b,c))) {
      printf("timeout occurred\n");
  }
  else {
      ... 
  }

If the call to fun() has not returned before 'delay' ms, it will be
interrupted and and timeout() will return a non-zero value.

If fun() finishes before 'delay' ms, then timeout will return 0.

If you need the return value from fun then assign it like this:

  if (timeout(delay,(x = fun(...)))) {
  }

These functions work by setting and catching SIGALRM, and although it
saves and restores the signal handler, it may not work in situations
where you are already using SIGALRM (this includes calls to sleep(3)).

Note that although recursive calls to timeout will not fail, they may
not give the expected results. All invocations of timeout use the same
timer, which is set on entrance to timeout and restored on exit from
timeout. So although an inner call to timeout will restart the timer
for any pending outer call when it exits, any time that has already
elapsed against the outer timeout is forgotten. In addition, the alarm
signal will always go to the innermost (last called) timeout, which
may or may not be the intention in recursive cases. 

*/

#define JMPVAL 997 /* magic */

#define timeout(ms,funcall) \
      (setjmp(*timeout_setup(ms)) == JMPVAL ? -1: \
       ((void)(funcall), timeout_cancel()))


/* don't call any of these directly - use the macro! see above! */   
jmp_buf *timeout_setup(int ms);
int timeout_cancel(void);

#endif /* WIN32 && VXWORKS */

#endif  /* _ERL_TIMEOUT_H */