aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/legacy/erl_timeout.c
blob: 146a106e7c79825155a4b1f7d759b4e59d86a06c (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1997-2011. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
/* timeout.c 
 * 
 * todo: use posix timers (timer_create etc) instead of setitimer.
 * 
 */
#if !defined(__WIN32__) && !defined(VXWORKS)

/* FIXME: well, at least I can compile now... */

#include "eidef.h"

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <signal.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include "erl_interface.h"
#include "erl_timeout.h"

typedef struct jmp_s {
  jmp_buf jmpbuf;
  struct itimerval timerinfo;
  void *siginfo;
  struct jmp_s *next;
} *jmp_t;

static jmp_t push(jmp_t j);
static jmp_t pop(void);
static void timeout_handler(int dummy);

jmp_buf *timeout_setup(int ms)
{
  struct itimerval t;
  jmp_t j;
  void *s;

#ifdef DEBUG
  fprintf(stderr,"timeout setup\n");
#endif
  s=signal(SIGALRM,timeout_handler);

  /* set the timer */
  t.it_interval.tv_sec = 0;
  t.it_interval.tv_usec = 0;
  t.it_value.tv_sec = ms / 1000;
  t.it_value.tv_usec = (ms % 1000) * 1000;

  /* get a jump buffer and save it */
  j = erl_malloc(sizeof(*j));
  j->siginfo = s;
  push(j);

  setitimer(ITIMER_REAL,&t,&(j->timerinfo));

  return &(j->jmpbuf);
}


int timeout_cancel(void)
{
  jmp_t j;

#ifdef DEBUG
  fprintf(stderr,"timeout cancel\n");
#endif
  /* retrieve the jump buffer */
  j=pop();
  /* restore the timer and signal disposition */
  setitimer(ITIMER_REAL,&(j->timerinfo),NULL);
  signal(SIGALRM,j->siginfo);

  free(j);

  return 0;
}

void timeout_handler(int dummy)
{
  jmp_t j;

#ifdef DEBUG
  fprintf(stderr,"timeout handler\n");
#endif

  /* retrieve the jump buffer */
  j=pop();

  /* restore the timer and signal disposition */
  setitimer(ITIMER_REAL,&(j->timerinfo),NULL);
  signal(SIGALRM,j->siginfo);

  free(j);
  longjmp(j->jmpbuf,JMPVAL);
  return; /* not reached */
}


/* a simple stack for saving the jump buffer allows us to pass a
 * variable between functions that don't call each other, in a way
 * that will survive the longjmp(). 
 */

/* FIXME problem for threaded ? */
static jmp_t jmp_head=NULL;
#ifdef DEBUG
static int depth = 0;
static int maxdepth = 0;
#endif

static jmp_t push(jmp_t j)
{
  j->next = jmp_head;
  jmp_head = j;

#ifdef DEBUG
  depth++;
  if (depth > maxdepth) maxdepth = depth;
#endif

  return j;
}

static jmp_t pop(void)
{
  jmp_t j = jmp_head;
  if (j) jmp_head = j->next;
#ifdef DEBUG
  depth--;
#endif
  return j;
}

#endif /* platform */