diff options
-rw-r--r-- | flowtop.c | 9 | ||||
-rw-r--r-- | locking.h | 31 |
2 files changed, 40 insertions, 0 deletions
@@ -80,6 +80,7 @@ struct flow_list { static volatile sig_atomic_t sigint = 0; static int what = INCLUDE_IPV4 | INCLUDE_IPV6 | INCLUDE_TCP, show_src = 0; static struct flow_list flow_list; +static struct condlock collector_ready; static const char *short_options = "vhTUsDIS46u"; static const struct option long_options[] = { @@ -978,6 +979,8 @@ static void presenter(void) int skip_lines = 0; WINDOW *screen; + condlock_wait(&collector_ready); + lookup_init_ports(PORTS_TCP); lookup_init_ports(PORTS_UDP); screen = screen_init(false); @@ -1104,6 +1107,8 @@ static void *collector(void *null __maybe_unused) nfct_filter_destroy(filter); flow_list_init(&flow_list); + condlock_signal(&collector_ready); + rcu_register_thread(); while (!sigint && ret >= 0) @@ -1179,12 +1184,16 @@ int main(int argc, char **argv) init_geoip(1); + condlock_init(&collector_ready); + ret = pthread_create(&tid, NULL, collector, NULL); if (ret < 0) panic("Cannot create phthread!\n"); presenter(); + condlock_destroy(&collector_ready); + destroy_geoip(); return 0; @@ -15,6 +15,11 @@ struct rwlock { pthread_rwlock_t lock; }; +struct condlock { + pthread_mutex_t lock; + pthread_cond_t cond; +}; + static inline int spinlock_init(struct spinlock *l) { return -pthread_spin_init(&l->lock, 0); @@ -86,4 +91,30 @@ static inline void rwlock_unlock(struct rwlock *l) pthread_rwlock_unlock(&l->lock); } +static inline void condlock_init(struct condlock *c) +{ + pthread_mutex_init(&c->lock, NULL); + pthread_cond_init(&c->cond, NULL); +} + +static inline void condlock_signal(struct condlock *c) +{ + pthread_mutex_lock(&c->lock); + pthread_cond_signal(&c->cond); + pthread_mutex_unlock(&c->lock); +} + +static inline void condlock_wait(struct condlock *c) +{ + pthread_mutex_lock(&c->lock); + pthread_cond_wait(&c->cond, &c->lock); + pthread_mutex_unlock(&c->lock); +} + +static inline void condlock_destroy(struct condlock *c) +{ + pthread_mutex_destroy(&c->lock); + pthread_cond_destroy(&c->cond); +} + #endif /* LOCKING_H */ |