1999-12-10 02:54:20 +08:00
|
|
|
/*
|
|
|
|
* BIRD Object Locks
|
|
|
|
*
|
|
|
|
* (c) 1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-02 00:16:49 +08:00
|
|
|
/**
|
|
|
|
* DOC: Object locks
|
|
|
|
*
|
|
|
|
* The lock module provides a simple mechanism for avoiding conflicts between
|
|
|
|
* various protocols which would like to use a single physical resource (for
|
|
|
|
* example a network port). It would be easy to say that such collisions can
|
|
|
|
* occur only when the user specifies an invalid configuration and therefore
|
|
|
|
* he deserves to get what he has asked for, but unfortunately they can also
|
|
|
|
* arise legitimately when the daemon is reconfigured and there exists (although
|
2000-06-07 21:25:53 +08:00
|
|
|
* for a short time period only) an old protocol instance being shut down and a new one
|
2000-06-02 00:16:49 +08:00
|
|
|
* willing to start up on the same interface.
|
|
|
|
*
|
|
|
|
* The solution is very simple: when any protocol wishes to use a network port
|
2000-06-07 21:25:53 +08:00
|
|
|
* or some other non-shareable resource, it asks the core to lock it and it doesn't
|
2000-06-02 00:16:49 +08:00
|
|
|
* use the resource until it's notified that it has acquired the lock.
|
|
|
|
*
|
2014-07-19 00:24:12 +08:00
|
|
|
* Object locks are represented by &object_lock structures which are in turn a
|
|
|
|
* kind of resource. Lockable resources are uniquely determined by resource type
|
2000-06-02 00:16:49 +08:00
|
|
|
* (%OBJLOCK_UDP for a UDP port etc.), IP address (usually a broadcast or
|
2014-07-19 00:24:12 +08:00
|
|
|
* multicast address the port is bound to), port number, interface and optional
|
|
|
|
* instance ID.
|
2000-06-02 00:16:49 +08:00
|
|
|
*/
|
|
|
|
|
2000-05-04 17:08:28 +08:00
|
|
|
#undef LOCAL_DEBUG
|
1999-12-10 02:54:20 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "nest/locks.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
|
|
|
|
static list olock_list;
|
|
|
|
static event *olock_event;
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
olock_same(struct object_lock *x, struct object_lock *y)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
x->type == y->type &&
|
|
|
|
x->iface == y->iface &&
|
|
|
|
x->port == y->port &&
|
2014-07-19 00:24:12 +08:00
|
|
|
x->inst == y->inst &&
|
1999-12-10 02:54:20 +08:00
|
|
|
ipa_equal(x->addr, y->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
olock_free(resource *r)
|
|
|
|
{
|
|
|
|
struct object_lock *q, *l = (struct object_lock *) r;
|
|
|
|
node *n;
|
|
|
|
|
|
|
|
DBG("olock: Freeing %p\n", l);
|
|
|
|
switch (l->state)
|
|
|
|
{
|
|
|
|
case OLOCK_STATE_FREE:
|
|
|
|
break;
|
|
|
|
case OLOCK_STATE_LOCKED:
|
|
|
|
case OLOCK_STATE_EVENT:
|
|
|
|
rem_node(&l->n);
|
|
|
|
n = HEAD(l->waiters);
|
|
|
|
if (n->next)
|
|
|
|
{
|
|
|
|
DBG("olock: -> %p becomes locked\n", n);
|
|
|
|
q = SKIP_BACK(struct object_lock, n, n);
|
|
|
|
rem_node(n);
|
2015-02-21 19:27:05 +08:00
|
|
|
add_tail_list(&q->waiters, &l->waiters);
|
1999-12-10 02:54:20 +08:00
|
|
|
q->state = OLOCK_STATE_EVENT;
|
|
|
|
add_head(&olock_list, n);
|
|
|
|
ev_schedule(olock_event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OLOCK_STATE_WAITING:
|
|
|
|
rem_node(&l->n);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
olock_dump(resource *r)
|
|
|
|
{
|
|
|
|
struct object_lock *l = (struct object_lock *) r;
|
|
|
|
static char *olock_states[] = { "free", "locked", "waiting", "event" };
|
|
|
|
|
2014-07-19 00:24:12 +08:00
|
|
|
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]);
|
1999-12-10 02:54:20 +08:00
|
|
|
if (!EMPTY_LIST(l->waiters))
|
|
|
|
debug(" [wanted]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resclass olock_class = {
|
|
|
|
"ObjLock",
|
|
|
|
sizeof(struct object_lock),
|
|
|
|
olock_free,
|
2010-02-21 21:34:53 +08:00
|
|
|
olock_dump,
|
2016-10-14 21:37:04 +08:00
|
|
|
NULL,
|
|
|
|
NULL,
|
1999-12-10 02:54:20 +08:00
|
|
|
};
|
|
|
|
|
2000-06-02 00:16:49 +08:00
|
|
|
/**
|
|
|
|
* olock_new - create an object lock
|
|
|
|
* @p: resource pool to create the lock in.
|
|
|
|
*
|
|
|
|
* The olock_new() function creates a new resource of type &object_lock
|
|
|
|
* and returns a pointer to it. After filling in the structure, the caller
|
|
|
|
* should call olock_acquire() to do the real locking.
|
|
|
|
*/
|
1999-12-10 02:54:20 +08:00
|
|
|
struct object_lock *
|
|
|
|
olock_new(pool *p)
|
|
|
|
{
|
|
|
|
struct object_lock *l = ralloc(p, &olock_class);
|
|
|
|
|
|
|
|
l->state = OLOCK_STATE_FREE;
|
|
|
|
init_list(&l->waiters);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2000-06-02 00:16:49 +08:00
|
|
|
/**
|
|
|
|
* olock_acquire - acquire a lock
|
|
|
|
* @l: the lock to acquire
|
|
|
|
*
|
|
|
|
* This function attempts to acquire exclusive access to the non-shareable
|
|
|
|
* resource described by the lock @l. It returns immediately, but as soon
|
|
|
|
* as the resource becomes available, it calls the hook() function set up
|
|
|
|
* by the caller.
|
|
|
|
*
|
|
|
|
* When you want to release the resource, just rfree() the lock.
|
|
|
|
*/
|
1999-12-10 02:54:20 +08:00
|
|
|
void
|
|
|
|
olock_acquire(struct object_lock *l)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
struct object_lock *q;
|
|
|
|
|
|
|
|
WALK_LIST(n, olock_list)
|
|
|
|
{
|
|
|
|
q = SKIP_BACK(struct object_lock, n, n);
|
|
|
|
if (olock_same(q, l))
|
|
|
|
{
|
|
|
|
l->state = OLOCK_STATE_WAITING;
|
|
|
|
add_tail(&q->waiters, &l->n);
|
|
|
|
DBG("olock: %p waits\n", l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DBG("olock: %p acquired immediately\n", l);
|
|
|
|
l->state = OLOCK_STATE_EVENT;
|
|
|
|
add_head(&olock_list, &l->n);
|
|
|
|
ev_schedule(olock_event);
|
|
|
|
}
|
|
|
|
|
2000-04-28 06:28:49 +08:00
|
|
|
static void
|
2004-06-05 17:10:56 +08:00
|
|
|
olock_run_event(void *unused UNUSED)
|
1999-12-10 02:54:20 +08:00
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
struct object_lock *q;
|
|
|
|
|
|
|
|
DBG("olock: Processing events\n");
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
n = HEAD(olock_list);
|
|
|
|
if (!n->next)
|
|
|
|
break;
|
|
|
|
q = SKIP_BACK(struct object_lock, n, n);
|
|
|
|
if (q->state != OLOCK_STATE_EVENT)
|
|
|
|
break;
|
|
|
|
DBG("olock: %p locked\n", q);
|
|
|
|
q->state = OLOCK_STATE_LOCKED;
|
|
|
|
rem_node(&q->n);
|
|
|
|
add_tail(&olock_list, &q->n);
|
|
|
|
q->hook(q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 00:16:49 +08:00
|
|
|
/**
|
|
|
|
* olock_init - initialize the object lock mechanism
|
|
|
|
*
|
|
|
|
* This function is called during BIRD startup. It initializes
|
|
|
|
* all the internal data structures of the lock module.
|
|
|
|
*/
|
1999-12-10 02:54:20 +08:00
|
|
|
void
|
|
|
|
olock_init(void)
|
|
|
|
{
|
|
|
|
DBG("olock: init\n");
|
|
|
|
init_list(&olock_list);
|
|
|
|
olock_event = ev_new(&root_pool);
|
|
|
|
olock_event->hook = olock_run_event;
|
|
|
|
}
|