- ovs_rwlock_rdlock(&monitor_rwlock);
- HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) {
- if (mport->cfm) {
- cfm_run(mport->cfm);
- }
- if (mport->bfd) {
- bfd_run(mport->bfd);
- }
+ /* If the monitor thread is not running and the hmap
+ * is not empty, starts it. If it is and the hmap is empty,
+ * terminates it. */
+ if (!monitor_running && !hmap_is_empty(&monitor_hmap)) {
+ latch_init(&monitor_exit_latch);
+ xpthread_create(&monitor_tid, NULL, monitor_main, NULL);
+ monitor_running = true;
+ } else if (monitor_running && hmap_is_empty(&monitor_hmap)) {
+ latch_set(&monitor_exit_latch);
+ xpthread_join(monitor_tid, NULL);
+ latch_destroy(&monitor_exit_latch);
+ monitor_running = false;