summaryrefslogtreecommitdiff
path: root/locking.h
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2015-03-19 02:02:44 +0200
committerTobias Klauser <tklauser@distanz.ch>2015-03-20 17:53:15 +0100
commit451275470106024f106a310a5af050b3ca046a4f (patch)
tree291cfa92cf4e06a0d3a10e7721a3913c2ec90538 /locking.h
parent3dfaab495b62658b88a1ed201b2ed61aea34570d (diff)
flowtop: Don't init screen until collector is ready
In case if main thread already initialized screen but then collector called panic, the process exits but console stays with the same colored screen and shifted shell prompt. Fixed by adding conditional variable locking. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'locking.h')
-rw-r--r--locking.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/locking.h b/locking.h
index 51034b3..cb57a9d 100644
--- a/locking.h
+++ b/locking.h
@@ -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 */