Description: Fix locking
Author: Simon Richter <sjr@debian.org>
Last-Update: 2023-12-16

--- librevisa-0.0.20130812.orig/src/messagepump.cpp
+++ librevisa-0.0.20130812/src/messagepump.cpp
@@ -32,11 +32,20 @@
 namespace librevisa {
 
 messagepump::messagepump() throw() :
+        keep_running(true),
         worker(*this)
 {
         return;
 }
 
+messagepump::~messagepump() throw()
+{
+        keep_running = false;
+        lock l(cs);
+        cv.signal();
+        worker.kill(SIGUSR1);
+}
+
 void messagepump::register_watch(watch &w)
 {
         worker.start();
@@ -120,7 +129,7 @@ void messagepump::run()
         timeval now;
         ::gettimeofday(&now, 0);
 
-        for(;;)
+        while(keep_running)
         {
                 timeout *expired = 0;
                 bool have_timeout = false;
@@ -212,6 +221,9 @@ void messagepump::run()
                         next_as_ts.tv_nsec = next.tv_usec * 1000;
                 }
 
+                if(maxfd == -1 && !have_timeout)
+                        break;
+
                 int rc = ::pselect(maxfd + 1, &readfds, &writefds, &exceptfds, have_timeout? &next_as_ts : 0, &mask);
                 if(rc == -1)
                 {
--- librevisa-0.0.20130812.orig/src/messagepump.h
+++ librevisa-0.0.20130812/src/messagepump.h
@@ -33,6 +33,7 @@ class messagepump :
 {
 public:
         messagepump() throw();
+        ~messagepump() throw();
 
         // thread::runnable
         virtual void init();
@@ -84,6 +85,8 @@ public:
         void update_timeout(timeout &, timeval const *);
 
 private:
+        bool keep_running;
+
         typedef intrusive_list<watch> watch_list;
         typedef watch_list::iterator watch_iterator;
         watch_list watches;
--- librevisa-0.0.20130812.orig/src/thread_pthread.h
+++ librevisa-0.0.20130812/src/thread_pthread.h
@@ -40,6 +40,12 @@ public:
         };
 
         thread(runnable &r) : sui(r) { }
+        ~thread() throw()
+        {
+                if(!sui.running)
+                        return;
+                pthread_join(handle, 0);
+        }
 
         void start()
         {
@@ -54,6 +60,9 @@ public:
 
         void kill(int sig)
         {
+                if(!sui.running)
+                        return;
+
                 pthread_kill(handle, sig);
         }
 
