~ubuntu-branches/ubuntu/raring/python-scipy/raring-proposed

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/src/play/all/alarms.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * alarms.c -- $Id: alarms.c 685 2003-03-08 15:26:51Z travo $
 
3
 * alarm event functions, implemented using play interface
 
4
 *
 
5
 * Copyright (c) 1998.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#include "config.h"
 
9
#include "play.h"
 
10
#include "pstdlib.h"
 
11
 
 
12
typedef struct p_alarm p_alarm;
 
13
struct p_alarm {
 
14
  p_alarm *next;
 
15
  double time;
 
16
  void (*on_alarm)(void *c);
 
17
  void *context;
 
18
};
 
19
 
 
20
static p_alarm *alarm_next = 0;
 
21
static p_alarm *alarm_free = 0;
 
22
static double alarm_query(void);
 
23
static int idle_eligible = 1;
 
24
 
 
25
static int p_dflt_idle(void);
 
26
static int (*p_app_idle)(void)= &p_dflt_idle;
 
27
 
 
28
void
 
29
p_idler(int (*on_idle)(void))
 
30
{
 
31
  p_app_idle = on_idle;
 
32
}
 
33
 
 
34
static int
 
35
p_dflt_idle(void)
 
36
{
 
37
  return 0;
 
38
}
 
39
 
 
40
void
 
41
p_on_idle(int reset)
 
42
{
 
43
  if (!reset) {
 
44
    if (alarm_next && !alarm_query()) {
 
45
      /* alarm has rung - unlink it and call its on_alarm */
 
46
      p_alarm *next = alarm_next;
 
47
      alarm_next = next->next;
 
48
      next->next = alarm_free;
 
49
      alarm_free = next;
 
50
      next->on_alarm(next->context);
 
51
      idle_eligible = 1;
 
52
    } else {
 
53
      idle_eligible = p_app_idle();
 
54
    }
 
55
  } else {
 
56
    idle_eligible = 1;
 
57
  }
 
58
}
 
59
 
 
60
double
 
61
p_timeout(void)
 
62
{
 
63
  int eligible = idle_eligible;
 
64
  idle_eligible = 1;
 
65
  return eligible? 0.0 : (alarm_next? alarm_query() : -1.0);
 
66
}
 
67
 
 
68
void
 
69
p_set_alarm(double secs, void (*on_alarm)(void *c), void *context)
 
70
{
 
71
  p_alarm *me;
 
72
  p_alarm *next = alarm_next;
 
73
  p_alarm **prev = &alarm_next;
 
74
  double time;
 
75
  if (!alarm_free) {
 
76
    int n = 8;
 
77
    alarm_free = p_malloc(sizeof(p_alarm)*n);
 
78
    alarm_free[--n].next = 0;
 
79
    while (n--) alarm_free[n].next = &alarm_free[n+1];
 
80
  }
 
81
  me = alarm_free;
 
82
  me->time = time = p_wall_secs() + secs;
 
83
  me->on_alarm = on_alarm;
 
84
  me->context = context;
 
85
  /* insert me into alarm_next list, kept in order of time */
 
86
  while (next && next->time<=time) {
 
87
    prev = &next->next;
 
88
    next = next->next;
 
89
  }
 
90
  alarm_free = alarm_free->next;
 
91
  me->next = next;
 
92
  *prev = me;
 
93
}
 
94
 
 
95
void
 
96
p_clr_alarm(void (*on_alarm)(void *c), void *context)
 
97
{
 
98
  p_alarm *next, **prev = &alarm_next;
 
99
  for (next=alarm_next ; next ; next=*prev) {
 
100
    if ((!on_alarm || on_alarm==next->on_alarm) &&
 
101
        (!context || context==next->context)) {
 
102
      *prev = next->next;
 
103
      next->next = alarm_free;
 
104
      alarm_free = next;
 
105
    } else {
 
106
      prev = &next->next;
 
107
    }
 
108
  }
 
109
}
 
110
 
 
111
static double
 
112
alarm_query(void)
 
113
{
 
114
  if (alarm_next->time != -1.e35) {
 
115
    double time = p_wall_secs();
 
116
    p_alarm *next = alarm_next;
 
117
    /* if no alarms need to ring yet, return earliest */
 
118
    if (next->time > time)
 
119
      return next->time - time;
 
120
    do {
 
121
      next->time = -1.e35;   /* mark all alarms that need to ring */
 
122
      next = next->next;
 
123
    } while (next && next->time<=time);
 
124
  }
 
125
  return 0.0;
 
126
}