Server::Server()
{
pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&queued_data_mutex, NULL);
+ pthread_mutex_init(&queued_clients_mutex, NULL);
epoll_fd = epoll_create(1024); // Size argument is ignored.
if (epoll_fd == -1) {
void Server::add_client_deferred(int sock)
{
- MutexLock lock(&queued_data_mutex);
+ MutexLock lock(&queued_clients_mutex);
queued_add_clients.push_back(sock);
}
void Server::add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
{
- MutexLock lock(&queued_data_mutex);
assert(stream_index >= 0 && stream_index < ssize_t(streams.size()));
streams[stream_index]->add_data_deferred(data, bytes, suitable_for_stream_start);
}
void Server::process_queued_data()
{
- MutexLock lock(&queued_data_mutex);
+ {
+ MutexLock lock(&queued_clients_mutex);
- for (size_t i = 0; i < queued_add_clients.size(); ++i) {
- add_client(queued_add_clients[i]);
+ for (size_t i = 0; i < queued_add_clients.size(); ++i) {
+ add_client(queued_add_clients[i]);
+ }
+ queued_add_clients.clear();
}
- queued_add_clients.clear();
for (size_t i = 0; i < streams.size(); ++i) {
streams[i]->process_queued_data();
void set_encoding(int stream_index, Stream::Encoding encoding);
private:
- // Mutex protecting queued_add_clients and streams[..]->queued_data.
+ // Mutex protecting queued_add_clients.
// Note that if you want to hold both this and <mutex> below,
// you will need to take <mutex> before this one.
- mutable pthread_mutex_t queued_data_mutex;
+ mutable pthread_mutex_t queued_clients_mutex;
// Deferred commands that should be run from the do_work() thread as soon as possible.
// We defer these for two reasons:
// add_data(), since they want to do add_data() rather often, and <mutex>
// can be taken a lot of the time.
//
- // Protected by <queued_data_mutex>.
+ // Protected by <queued_clients_mutex>.
std::vector<int> queued_add_clients;
// All variables below this line are protected by the mutex.
#include "log.h"
#include "metacube2.h"
+#include "mutexlock.h"
#include "state.pb.h"
#include "stream.h"
#include "util.h"
if (data_fd == -1) {
exit(1);
}
+
+ pthread_mutex_init(&queued_data_mutex, NULL);
}
Stream::~Stream()
} else {
last_suitable_starting_point = bytes_received;
}
+
+ pthread_mutex_init(&queued_data_mutex, NULL);
}
StreamProto Stream::serialize()
void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
{
+ MutexLock lock(&queued_data_mutex);
assert(suitable_for_stream_start == SUITABLE_FOR_STREAM_START ||
suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START);
if (suitable_for_stream_start == SUITABLE_FOR_STREAM_START) {
void Stream::process_queued_data()
{
- if (queued_data.empty()) {
- return;
+ std::vector<iovec> queued_data_copy;
+ int queued_data_last_starting_point_copy = -1;
+
+ // Hold the lock for as short as possible, since add_data_raw() can possibly
+ // write to disk, which might disturb the input thread.
+ {
+ MutexLock lock(&queued_data_mutex);
+ if (queued_data.empty()) {
+ return;
+ }
+
+ swap(queued_data, queued_data_copy);
+ swap(queued_data_last_starting_point, queued_data_last_starting_point_copy);
}
// Update the last suitable starting point for the stream,
// if the queued data contains such a starting point.
- assert(queued_data_last_starting_point < ssize_t(queued_data.size()));
- if (queued_data_last_starting_point >= 0) {
+ assert(queued_data_last_starting_point_copy < ssize_t(queued_data_copy.size()));
+ if (queued_data_last_starting_point_copy >= 0) {
last_suitable_starting_point = bytes_received;
- for (int i = 0; i < queued_data_last_starting_point; ++i) {
- last_suitable_starting_point += queued_data[i].iov_len;
+ for (int i = 0; i < queued_data_last_starting_point_copy; ++i) {
+ last_suitable_starting_point += queued_data_copy[i].iov_len;
}
}
- add_data_raw(queued_data);
- for (size_t i = 0; i < queued_data.size(); ++i) {
- char *data = reinterpret_cast<char *>(queued_data[i].iov_base);
+ add_data_raw(queued_data_copy);
+ for (size_t i = 0; i < queued_data_copy.size(); ++i) {
+ char *data = reinterpret_cast<char *>(queued_data_copy[i].iov_base);
delete[] data;
}
- queued_data.clear();
- queued_data_last_starting_point = -1;
// We have more data, so wake up all clients.
if (to_process.empty()) {
// Changes the backlog size, restructuring the data as needed.
void set_backlog_size(size_t new_size);
+ // Mutex protecting <queued_data> and <queued_data_last_starting_point>.
+ // Note that if you want to hold both this and the owning server's
+ // <mutex> you will need to take <mutex> before this one.
+ mutable pthread_mutex_t queued_data_mutex;
+
std::string url;
// The HTTP response header, without the trailing double newline.
// What pool to fetch marks from, or NULL.
MarkPool *mark_pool;
- // Queued data, if any. Protected by the owning Server's <queued_data_mutex>.
+ // Queued data, if any. Protected by <queued_data_mutex>.
// The data pointers in the iovec are owned by us.
std::vector<iovec> queued_data;
// Index of the last element in queued_data that is suitable to start streaming at.
- // -1 if none.
+ // -1 if none. Protected by <queued_data_mutex>.
int queued_data_last_starting_point;
// Put client to sleep, since there is no more data for it; we will on
// Add more data to <queued_data>, adding Metacube headers if needed.
// Does not take ownership of <data>.
- // You should hold the owning Server's <queued_data_mutex>.
void add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
// Add queued data to the stream, if any.
- // You should hold the owning Server's <mutex> _and_ <queued_data_mutex>.
+ // You should hold the owning Server's <mutex>.
void process_queued_data();
private: