Skip to content

Commit

Permalink
ovs-atomic: Add atomic_destroy() and use everywhere it is needed.
Browse files Browse the repository at this point in the history
C11 is able to require that atomics don't need to be destroyed, but some
of the OVS implementations do.

Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
blp committed Jan 9, 2014
1 parent 4597d2a commit c5f81b2
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ bfd_unref(struct bfd *bfd) OVS_EXCLUDED(mutex)
hmap_remove(all_bfds, &bfd->node);
netdev_close(bfd->netdev);
free(bfd->name);
atomic_destroy(&bfd->ref_cnt);
free(bfd);
ovs_mutex_unlock(&mutex);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/cfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ cfm_unref(struct cfm *cfm) OVS_EXCLUDED(mutex)
hmap_destroy(&cfm->remote_mps);
netdev_close(cfm->netdev);
free(cfm->rmps_array);

atomic_destroy(&cfm->extended);
atomic_destroy(&cfm->check_tnl_key);
atomic_destroy(&cfm->ref_cnt);

free(cfm);
}

Expand Down
1 change: 1 addition & 0 deletions lib/lacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ lacp_unref(struct lacp *lacp) OVS_EXCLUDED(mutex)
hmap_destroy(&lacp->slaves);
list_remove(&lacp->node);
free(lacp->name);
atomic_destroy(&lacp->ref_cnt);
free(lacp);
ovs_mutex_unlock(&mutex);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/mac-learning.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -151,6 +151,7 @@ mac_learning_unref(struct mac_learning *ml)

bitmap_free(ml->flood_vlans);
ovs_rwlock_destroy(&ml->rwlock);
atomic_destroy(&ml->ref_cnt);
free(ml);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ netdev_unregister_provider(const char *type)
atomic_read(&rc->ref_cnt, &ref_cnt);
if (!ref_cnt) {
hmap_remove(&netdev_classes, &rc->hmap_node);
atomic_destroy(&rc->ref_cnt);
free(rc);
error = 0;
} else {
Expand Down
1 change: 1 addition & 0 deletions lib/ovs-atomic-clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef _Atomic(int64_t) atomic_int64_t;
#define ATOMIC_VAR_INIT(VALUE) (VALUE)

#define atomic_init(OBJECT, VALUE) __c11_atomic_init(OBJECT, VALUE)
#define atomic_destroy(OBJECT) ((void) (OBJECT))

/* Clang hard-codes these exact values internally but does not appear to
* export any names for them. */
Expand Down
1 change: 1 addition & 0 deletions lib/ovs-atomic-gcc4+.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ int64_t locked_int64_and(struct locked_int64 *, int64_t arg);

#define ATOMIC_VAR_INIT(VALUE) { .value = (VALUE) }
#define atomic_init(OBJECT, VALUE) ((OBJECT)->value = (VALUE), (void) 0)
#define atomic_destroy(OBJECT) ((void) (OBJECT))

static inline void
atomic_thread_fence(memory_order order)
Expand Down
1 change: 1 addition & 0 deletions lib/ovs-atomic-gcc4.7+.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef enum {

#define ATOMIC_VAR_INIT(VALUE) (VALUE)
#define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
#define atomic_destroy(OBJECT) ((void) (OBJECT))

#define atomic_thread_fence __atomic_thread_fence
#define atomic_signal_fence __atomic_signal_fence
Expand Down
3 changes: 3 additions & 0 deletions lib/ovs-atomic-pthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ typedef enum {
((OBJECT)->value = (VALUE), \
pthread_mutex_init(&(OBJECT)->mutex, NULL), \
(void) 0)
#define atomic_destroy(OBJECT) \
(pthread_mutex_destroy(&(OBJECT)->mutex), \
(void) 0)

static inline void
atomic_thread_fence(memory_order order OVS_UNUSED)
Expand Down
13 changes: 11 additions & 2 deletions lib/ovs-atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
* that.
*
*
* Initialization
* ==============
* Life Cycle
* ==========
*
* To initialize an atomic variable at its point of definition, use
* ATOMIC_VAR_INIT:
Expand All @@ -98,6 +98,15 @@
* ...
* atomic_init(&ai, 123);
*
* C11 does not hav an destruction function for atomic types, but some
* implementations of the OVS atomics do need them. Thus, the following
* function is provided for destroying non-static atomic objects (A is any
* atomic type):
*
* void atomic_destroy(A *object);
*
* Destroys 'object'.
*
*
* Barriers
* ========
Expand Down
1 change: 1 addition & 0 deletions lib/stp.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ stp_unref(struct stp *stp)
list_remove(&stp->node);
ovs_mutex_unlock(&mutex);
free(stp->name);
atomic_destroy(&stp->ref_cnt);
free(stp);
}
}
Expand Down
1 change: 1 addition & 0 deletions ofproto/bond.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ bond_unref(struct bond *bond)

free(bond->hash);
free(bond->name);
atomic_destroy(&bond->ref_cnt);
free(bond);
}

