summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/pff.c
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@ugent.be>2017-04-28 17:56:02 +0200
committerdimitri staessens <dimitri.staessens@ugent.be>2017-04-28 17:56:44 +0200
commit41b73370dba4105ae183babe8f89480e8c9d038a (patch)
tree50e15983363c163164362851d91c3b6f9511e408 /src/ipcpd/normal/pff.c
parent72e8f778024ece5c8025be4767ce89eae349f4b5 (diff)
downloadouroboros-41b73370dba4105ae183babe8f89480e8c9d038a.tar.gz
ouroboros-41b73370dba4105ae183babe8f89480e8c9d038a.zip
ipcpd: Fix some bugs for the normal split
Diffstat (limited to 'src/ipcpd/normal/pff.c')
-rw-r--r--src/ipcpd/normal/pff.c45
1 files changed, 29 insertions, 16 deletions
diff --git a/src/ipcpd/normal/pff.c b/src/ipcpd/normal/pff.c
index c26c2263..38ca7232 100644
--- a/src/ipcpd/normal/pff.c
+++ b/src/ipcpd/normal/pff.c
@@ -33,8 +33,8 @@
#include "pff.h"
struct pff {
- struct htable * table;
- pthread_mutex_t lock;
+ struct htable * table;
+ pthread_rwlock_t lock;
};
struct pff * pff_create(void)
@@ -45,14 +45,18 @@ struct pff * pff_create(void)
if (tmp == NULL)
return NULL;
+ if (pthread_rwlock_init(&tmp->lock, NULL)) {
+ free(tmp);
+ return NULL;
+ }
+
tmp->table = htable_create(PFT_SIZE, false);
if (tmp->table == NULL) {
+ pthread_rwlock_destroy(&tmp->lock);
free(tmp);
return NULL;
}
- pthread_mutex_init(&tmp->lock, NULL);
-
return tmp;
}
@@ -62,21 +66,23 @@ void pff_destroy(struct pff * instance)
htable_destroy(instance->table);
- pthread_mutex_destroy(&instance->lock);
+ pthread_rwlock_destroy(&instance->lock);
free(instance);
}
void pff_lock(struct pff * instance)
{
- pthread_mutex_lock(&instance->lock);
+ pthread_rwlock_wrlock(&instance->lock);
}
void pff_unlock(struct pff * instance)
{
- pthread_mutex_unlock(&instance->lock);
+ pthread_rwlock_unlock(&instance->lock);
}
-int pff_add(struct pff * instance, uint64_t addr, int fd)
+int pff_add(struct pff * instance,
+ uint64_t addr,
+ int fd)
{
int * val;
@@ -85,6 +91,7 @@ int pff_add(struct pff * instance, uint64_t addr, int fd)
val = malloc(sizeof(*val));
if (val == NULL)
return -ENOMEM;
+
*val = fd;
if (htable_insert(instance->table, addr, val)) {
@@ -95,7 +102,9 @@ int pff_add(struct pff * instance, uint64_t addr, int fd)
return 0;
}
-int pff_update(struct pff * instance, uint64_t addr, int fd)
+int pff_update(struct pff * instance,
+ uint64_t addr,
+ int fd)
{
int * val;
@@ -119,7 +128,8 @@ int pff_update(struct pff * instance, uint64_t addr, int fd)
return 0;
}
-int pff_remove(struct pff * instance, uint64_t addr)
+int pff_remove(struct pff * instance,
+ uint64_t addr)
{
assert(instance);
@@ -136,18 +146,21 @@ void pff_flush(struct pff * instance)
htable_flush(instance->table);
}
-int pff_nhop(struct pff * instance, uint64_t addr)
+int pff_nhop(struct pff * instance,
+ uint64_t addr)
{
int * j;
- int fd;
+ int fd = -1;
assert(instance);
+ pthread_rwlock_rdlock(&instance->lock);
+
j = (int *) htable_lookup(instance->table, addr);
- if (j == NULL) {
- return -1;
- }
- fd = *j;
+ if (j != NULL)
+ fd = *j;
+
+ pthread_rwlock_unlock(&instance->lock);
return fd;
}