Use C++11 std::mutex and std::lock_guard instead of our RAII wrapper.
[cubemap] / httpinput.cpp
index 155e0b3..05ff37c 100644 (file)
@@ -20,7 +20,6 @@
 #include "httpinput.h"
 #include "log.h"
 #include "metacube2.h"
-#include "mutexlock.h"
 #include "parse.h"
 #include "serverpool.h"
 #include "state.pb.h"
@@ -51,7 +50,6 @@ HTTPInput::HTTPInput(const string &url, Input::Encoding encoding)
          url(url),
          encoding(encoding)
 {
-       pthread_mutex_init(&stats_mutex, nullptr);
        stats.url = url;
        stats.bytes_received = 0;
        stats.data_bytes_received = 0;
@@ -80,7 +78,6 @@ HTTPInput::HTTPInput(const InputProto &serialized)
        string protocol, user;
        parse_url(url, &protocol, &user, &host, &port, &path);  // Don't care if it fails.
 
-       pthread_mutex_init(&stats_mutex, nullptr);
        stats.url = url;
        stats.bytes_received = serialized.bytes_received();
        stats.data_bytes_received = serialized.data_bytes_received();
@@ -107,7 +104,7 @@ void HTTPInput::close_socket()
                sock = -1;
        }
 
-       MutexLock lock(&stats_mutex);
+       lock_guard<mutex> lock(stats_mutex);
        stats.connect_time = -1;
 }
 
@@ -396,7 +393,7 @@ void HTTPInput::do_work()
                                        request_bytes_sent = 0;
                                }
 
-                               MutexLock lock(&stats_mutex);
+                               lock_guard<mutex> lock(stats_mutex);
                                stats.connect_time = time(nullptr);
                                clock_gettime(CLOCK_MONOTONIC_COARSE, &last_activity);
                        }
@@ -566,7 +563,7 @@ void HTTPInput::do_work()
 void HTTPInput::process_data(char *ptr, size_t bytes)
 {
        {
-               MutexLock mutex(&stats_mutex);
+               lock_guard<mutex> lock(stats_mutex);
                stats.bytes_received += bytes;
        }
 
@@ -645,14 +642,14 @@ void HTTPInput::process_data(char *ptr, size_t bytes)
                // TODO: Keep metadata when sending on to other Metacube users.
                if (flags & METACUBE_FLAGS_METADATA) {
                        {
-                               MutexLock lock(&stats_mutex);
+                               lock_guard<mutex> lock(stats_mutex);
                                stats.metadata_bytes_received += size;
                        }
                        process_metacube_metadata_block(hdr, pending_data.data() + sizeof(hdr), size);
                } else {
                        // Send this block on to the servers.
                        {
-                               MutexLock lock(&stats_mutex);
+                               lock_guard<mutex> lock(stats_mutex);
                                stats.data_bytes_received += size;
                        }
                        char *inner_data = pending_data.data() + sizeof(metacube2_block_header);
@@ -694,7 +691,7 @@ void HTTPInput::add_destination(int stream_index)
 
 InputStats HTTPInput::get_stats() const
 {
-       MutexLock lock(&stats_mutex);
+       lock_guard<mutex> lock(stats_mutex);
        return stats;
 }
 
@@ -725,7 +722,7 @@ void HTTPInput::process_metacube_metadata_block(const metacube2_block_header &hd
        double elapsed = now.tv_sec - be64toh(pkt->tv_sec) +
                1e-9 * (now.tv_nsec - long(be64toh(pkt->tv_nsec)));
        {
-               MutexLock lock(&stats_mutex);
+               lock_guard<mutex> lock(stats_mutex);
                stats.latency_sec = elapsed;
        }
 }