summaryrefslogtreecommitdiff
path: root/lib/rconn.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2013-09-13 15:07:35 -0700
committerBen Pfaff <blp@nicira.com>2013-10-11 10:12:54 -0700
commita3d1ff00de09c1ea64cab59135d6eb0d0de369b0 (patch)
tree8952c81f4d011e66602fa5f94bbd6a81a8e0504c /lib/rconn.c
parent30fe866b46dd6b5658dfa7c2fb60b8f756ea6f95 (diff)
downloadopenvswitch-a3d1ff00de09c1ea64cab59135d6eb0d0de369b0.tar.gz
rconn: Make rconn_packet_counter thread-safe.
Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Ethan Jackson <ethan@nicira.com>
Diffstat (limited to 'lib/rconn.c')
-rw-r--r--lib/rconn.c61
1 files changed, 51 insertions, 10 deletions
diff --git a/lib/rconn.c b/lib/rconn.c
index 64cc6d067..9273cff83 100644
--- a/lib/rconn.c
+++ b/lib/rconn.c
@@ -664,7 +664,7 @@ int
rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b,
struct rconn_packet_counter *counter, int queue_limit)
{
- if (counter->n_packets < queue_limit) {
+ if (rconn_packet_counter_n_packets(counter) < queue_limit) {
return rconn_send(rc, b, counter);
} else {
COVERAGE_INC(rconn_overflow);
@@ -863,7 +863,10 @@ struct rconn_packet_counter *
rconn_packet_counter_create(void)
{
struct rconn_packet_counter *c = xzalloc(sizeof *c);
+ ovs_mutex_init(&c->mutex);
+ ovs_mutex_lock(&c->mutex);
c->ref_cnt = 1;
+ ovs_mutex_unlock(&c->mutex);
return c;
}
@@ -871,8 +874,15 @@ void
rconn_packet_counter_destroy(struct rconn_packet_counter *c)
{
if (c) {
+ bool dead;
+
+ ovs_mutex_lock(&c->mutex);
ovs_assert(c->ref_cnt > 0);
- if (!--c->ref_cnt && !c->n_packets) {
+ dead = !--c->ref_cnt && !c->n_packets;
+ ovs_mutex_unlock(&c->mutex);
+
+ if (dead) {
+ ovs_mutex_destroy(&c->mutex);
free(c);
}
}
@@ -881,25 +891,56 @@ rconn_packet_counter_destroy(struct rconn_packet_counter *c)
void
rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes)
{
+ ovs_mutex_lock(&c->mutex);
c->n_packets++;
c->n_bytes += n_bytes;
+ ovs_mutex_unlock(&c->mutex);
}
void
rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes)
{
- ovs_assert(c->n_packets > 0);
- ovs_assert(c->n_bytes >= n_bytes);
+ bool dead = false;
- c->n_bytes -= n_bytes;
+ ovs_mutex_lock(&c->mutex);
+ ovs_assert(c->n_packets > 0);
+ ovs_assert(c->n_packets == 1
+ ? c->n_bytes == n_bytes
+ : c->n_bytes > n_bytes);
c->n_packets--;
- if (!c->n_packets) {
- ovs_assert(!c->n_bytes);
- if (!c->ref_cnt) {
- free(c);
- }
+ c->n_bytes -= n_bytes;
+ dead = !c->n_packets && !c->ref_cnt;
+ ovs_mutex_unlock(&c->mutex);
+
+ if (dead) {
+ ovs_mutex_destroy(&c->mutex);
+ free(c);
}
}
+
+unsigned int
+rconn_packet_counter_n_packets(const struct rconn_packet_counter *c)
+{
+ unsigned int n;
+
+ ovs_mutex_lock(&c->mutex);
+ n = c->n_packets;
+ ovs_mutex_unlock(&c->mutex);
+
+ return n;
+}
+
+unsigned int
+rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c)
+{
+ unsigned int n;
+
+ ovs_mutex_lock(&c->mutex);
+ n = c->n_bytes;
+ ovs_mutex_unlock(&c->mutex);
+
+ return n;
+}
/* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name'
* is null, 'target' is used.