0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 12:48:43 +00:00

Resource system: Locking

This commit is contained in:
Maria Matejka 2020-04-27 16:14:02 +02:00
parent c92c8c6913
commit 327ffa973d
2 changed files with 42 additions and 2 deletions

View File

@ -9,6 +9,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <pthread.h>
#include "nest/bird.h" #include "nest/bird.h"
#include "lib/resource.h" #include "lib/resource.h"
@ -31,6 +32,7 @@
struct pool { struct pool {
resource r; resource r;
list inside; list inside;
pthread_mutex_t mutex;
const char *name; const char *name;
}; };
@ -66,6 +68,7 @@ rp_new(pool *p, const char *name)
pool *z = ralloc(p, &pool_class); pool *z = ralloc(p, &pool_class);
z->name = name; z->name = name;
init_list(&z->inside); init_list(&z->inside);
pthread_mutex_init(&z->mutex, NULL);
return z; return z;
} }
@ -75,6 +78,8 @@ pool_free(resource *P)
pool *p = (pool *) P; pool *p = (pool *) P;
resource *r, *rr; resource *r, *rr;
pthread_mutex_lock(&p->mutex);
r = HEAD(p->inside); r = HEAD(p->inside);
while (rr = (resource *) r->n.next) while (rr = (resource *) r->n.next)
{ {
@ -82,6 +87,8 @@ pool_free(resource *P)
xfree(r); xfree(r);
r = rr; r = rr;
} }
pthread_mutex_unlock(&p->mutex);
} }
static void static void
@ -90,11 +97,14 @@ pool_dump(resource *P)
pool *p = (pool *) P; pool *p = (pool *) P;
resource *r; resource *r;
pthread_mutex_lock(&p->mutex);
debug("%s\n", p->name); debug("%s\n", p->name);
indent += 3; indent += 3;
WALK_LIST(r, p->inside) WALK_LIST(r, p->inside)
rdump(r); rdump(r);
indent -= 3; indent -= 3;
pthread_mutex_unlock(&p->mutex);
} }
static size_t static size_t
@ -104,8 +114,10 @@ pool_memsize(resource *P)
resource *r; resource *r;
size_t sum = sizeof(pool) + ALLOC_OVERHEAD; size_t sum = sizeof(pool) + ALLOC_OVERHEAD;
pthread_mutex_lock(&p->mutex);
WALK_LIST(r, p->inside) WALK_LIST(r, p->inside)
sum += rmemsize(r); sum += rmemsize(r);
pthread_mutex_unlock(&p->mutex);
return sum; return sum;
} }
@ -116,9 +128,12 @@ pool_lookup(resource *P, unsigned long a)
pool *p = (pool *) P; pool *p = (pool *) P;
resource *r, *q; resource *r, *q;
pthread_mutex_lock(&p->mutex);
WALK_LIST(r, p->inside) WALK_LIST(r, p->inside)
if (r->class->lookup && (q = r->class->lookup(r, a))) if (r->class->lookup && (q = r->class->lookup(r, a)))
return q; return q;
pthread_mutex_unlock(&p->mutex);
return NULL; return NULL;
} }
@ -136,9 +151,16 @@ void rmove(void *res, pool *p)
if (r) if (r)
{ {
if (r->n.next) if (r->parent)
{
pthread_mutex_lock(&r->parent->mutex);
rem_node(&r->n); rem_node(&r->n);
pthread_mutex_unlock(&r->parent->mutex);
}
pthread_mutex_lock(&p->mutex);
add_tail(&p->inside, &r->n); add_tail(&p->inside, &r->n);
pthread_mutex_unlock(&p->mutex);
} }
} }
@ -160,8 +182,13 @@ rfree(void *res)
if (!r) if (!r)
return; return;
if (r->n.next) if (r->parent)
{
pthread_mutex_lock(&r->parent->mutex);
rem_node(&r->n); rem_node(&r->n);
pthread_mutex_unlock(&r->parent->mutex);
}
r->class->free(r); r->class->free(r);
r->class = NULL; r->class = NULL;
xfree(r); xfree(r);
@ -221,8 +248,14 @@ ralloc(pool *p, struct resclass *c)
bzero(r, c->size); bzero(r, c->size);
r->class = c; r->class = c;
r->parent = p;
if (p) if (p)
{
pthread_mutex_lock(&p->mutex);
add_tail(&p->inside, &r->n); add_tail(&p->inside, &r->n);
pthread_mutex_unlock(&p->mutex);
}
return r; return r;
} }
@ -262,6 +295,7 @@ resource_init(void)
root_pool.r.class = &pool_class; root_pool.r.class = &pool_class;
root_pool.name = "Root"; root_pool.name = "Root";
init_list(&root_pool.inside); init_list(&root_pool.inside);
pthread_mutex_init(&root_pool.mutex, NULL);
} }
/** /**
@ -340,8 +374,11 @@ mb_alloc(pool *p, unsigned size)
struct mblock *b = xmalloc(sizeof(struct mblock) + size); struct mblock *b = xmalloc(sizeof(struct mblock) + size);
b->r.class = &mb_class; b->r.class = &mb_class;
b->r.parent = p;
b->r.n = (node) {}; b->r.n = (node) {};
pthread_mutex_lock(&p->mutex);
add_tail(&p->inside, &b->r.n); add_tail(&p->inside, &b->r.n);
pthread_mutex_unlock(&p->mutex);
b->size = size; b->size = size;
return b->data; return b->data;
} }
@ -387,8 +424,10 @@ mb_realloc(void *m, unsigned size)
{ {
struct mblock *b = SKIP_BACK(struct mblock, data, m); struct mblock *b = SKIP_BACK(struct mblock, data, m);
pthread_mutex_lock(&b->r.parent->mutex);
b = xrealloc(b, sizeof(struct mblock) + size); b = xrealloc(b, sizeof(struct mblock) + size);
update_node(&b->r.n); update_node(&b->r.n);
pthread_mutex_unlock(&b->r.parent->mutex);
b->size = size; b->size = size;
return b->data; return b->data;
} }

View File

@ -15,6 +15,7 @@
typedef struct resource { typedef struct resource {
node n; /* Inside resource pool */ node n; /* Inside resource pool */
struct pool *parent; /* Parent resource pool */
struct resclass *class; /* Resource class */ struct resclass *class; /* Resource class */
} resource; } resource;