Expand Down
3 changes: 2 additions & 1 deletion ofproto/netflow.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
* Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -438,6 +438,7 @@ netflow_unref(struct netflow *nf)
atomic_sub(&netflow_count, 1, &orig);
collectors_destroy(nf->collectors);
ofpbuf_uninit(&nf->packet);
atomic_destroy(&nf->ref_cnt);
free(nf);
}
}
Expand Down
3 changes: 2 additions & 1 deletion ofproto/ofproto-dpif-ipfix.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012 Nicira, Inc.
* Copyright (c) 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -696,6 +696,7 @@ dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
dpif_ipfix_clear(di);
dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
hmap_destroy(&di->flow_exporter_map);
atomic_destroy(&di->ref_cnt);
free(di);
ovs_mutex_unlock(&mutex);
}
Expand Down
1 change: 1 addition & 0 deletions ofproto/ofproto-dpif-sflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ dpif_sflow_unref(struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
dpif_sflow_del_port__(ds, dsp);
}
hmap_destroy(&ds->ports);
atomic_destroy(&ds->ref_cnt);
free(ds);
}
}
Expand Down
2 changes: 2 additions & 0 deletions ofproto/ofproto-dpif-upcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ udpif_destroy(struct udpif *udpif)
latch_destroy(&udpif->exit_latch);
seq_destroy(udpif->reval_seq);
seq_destroy(udpif->dump_seq);
atomic_destroy(&udpif->max_idle);
atomic_destroy(&udpif->flow_limit);
free(udpif);
}

Expand Down
5 changes: 4 additions & 1 deletion ofproto/ofproto.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
* Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -2578,6 +2578,7 @@ ofproto_rule_destroy__(struct rule *rule)
cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
rule_actions_unref(rule->actions);
ovs_mutex_destroy(&rule->mutex);
atomic_destroy(&rule->ref_count);
rule->ofproto->ofproto_class->rule_dealloc(rule);
}

Expand Down Expand Up @@ -2625,6 +2626,7 @@ rule_actions_unref(struct rule_actions *actions)

atomic_sub(&actions->ref_count, 1, &orig);
if (orig == 1) {
atomic_destroy(&actions->ref_count);
free(actions->ofpacts);
free(actions);
} else {
Expand Down Expand Up @@ -6665,6 +6667,7 @@ oftable_destroy(struct oftable *table)
oftable_disable_eviction(table);
classifier_destroy(&table->cls);
free(table->name);
atomic_destroy(&table->config);
}

/* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
Expand Down
2 changes: 2 additions & 0 deletions tests/test-atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
ovs_assert(orig == 2); \
atomic_read(&x, &value); \
ovs_assert(value == 8); \
\
atomic_destroy(&x); \
}

static void
Expand Down

0 comments on commit c5f81b2

Please sign in to comment.