]>
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
26 * kind of 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, interface and optional
34 #include "nest/bird.h"
35 #include "lib/resource.h"
36 #include "nest/locks.h"
37 #include "nest/iface.h"
39 static list olock_list
;
40 static event
*olock_event
;
43 olock_same(struct object_lock
*x
, struct object_lock
*y
)
47 x
->iface
== y
->iface
&&
50 ipa_equal(x
->addr
, y
->addr
);
54 olock_free(resource
*r
)
56 struct object_lock
*q
, *l
= (struct object_lock
*) r
;
59 DBG("olock: Freeing %p\n", l
);
62 case OLOCK_STATE_FREE
:
64 case OLOCK_STATE_LOCKED
:
65 case OLOCK_STATE_EVENT
:
70 DBG("olock: -> %p becomes locked\n", n
);
71 q
= SKIP_BACK(struct object_lock
, n
, n
);
73 add_tail_list(&q
->waiters
, &l
->waiters
);
74 q
->state
= OLOCK_STATE_EVENT
;
75 add_head(&olock_list
, n
);
76 ev_schedule(olock_event
);
79 case OLOCK_STATE_WAITING
:
88 olock_dump(resource
*r
)
90 struct object_lock
*l
= (struct object_lock
*) r
;
91 static char *olock_states
[] = { "free", "locked", "waiting", "event" };
93 debug("(%d:%s:%I:%d:%d) [%s]\n", l
->type
, (l
->iface
? l
->iface
->name
: "?"), l
->addr
, l
->port
, l
->inst
, olock_states
[l
->state
]);
94 if (!EMPTY_LIST(l
->waiters
))
98 static struct resclass olock_class
= {
100 sizeof(struct object_lock
),
108 * olock_new - create an object lock
109 * @p: resource pool to create the lock in.
111 * The olock_new() function creates a new resource of type &object_lock
112 * and returns a pointer to it. After filling in the structure, the caller
113 * should call olock_acquire() to do the real locking.
118 struct object_lock
*l
= ralloc(p
, &olock_class
);
120 l
->state
= OLOCK_STATE_FREE
;
121 init_list(&l
->waiters
);
126 * olock_acquire - acquire a lock
127 * @l: the lock to acquire
129 * This function attempts to acquire exclusive access to the non-shareable
130 * resource described by the lock @l. It returns immediately, but as soon
131 * as the resource becomes available, it calls the hook() function set up
134 * When you want to release the resource, just rfree() the lock.
137 olock_acquire(struct object_lock
*l
)
140 struct object_lock
*q
;
142 WALK_LIST(n
, olock_list
)
144 q
= SKIP_BACK(struct object_lock
, n
, n
);
145 if (olock_same(q
, l
))
147 l
->state
= OLOCK_STATE_WAITING
;
148 add_tail(&q
->waiters
, &l
->n
);
149 DBG("olock: %p waits\n", l
);
153 DBG("olock: %p acquired immediately\n", l
);
154 l
->state
= OLOCK_STATE_EVENT
;
155 add_head(&olock_list
, &l
->n
);
156 ev_schedule(olock_event
);
160 olock_run_event(void *unused UNUSED
)
163 struct object_lock
*q
;
165 DBG("olock: Processing events\n");
168 n
= HEAD(olock_list
);
171 q
= SKIP_BACK(struct object_lock
, n
, n
);
172 if (q
->state
!= OLOCK_STATE_EVENT
)
174 DBG("olock: %p locked\n", q
);
175 q
->state
= OLOCK_STATE_LOCKED
;
177 add_tail(&olock_list
, &q
->n
);
183 * olock_init - initialize the object lock mechanism
185 * This function is called during BIRD startup. It initializes
186 * all the internal data structures of the lock module.
191 DBG("olock: init\n");
192 init_list(&olock_list
);
193 olock_event
= ev_new(&root_pool
);
194 olock_event
->hook
= olock_run_event
;