-/* Increments the counter with the given NAME. Coverage counters need not be
- * declared explicitly, but when you add the first coverage counter to a given
- * file, you must also add that file to COVERAGE_FILES in lib/automake.mk. */
-#define COVERAGE_INC(NAME) \
- do { \
- extern struct coverage_counter NAME##_count; \
- NAME##_count.count++; \
- } while (0)
-
-/* Adds AMOUNT to the coverage counter with the given NAME. */
-#define COVERAGE_ADD(NAME, AMOUNT) \
- do { \
- extern struct coverage_counter NAME##_count; \
- NAME##_count.count += AMOUNT; \
- } while (0)
+void coverage_counter_register(struct coverage_counter*);
+
+/* Defines COUNTER. There must be exactly one such definition at file scope
+ * within a program. */
+#define COVERAGE_DEFINE(COUNTER) \
+ DEFINE_STATIC_PER_THREAD_DATA(unsigned int, \
+ counter_##COUNTER, 0); \
+ static unsigned int COUNTER##_count(void) \
+ { \
+ unsigned int *countp = counter_##COUNTER##_get(); \
+ unsigned int count = *countp; \
+ *countp = 0; \
+ return count; \
+ } \
+ static inline void COUNTER##_add(unsigned int n) \
+ { \
+ *counter_##COUNTER##_get() += n; \
+ } \
+ extern struct coverage_counter counter_##COUNTER; \
+ struct coverage_counter counter_##COUNTER \
+ = { #COUNTER, COUNTER##_count, 0, 0, {0}, {0} }; \
+ OVS_CONSTRUCTOR(COUNTER##_init) { \
+ coverage_counter_register(&counter_##COUNTER); \
+ }
+
+/* Adds 1 to COUNTER. */
+#define COVERAGE_INC(COUNTER) COVERAGE_ADD(COUNTER, 1)
+
+/* Adds AMOUNT to COUNTER. */
+#define COVERAGE_ADD(COUNTER, AMOUNT) COUNTER##_add(AMOUNT)