Statistics
| Branch: | Revision:

iof-bird-daemon / nest / locks.c @ 7e95c05d

History | View | Annotate | Download (4.82 KB)

1
/*
2
 *        BIRD Object Locks
3
 *
4
 *        (c) 1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Object locks
11
 *
12
 * The lock module provides a simple mechanism for avoiding conflicts between
13
 * various protocols which would like to use a single physical resource (for
14
 * example a network port). It would be easy to say that such collisions can
15
 * occur only when the user specifies an invalid configuration and therefore
16
 * he deserves to get what he has asked for, but unfortunately they can also
17
 * arise legitimately when the daemon is reconfigured and there exists (although
18
 * for a short time period only) an old protocol instance being shut down and a new one
19
 * willing to start up on the same interface.
20
 *
21
 * The solution is very simple: when any protocol wishes to use a network port
22
 * or some other non-shareable resource, it asks the core to lock it and it doesn't
23
 * use the resource until it's notified that it has acquired the lock.
24
 *
25
 * Object locks are represented by &object_lock structures which are in turn a kind of
26
 * resource. Lockable resources are uniquely determined by resource type
27
 * (%OBJLOCK_UDP for a UDP port etc.), IP address (usually a broadcast or
28
 * multicast address the port is bound to), port number and interface.
29
 */
30

    
31
#undef LOCAL_DEBUG
32

    
33
#include "nest/bird.h"
34
#include "lib/resource.h"
35
#include "nest/locks.h"
36
#include "nest/iface.h"
37

    
38
static list olock_list;
39
static event *olock_event;
40

    
41
static inline int
42
olock_same(struct object_lock *x, struct object_lock *y)
43
{
44
  return
45
    x->type == y->type &&
46
    x->iface == y->iface &&
47
    x->port == y->port &&
48
    ipa_equal(x->addr, y->addr);
49
}
50

    
51
static void
52
olock_free(resource *r)
53
{
54
  struct object_lock *q, *l = (struct object_lock *) r;
55
  node *n;
56

    
57
  DBG("olock: Freeing %p\n", l);
58
  switch (l->state)
59
    {
60
    case OLOCK_STATE_FREE:
61
      break;
62
    case OLOCK_STATE_LOCKED:
63
    case OLOCK_STATE_EVENT:
64
      rem_node(&l->n);
65
      n = HEAD(l->waiters);
66
      if (n->next)
67
        {
68
          DBG("olock: -> %p becomes locked\n", n);
69
          q = SKIP_BACK(struct object_lock, n, n);
70
          rem_node(n);
71
          add_tail_list(&l->waiters, &q->waiters);
72
          q->state = OLOCK_STATE_EVENT;
73
          add_head(&olock_list, n);
74
          ev_schedule(olock_event);
75
        }
76
      break;
77
    case OLOCK_STATE_WAITING:
78
      rem_node(&l->n);
79
      break;
80
    default:
81
      ASSERT(0);
82
    }
83
}
84

    
85
static void
86
olock_dump(resource *r)
87
{
88
  struct object_lock *l = (struct object_lock *) r;
89
  static char *olock_states[] = { "free", "locked", "waiting", "event" };
90

    
91
  debug("(%d:%s:%I:%d) [%s]\n", l->type, (l->iface ? l->iface->name : "?"), l->addr, l->port, olock_states[l->state]);
92
  if (!EMPTY_LIST(l->waiters))
93
    debug(" [wanted]\n");
94
}
95

    
96
static struct resclass olock_class = {
97
  "ObjLock",
98
  sizeof(struct object_lock),
99
  olock_free,
100
  olock_dump,
101
  NULL
102
};
103

    
104
/**
105
 * olock_new - create an object lock
106
 * @p: resource pool to create the lock in.
107
 *
108
 * The olock_new() function creates a new resource of type &object_lock
109
 * and returns a pointer to it. After filling in the structure, the caller
110
 * should call olock_acquire() to do the real locking.
111
 */
112
struct object_lock *
113
olock_new(pool *p)
114
{
115
  struct object_lock *l = ralloc(p, &olock_class);
116

    
117
  l->state = OLOCK_STATE_FREE;
118
  init_list(&l->waiters);
119
  return l;
120
}
121

    
122
/**
123
 * olock_acquire - acquire a lock
124
 * @l: the lock to acquire
125
 *
126
 * This function attempts to acquire exclusive access to the non-shareable
127
 * resource described by the lock @l. It returns immediately, but as soon
128
 * as the resource becomes available, it calls the hook() function set up
129
 * by the caller.
130
 *
131
 * When you want to release the resource, just rfree() the lock.
132
 */
133
void
134
olock_acquire(struct object_lock *l)
135
{
136
  node *n;
137
  struct object_lock *q;
138

    
139
  WALK_LIST(n, olock_list)
140
    {
141
      q = SKIP_BACK(struct object_lock, n, n);
142
      if (olock_same(q, l))
143
        {
144
          l->state = OLOCK_STATE_WAITING;
145
          add_tail(&q->waiters, &l->n);
146
          DBG("olock: %p waits\n", l);
147
          return;
148
        }
149
    }
150
  DBG("olock: %p acquired immediately\n", l);
151
  l->state = OLOCK_STATE_EVENT;
152
  add_head(&olock_list, &l->n);
153
  ev_schedule(olock_event);
154
}
155

    
156
static void
157
olock_run_event(void *unused UNUSED)
158
{
159
  node *n;
160
  struct object_lock *q;
161

    
162
  DBG("olock: Processing events\n");
163
  for(;;)
164
    {
165
      n = HEAD(olock_list);
166
      if (!n->next)
167
        break;
168
      q = SKIP_BACK(struct object_lock, n, n);
169
      if (q->state != OLOCK_STATE_EVENT)
170
        break;
171
      DBG("olock: %p locked\n", q);
172
      q->state = OLOCK_STATE_LOCKED;
173
      rem_node(&q->n);
174
      add_tail(&olock_list, &q->n);
175
      q->hook(q);
176
    }
177
}
178

    
179
/**
180
 * olock_init - initialize the object lock mechanism
181
 *
182
 * This function is called during BIRD startup. It initializes
183
 * all the internal data structures of the lock module.
184
 */
185
void
186
olock_init(void)
187
{
188
  DBG("olock: init\n");
189
  init_list(&olock_list);
190
  olock_event = ev_new(&root_pool);
191
  olock_event->hook = olock_run_event;
192
}