X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=server.cpp;h=852ecddf1f6212bfb1efb93d050d4fc37473a673;hp=2ec2fe0c2cdc1e9b60bfa029ddbfda457eb7f873;hb=50651c954803c1941e6ad1bb494712891c18f7d2;hpb=f0621e41fdb96ce1bd58e7561e0aa76345072ba3 diff --git a/server.cpp b/server.cpp index 2ec2fe0..852ecdd 100644 --- a/server.cpp +++ b/server.cpp @@ -23,7 +23,6 @@ #include "accesslog.h" #include "log.h" #include "metacube2.h" -#include "mutexlock.h" #include "parse.h" #include "server.h" #include "state.pb.h" @@ -57,9 +56,6 @@ inline bool is_earlier(timespec a, timespec b) Server::Server() { - pthread_mutex_init(&mutex, nullptr); - pthread_mutex_init(&queued_clients_mutex, nullptr); - epoll_fd = epoll_create(1024); // Size argument is ignored. if (epoll_fd == -1) { log_perror("epoll_fd"); @@ -76,7 +72,7 @@ vector Server::get_client_stats() const { vector ret; - MutexLock lock(&mutex); + lock_guard lock(mu); for (const auto &fd_and_client : clients) { ret.push_back(fd_and_client.second.get_stats()); } @@ -100,7 +96,7 @@ void Server::do_work() exit(1); } - MutexLock lock(&mutex); // We release the mutex between iterations. + lock_guard lock(mu); // We release the mutex between iterations. process_queued_data(); @@ -199,7 +195,7 @@ CubemapStateProto Server::serialize() void Server::add_client_deferred(int sock, Acceptor *acceptor) { - MutexLock lock(&queued_clients_mutex); + lock_guard lock(queued_clients_mutex); queued_add_clients.push_back(std::make_pair(sock, acceptor)); } @@ -254,7 +250,7 @@ void Server::add_client(int sock, Acceptor *acceptor) void Server::add_client_from_serialized(const ClientProto &client) { - MutexLock lock(&mutex); + lock_guard lock(mu); Stream *stream; int stream_index = lookup_stream_by_url(client.url()); if (stream_index == -1) { @@ -313,7 +309,7 @@ int Server::lookup_stream_by_url(const string &url) const int Server::add_stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Stream::Encoding encoding, Stream::Encoding src_encoding) { - MutexLock lock(&mutex); + lock_guard lock(mu); stream_url_map.insert(make_pair(url, streams.size())); streams.emplace_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding)); return streams.size() - 1; @@ -321,7 +317,7 @@ int Server::add_stream(const string &url, size_t backlog_size, size_t prebufferi int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd) { - MutexLock lock(&mutex); + lock_guard lock(mu); stream_url_map.insert(make_pair(stream.url(), streams.size())); streams.emplace_back(new Stream(stream, data_fd)); return streams.size() - 1; @@ -329,35 +325,35 @@ int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd) void Server::set_backlog_size(int stream_index, size_t new_size) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->set_backlog_size(new_size); } void Server::set_prebuffering_bytes(int stream_index, size_t new_amount) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->prebuffering_bytes = new_amount; } void Server::set_encoding(int stream_index, Stream::Encoding encoding) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->encoding = encoding; } void Server::set_src_encoding(int stream_index, Stream::Encoding encoding) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->src_encoding = encoding; } void Server::set_header(int stream_index, const string &http_header, const string &stream_header) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->http_header = http_header; @@ -376,7 +372,7 @@ void Server::set_header(int stream_index, const string &http_header, const strin void Server::set_pacing_rate(int stream_index, uint32_t pacing_rate) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(clients.empty()); assert(stream_index >= 0 && stream_index < ssize_t(streams.size())); streams[stream_index]->pacing_rate = pacing_rate; @@ -384,7 +380,7 @@ void Server::set_pacing_rate(int stream_index, uint32_t pacing_rate) void Server::add_gen204(const std::string &url, const std::string &allow_origin) { - MutexLock lock(&mutex); + lock_guard lock(mu); assert(clients.empty()); ping_url_map[url] = allow_origin; } @@ -978,7 +974,7 @@ void Server::change_epoll_events(Client *client, uint32_t events) void Server::process_queued_data() { { - MutexLock lock(&queued_clients_mutex); + lock_guard lock(queued_clients_mutex); for (const pair &id_and_acceptor : queued_add_clients) { add_client(id_and_acceptor.first, id_and_acceptor.second);