]>
git.ipfire.org Git - thirdparty/bird.git/blob - nest/locks.c
4 * (c) 1999 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
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.
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.
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.
33 #include "nest/bird.h"
34 #include "lib/resource.h"
35 #include "nest/locks.h"
36 #include "nest/iface.h"
38 static list olock_list
;
39 static event
*olock_event
;
42 olock_same(struct object_lock
*x
, struct object_lock
*y
)
46 x
->iface
== y
->iface
&&
48 ipa_equal(x
->addr
, y
->addr
);
52 olock_free(resource
*r
)
54 struct object_lock
*q
, *l
= (struct object_lock
*) r
;
57 DBG("olock: Freeing %p\n", l
);
60 case OLOCK_STATE_FREE
:
62 case OLOCK_STATE_LOCKED
:
63 case OLOCK_STATE_EVENT
:
68 DBG("olock: -> %p becomes locked\n", n
);
69 q
= SKIP_BACK(struct object_lock
, n
, 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
);
77 case OLOCK_STATE_WAITING
:
86 olock_dump(resource
*r
)
88 struct object_lock
*l
= (struct object_lock
*) r
;
89 static char *olock_states
[] = { "free", "locked", "waiting", "event" };
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
))
96 static struct resclass olock_class
= {
98 sizeof(struct object_lock
),
105 * olock_new - create an object lock
106 * @p: resource pool to create the lock in.
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.
115 struct object_lock
*l
= ralloc(p
, &olock_class
);
117 l
->state
= OLOCK_STATE_FREE
;
118 init_list(&l
->waiters
);
123 * olock_acquire - acquire a lock
124 * @l: the lock to acquire
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
131 * When you want to release the resource, just rfree() the lock.
134 olock_acquire(struct object_lock
*l
)
137 struct object_lock
*q
;
139 WALK_LIST(n
, olock_list
)
141 q
= SKIP_BACK(struct object_lock
, n
, n
);
142 if (olock_same(q
, l
))
144 l
->state
= OLOCK_STATE_WAITING
;
145 add_tail(&q
->waiters
, &l
->n
);
146 DBG("olock: %p waits\n", l
);
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
);
157 olock_run_event(void *unused UNUSED
)
160 struct object_lock
*q
;
162 DBG("olock: Processing events\n");
165 n
= HEAD(olock_list
);
168 q
= SKIP_BACK(struct object_lock
, n
, n
);
169 if (q
->state
!= OLOCK_STATE_EVENT
)
171 DBG("olock: %p locked\n", q
);
172 q
->state
= OLOCK_STATE_LOCKED
;
174 add_tail(&olock_list
, &q
->n
);
180 * olock_init - initialize the object lock mechanism
182 * This function is called during BIRD startup. It initializes
183 * all the internal data structures of the lock module.
188 DBG("olock: init\n");
189 init_list(&olock_list
);
190 olock_event
= ev_new(&root_pool
);
191 olock_event
->hook
= olock_run_event
;