Fix so clients would actually be woken up from the worker thread, not the input threa...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 7 Apr 2013 21:26:37 +0000 (23:26 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 7 Apr 2013 21:26:37 +0000 (23:26 +0200)
server.cpp
server.h

index eeab64b..d41e3c7 100644 (file)
@@ -162,7 +162,7 @@ void Server::do_work()
                if (should_stop) {
                        return;
                }
-       
+
                for (int i = 0; i < nfds; ++i) {
                        int fd = events[i].data.fd;
                        assert(clients.count(fd) != 0);
@@ -175,6 +175,11 @@ void Server::do_work()
 
                        process_client(client);
                }
+
+               for (unsigned i = 0; i < to_process.size(); ++i) {
+                       process_client(to_process[i]);
+               }
+               to_process.clear();
        }
 }
 
@@ -579,10 +584,11 @@ void Server::put_client_to_sleep(Client *client)
 
 void Server::wake_up_all_clients()
 {
-       vector<Client *> to_process;
-       swap(sleeping_clients, to_process);
-       for (unsigned i = 0; i < to_process.size(); ++i) {
-               process_client(to_process[i]);
+       if (to_process.empty()) {
+               swap(sleeping_clients, to_process);
+       } else {
+               to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
+               sleeping_clients.clear();
        }
 }
        
index fd309f4..6e0dd9d 100644 (file)
--- a/server.h
+++ b/server.h
@@ -124,6 +124,10 @@ private:
        // See put_client_to_sleep() and wake_up_all_clients().
        std::vector<Client *> sleeping_clients;
 
+       // Clients that we recently got data for (when they were in
+       // <sleeping_clients>).
+       std::vector<Client *> to_process;
+
        // Recover the this pointer, and call do_work().
        static void *do_work_thunk(void *arg);