Skip to content

Commit

Permalink
o libboost caused too many troubles in embedded systems with weak
Browse files Browse the repository at this point in the history
  library support. Since we anyway only use mutexes, let's write our
  own little posix-mutex wrapper around that.
  • Loading branch information
hzeller committed Sep 29, 2012
1 parent b76ff19 commit b4eec5c
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 63 deletions.
2 changes: 1 addition & 1 deletion INSTALL.md
Expand Up @@ -4,7 +4,7 @@ On a reasonably fresh system (e.g. Ubuntu 11.10 and 12.04), installation is
straightforward. To compile, this is what you need to do:

sudo apt-get install libsndfile-dev libflac-dev libzita-convolver-dev \
libfuse-dev libboost-thread-dev libmicrohttpd-dev
libfuse-dev libmicrohttpd-dev
make

To install in the default location /usr/local/bin, just do
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -6,7 +6,7 @@ F_VERSION=$(shell git log -n1 --date=short --format="%cd (commit=%h)" 2>/dev/nul
CFLAGS=-D_FILE_OFFSET_BITS=64 -Wall -O2 -DFOLVE_VERSION='"$(F_VERSION)"'

CXXFLAGS=$(CFLAGS)
LDFLAGS=-lfuse -lsndfile -lzita-convolver -lmicrohttpd -lboost_thread-mt -lfftw3f
LDFLAGS=-lfuse -lsndfile -lzita-convolver -lmicrohttpd -lfftw3f

ifdef LINK_STATIC
# static linking requires us to be much more explicit when linking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -70,7 +70,7 @@ This project is notably based on
This requires the latest versions of some development libraries.

sudo apt-get install libsndfile-dev libflac-dev libzita-convolver-dev \
libfuse-dev libboost-thread-dev libmicrohttpd-dev
libfuse-dev libmicrohttpd-dev
make
sudo make install

Expand Down
18 changes: 8 additions & 10 deletions conversion-buffer.cc
@@ -1,7 +1,7 @@
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
Expand All @@ -20,13 +20,12 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <boost/thread/locks.hpp>

// Annoyingly, mkstemp() does not do TMPDIR trickery and tempnam() is obsolete.
static char *TempNameAllocated(const char *pattern) {
const char *tmp_path = getenv("TMPDIR");
Expand Down Expand Up @@ -140,15 +139,14 @@ ssize_t ConversionBuffer::Read(char *buf, size_t size, off_t offset) {
const off_t required_min_written = offset + (offset >= header_end_ ? size : 1);

// As soon as someone tries to read beyond of what we already have, we call
// our WriteToSoundfile() callback that fills more of it.
// the callback that fills more of it.
// We are shared between potentially several open files. Serialize threads.
{
boost::lock_guard<boost::mutex> l(mutex_);
while (total_written_ < required_min_written) {
if (!source_->AddMoreSoundData())
break;
}
mutex_.Lock();
while (total_written_ < required_min_written) {
if (!source_->AddMoreSoundData())
break;
}
mutex_.Unlock();

return pread(out_filedes_, buf, size, offset);
}
10 changes: 5 additions & 5 deletions conversion-buffer.h
Expand Up @@ -2,7 +2,7 @@
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
Expand All @@ -20,7 +20,7 @@
#define FOLVE_CONVERSION_BUFFER_H

#include <sndfile.h>
#include <boost/thread/mutex.hpp>
#include "util.h"

// A file-backed buffer for a SNDFILE, that is only filled on demand via
// a SoundSource.
Expand All @@ -38,11 +38,11 @@ class ConversionBuffer {
// requested. There can be an error in opening the sound-file, in that
// case SetOutputSoundfile() will be called with NULL.
// Ask sf_strerror() to find out why.
// Ownership is passed to the SoundSource, receiver needs to
// Ownership is passed to the SoundSource, receiver needs to
// sf_close() the file.
virtual void SetOutputSoundfile(ConversionBuffer *parent,
SNDFILE *sndfile) = 0;

// This callback is called by the ConversionBuffer if it needs more data.
// Rerturns 'true' if there is more, 'false' if that was the last available
// data.
Expand Down Expand Up @@ -102,7 +102,7 @@ class ConversionBuffer {
bool snd_writing_enabled_;
off_t total_written_;
off_t header_end_;
boost::mutex mutex_;
folve::Mutex mutex_;
};

#endif // FOLVE_CONVERSION_BUFFER_H
13 changes: 6 additions & 7 deletions file-handler-cache.cc
Expand Up @@ -20,8 +20,7 @@

#include <map>
#include <vector>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <algorithm>

#include "file-handler.h"
#include "file-handler-cache.h"
Expand All @@ -36,7 +35,7 @@ struct FileHandlerCache::Entry {

FileHandler *FileHandlerCache::InsertPinned(const std::string &key,
FileHandler *handler) {
boost::lock_guard<boost::mutex> l(mutex_);
folve::MutexLock l(&mutex_);
CacheMap::iterator ins
= cache_.insert(std::make_pair(key, (Entry*)NULL)).first;
if (ins->second == NULL) {
Expand All @@ -54,7 +53,7 @@ FileHandler *FileHandlerCache::InsertPinned(const std::string &key,
}

FileHandler *FileHandlerCache::FindAndPin(const std::string &key) {
boost::lock_guard<boost::mutex> l(mutex_);
folve::MutexLock l(&mutex_);
CacheMap::iterator found = cache_.find(key);
if (found == cache_.end())
return NULL;
Expand All @@ -64,7 +63,7 @@ FileHandler *FileHandlerCache::FindAndPin(const std::string &key) {
}

void FileHandlerCache::Unpin(const std::string &key) {
boost::lock_guard<boost::mutex> l(mutex_);
folve::MutexLock l(&mutex_);
CacheMap::iterator found = cache_.find(key);
assert(found != cache_.end());
--found->second->references;
Expand All @@ -81,7 +80,7 @@ void FileHandlerCache::SetObserver(Observer *observer) {

void FileHandlerCache::GetStats(std::vector<HandlerStats> *stats) {
HandlerStats s;
boost::lock_guard<boost::mutex> l(mutex_);
folve::MutexLock l(&mutex_);
for (CacheMap::iterator it = cache_.begin(); it != cache_.end(); ++it) {
it->second->handler->GetHandlerStatus(&s);
s.status = ((it->second->references == 0)
Expand Down Expand Up @@ -123,4 +122,4 @@ void FileHandlerCache::CleanupOldestUnreferenced_Locked() {
Erase_Locked(for_removal[i]);
}
}

5 changes: 2 additions & 3 deletions file-handler-cache.h
Expand Up @@ -25,9 +25,8 @@
#include <string>
#include <vector>

#include <boost/thread/mutex.hpp>

#include "file-handler.h"
#include "util.h"

class FileHandler;

Expand Down Expand Up @@ -84,7 +83,7 @@ class FileHandlerCache {

const size_t max_size_;
Observer *observer_;
boost::mutex mutex_;
folve::Mutex mutex_;
CacheMap cache_;
};

Expand Down
11 changes: 0 additions & 11 deletions file-handler.h
Expand Up @@ -31,17 +31,6 @@ class HandlerStats {
: duration_seconds(-1), progress(-1), status(OPEN), last_access(0),
max_output_value(0), in_gapless(false), out_gapless(false) {}

// Copy constructor: test to work around some gcc 2.7.1 seen in the field.
// To be removed after test.
HandlerStats(const HandlerStats &other)
: filename(other.filename), format(other.format), message(other.message),
duration_seconds(other.duration_seconds), progress(other.progress),
status(other.status), last_access(other.last_access),
max_output_value(other.max_output_value),
in_gapless(other.in_gapless), out_gapless(other.out_gapless),
filter_id(other.filter_id) {
}

std::string filename; // filesystem name.
std::string format; // File format info if recognized.
std::string message; // Per file (error) message if any.
Expand Down
19 changes: 8 additions & 11 deletions folve-filesystem.cc
Expand Up @@ -35,9 +35,6 @@
#include <string>
#include <zita-convolver.h>

#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>

#include "conversion-buffer.h"
#include "file-handler-cache.h"
#include "file-handler.h"
Expand Down Expand Up @@ -204,7 +201,7 @@ class SndFileHandler :
}

virtual void GetHandlerStatus(HandlerStats *stats) {
boost::lock_guard<boost::mutex> l(stats_mutex_);
folve::MutexLock l(&stats_mutex_);
if (processor_ != NULL) {
base_stats_.max_output_value = processor_->max_output_value();
}
Expand Down Expand Up @@ -407,9 +404,9 @@ class SndFileHandler :
Close();
return false;
}
stats_mutex_.lock();
stats_mutex_.Lock();
input_frames_left_ -= r;
stats_mutex_.unlock();
stats_mutex_.Unlock();
if (!input_frames_left_ && !processor_->is_input_buffer_complete()
&& fs_->gapless_processing()) {
typedef std::set<std::string> DirSet;
Expand All @@ -427,19 +424,19 @@ class SndFileHandler :
DLogf("Gapless pass-on from '%s' to alphabetically next '%s'",
base_stats_.filename.c_str(), found->c_str());
}
stats_mutex_.lock();
stats_mutex_.Lock();
processor_->WriteProcessed(snd_out_, r);
stats_mutex_.unlock();
stats_mutex_.Unlock();
if (passed_processor) {
base_stats_.out_gapless = true;
SaveOutputValues();
processor_ = NULL; // we handed over ownership.
}
if (next_file) fs_->Close(found->c_str(), next_file);
} else {
stats_mutex_.lock();
stats_mutex_.Lock();
processor_->WriteProcessed(snd_out_, r);
stats_mutex_.unlock();
stats_mutex_.Unlock();
}
if (input_frames_left_ == 0) {
Close();
Expand Down Expand Up @@ -560,7 +557,7 @@ class SndFileHandler :
const SF_INFO in_info_;
const std::string config_path_;

boost::mutex stats_mutex_;
folve::Mutex stats_mutex_;
HandlerStats base_stats_; // UI information about current file.

struct stat file_stat_; // we dynamically report a changing size.
Expand Down
9 changes: 4 additions & 5 deletions sound-processor.cc
@@ -1,7 +1,7 @@
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
Expand All @@ -20,13 +20,12 @@
#include <assert.h>
#include <string.h>

#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include "util.h"

// There seems to be a bug somewhere inside the fftwf library or the use
// within Convproc::configure()
// It creates a double-delete somewhere if accessed with multiple threads.
static boost::mutex fftw_mutex;
static folve::Mutex fftw_mutex;

SoundProcessor *SoundProcessor::Create(const std::string &config_file,
int samplerate, int channels) {
Expand All @@ -37,7 +36,7 @@ SoundProcessor *SoundProcessor::Create(const std::string &config_file,
zita.nout = channels;
zita.convproc = new Convproc();
{ // fftw threading bug workaround, see above.
boost::lock_guard<boost::mutex> l(fftw_mutex);
folve::MutexLock l(&fftw_mutex);
if ((config(&zita, config_file.c_str()) != 0)
|| zita.convproc->inpdata(channels - 1) == NULL
|| zita.convproc->outdata(channels - 1) == NULL) {
Expand Down
6 changes: 3 additions & 3 deletions status-server.cc
Expand Up @@ -30,7 +30,7 @@
#include <stdarg.h>
#include <microhttpd.h>

#include <boost/thread/locks.hpp>
#include <algorithm>

#include "folve-filesystem.h"
#include "status-server.h"
Expand Down Expand Up @@ -178,7 +178,7 @@ void StatusServer::RetireHandlerEvent(FileHandler *handler) {
}
stats.last_access = folve::CurrentTime();
stats.status = HandlerStats::RETIRED;
boost::lock_guard<boost::mutex> l(retired_mutex_);
folve::MutexLock l(&retired_mutex_);
retired_.push_front(stats);
while (retired_.size() > kMaxRetired) {
++expunged_retired_;
Expand Down Expand Up @@ -402,7 +402,7 @@ const std::string &StatusServer::CreatePage() {
if (retired_.size() > 0) {
content_.append("<h3>Retired</h3>\n");
content_.append("<table>\n");
boost::lock_guard<boost::mutex> l(retired_mutex_);
folve::MutexLock l(&retired_mutex_);
for (RetiredList::const_iterator it = retired_.begin();
it != retired_.end(); ++it) {
AppendFileInfo(kRetiredProgress, *it);
Expand Down
9 changes: 4 additions & 5 deletions status-server.h
Expand Up @@ -2,7 +2,7 @@
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
Expand All @@ -24,8 +24,7 @@

#include "file-handler-cache.h"
#include "file-handler.h"

#include <boost/thread/mutex.hpp>
#include "util.h"

class FolveFilesystem;
struct MHD_Daemon;
Expand Down Expand Up @@ -67,11 +66,11 @@ class StatusServer : protected FileHandlerCache::Observer {
// -- interface FileHandlerCache::Observer
virtual void InsertHandlerEvent(FileHandler *handler) {}
virtual void RetireHandlerEvent(FileHandler *handler);

typedef std::deque<HandlerStats> RetiredList;
RetiredList retired_;
int expunged_retired_;
boost::mutex retired_mutex_;
folve::Mutex retired_mutex_;

// Config directories with common prefix removed to have them concise.
std::vector<std::string> ui_config_directories_;
Expand Down

0 comments on commit b4eec5c

Please sign in to comment.