diff options
author | David Robillard <d@drobilla.net> | 2006-10-04 21:45:20 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2006-10-04 21:45:20 +0000 |
commit | 30cb85c307b4c1273791721a782337742ade222c (patch) | |
tree | 020932d6f50ebbcd813c58f6f23e85a293587665 /src/common/util | |
parent | 32261ba465be203f973a0e126672b8d7188ba327 (diff) | |
download | ingen-30cb85c307b4c1273791721a782337742ade222c.tar.gz ingen-30cb85c307b4c1273791721a782337742ade222c.tar.bz2 ingen-30cb85c307b4c1273791721a782337742ade222c.zip |
Moved generic utility stuff to new library "raul".
git-svn-id: http://svn.drobilla.net/lad/ingen@156 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/common/util')
-rw-r--r-- | src/common/util/Atom.h | 127 | ||||
-rw-r--r-- | src/common/util/Condition.h | 42 | ||||
-rw-r--r-- | src/common/util/CountedPtr.h | 58 | ||||
-rw-r--r-- | src/common/util/LibloAtom.h | 73 | ||||
-rw-r--r-- | src/common/util/Makefile.am | 12 | ||||
-rw-r--r-- | src/common/util/Mutex.h | 41 | ||||
-rw-r--r-- | src/common/util/Path.h | 251 | ||||
-rw-r--r-- | src/common/util/Queue.h | 160 | ||||
-rw-r--r-- | src/common/util/RedlandAtom.h | 93 | ||||
-rw-r--r-- | src/common/util/Semaphore.h | 63 | ||||
-rw-r--r-- | src/common/util/Slave.h | 57 | ||||
-rw-r--r-- | src/common/util/Thread.h | 100 |
12 files changed, 0 insertions, 1077 deletions
diff --git a/src/common/util/Atom.h b/src/common/util/Atom.h deleted file mode 100644 index 2a6ae982..00000000 --- a/src/common/util/Atom.h +++ /dev/null @@ -1,127 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef ATOM_H -#define ATOM_H - -#include <cstdlib> -#include <cassert> -#include <cstring> -#include <string> - -using std::string; - - -/** An OSC atom (fundamental data types OSC messages are composed of). - */ -class Atom { -public: - enum Type { - NIL, - INT, - FLOAT, - STRING, - BLOB - }; - - Atom() : _type(NIL), _blob_val(0) {} - Atom(int32_t val) : _type(INT), _int_val(val) {} - Atom(float val) : _type(FLOAT), _float_val(val) {} - Atom(const char* val) : _type(STRING), _string_val(strdup(val)) {} - Atom(const string& val) : _type(STRING), _string_val(strdup(val.c_str())) {} - - Atom(void* val) : _type(BLOB), _blob_size(sizeof(val)), _blob_val(malloc(_blob_size)) - { memcpy(_blob_val, val, sizeof(_blob_size)); } - - ~Atom() - { - if (_type == STRING) - free(_string_val); - else if (_type == BLOB) - free(_blob_val); - } - - // Gotta love C++ boilerplate: - - Atom(const Atom& copy) - : _type(copy._type) - , _blob_size(copy._blob_size) - { - switch (_type) { - case NIL: _blob_val = 0; break; - case INT: _int_val = copy._int_val; break; - case FLOAT: _float_val = copy._float_val; break; - case STRING: _string_val = strdup(copy._string_val); break; - - case BLOB: _blob_val = malloc(_blob_size); - memcpy(_blob_val, copy._blob_val, _blob_size); - break; - - default: break; - } - } - - Atom& operator=(const Atom& other) - { - if (_type == BLOB) - free(_blob_val); - else if (_type == STRING) - free(_string_val); - - _type = other._type; - _blob_size = other._blob_size; - - switch (_type) { - case NIL: _blob_val = 0; break; - case INT: _int_val = other._int_val; break; - case FLOAT: _float_val = other._float_val; break; - case STRING: _string_val = strdup(other._string_val); break; - - case BLOB: _blob_val = malloc(_blob_size); - memcpy(_blob_val, other._blob_val, _blob_size); - break; - - default: break; - } - return *this; - } - - /** Type of this atom. Always check this before attempting to get the - * value - attempting to get the incorrectly typed value is a fatal error. - */ - Type type() const { return _type; } - - inline int32_t get_int32() const { assert(_type == INT); return _int_val; } - inline float get_float() const { assert(_type == FLOAT); return _float_val; } - inline const char* get_string() const { assert(_type == STRING); return _string_val; } - inline const void* get_blob() const { assert(_type == BLOB); return _blob_val; } - - inline operator bool() const { return (_type != NIL); } - -private: - Type _type; - - size_t _blob_size; ///< always a multiple of 32 - - union { - int32_t _int_val; - float _float_val; - char* _string_val; - void* _blob_val; - }; -}; - -#endif // ATOM_H diff --git a/src/common/util/Condition.h b/src/common/util/Condition.h deleted file mode 100644 index 1ee28dbd..00000000 --- a/src/common/util/Condition.h +++ /dev/null @@ -1,42 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef CONDITION_H -#define CONDITION_H - -#include <pthread.h> - - -/** Trivial (but pretty) wrapper around POSIX Conditions (zero memory overhead). - * - * A semaphore that isn't a counter and is slow and NOT realtime safe. - */ -class Condition { -public: - inline Condition() { pthread_cond_init(&_cond, NULL); } - - inline ~Condition() { pthread_cond_destroy(&_cond); } - - inline void signal() { pthread_cond_signal(&_cond); } - inline void wait(Mutex& mutex) { pthread_cond_wait(&_cond, &mutex._mutex); } - -private: - pthread_cond_t _cond; -}; - - -#endif // CONDITION_H - diff --git a/src/common/util/CountedPtr.h b/src/common/util/CountedPtr.h deleted file mode 100644 index 63b2bdf4..00000000 --- a/src/common/util/CountedPtr.h +++ /dev/null @@ -1,58 +0,0 @@ -/* A Reference Counting Smart Pointer. - * Copyright (C) 2006 Dave Robillard. - * - * This 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 2 of the License, or (at your option) any later - * version. - * - * This file is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef COUNTED_PTR_H -#define COUNTED_PTR_H - -#include <cassert> -#include <cstddef> - -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS -#include <iostream> -#include <list> -#include <algorithm> - -static std::list<void*> counted_ptr_counters; - -// Use debug hooks to ensure 2 shared_ptrs never point to the same thing -namespace boost { - - inline void sp_scalar_constructor_hook(void* object, unsigned long cnt, void* ptr) { - assert(std::find(counted_ptr_counters.begin(), counted_ptr_counters.end(), - (void*)object) == counted_ptr_counters.end()); - counted_ptr_counters.push_back(object); - //std::cerr << "Creating CountedPtr to " - // << object << ", count = " << cnt << std::endl; - } - - inline void sp_scalar_destructor_hook(void* object, unsigned long cnt, void* ptr) { - counted_ptr_counters.remove(object); - //std::cerr << "Destroying CountedPtr to " - // << object << ", count = " << cnt << std::endl; - } - -} -#endif // BOOST_SP_ENABLE_DEBUG_HOOKS - - -#include <boost/shared_ptr.hpp> - -#define CountedPtr boost::shared_ptr -#define PtrCast boost::dynamic_pointer_cast - -#endif // COUNTED_PTR_H - diff --git a/src/common/util/LibloAtom.h b/src/common/util/LibloAtom.h deleted file mode 100644 index db76a807..00000000 --- a/src/common/util/LibloAtom.h +++ /dev/null @@ -1,73 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef LIBLO_ATOM_H -#define LIBLO_ATOM_H - -#include <lo/lo.h> -#include "util/Atom.h" - - -/** Support for serializing an Atom to/from liblo messages. - * - * (Here to prevent a unnecessary liblo dependency for Atom). - */ -class LibloAtom { -public: - static void lo_message_add_atom(lo_message m, const Atom& atom) { - switch (atom.type()) { - //case NIL: - // (see below) - //break; - case Atom::INT: - lo_message_add_int32(m, atom.get_int32()); - break; - case Atom::FLOAT: - lo_message_add_float(m, atom.get_float()); - break; - case Atom::STRING: - lo_message_add_string(m, atom.get_string()); - break; - case Atom::BLOB: - // FIXME: is this okay? what does liblo do? - lo_message_add_blob(m, const_cast<void*>(atom.get_blob())); - break; - default: // This catches Atom::Type::NIL too - lo_message_add_nil(m); - break; - } - } - - static Atom lo_arg_to_atom(char type, lo_arg* arg) { - switch (type) { - case 'i': - return Atom(arg->i); - case 'f': - return Atom(arg->f); - case 's': - return Atom(&arg->s); - //case 'b' - // FIXME: How to get a blob from a lo_arg? - //return Atom(arg->b); - default: - return Atom(); - } - } - -}; - - -#endif // LIBLO_ATOM_H diff --git a/src/common/util/Makefile.am b/src/common/util/Makefile.am deleted file mode 100644 index 6bddb707..00000000 --- a/src/common/util/Makefile.am +++ /dev/null @@ -1,12 +0,0 @@ -EXTRA_DIST = \ - CountedPtr.h \ - Path.h \ - Queue.h \ - Semaphore.h \ - Mutex.h \ - Condition.h \ - Thread.h \ - Slave.h \ - Atom.h \ - LibloAtom.h \ - RedlandAtom.h diff --git a/src/common/util/Mutex.h b/src/common/util/Mutex.h deleted file mode 100644 index c5aaf3ae..00000000 --- a/src/common/util/Mutex.h +++ /dev/null @@ -1,41 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef MUTEX_H -#define MUTEX_H - -#include <pthread.h> - - -/** Trivial (but pretty) wrapper around POSIX Mutexes (zero memory overhead). - */ -class Mutex { -public: - inline Mutex() { pthread_mutex_init(&_mutex, NULL); } - - inline ~Mutex() { pthread_mutex_destroy(&_mutex); } - - inline bool try_lock() { return (pthread_mutex_trylock(&_mutex) == 0); } - inline void lock() { pthread_mutex_lock(&_mutex); } - inline void unlock() { pthread_mutex_unlock(&_mutex); } - -private: - friend class Condition; - pthread_mutex_t _mutex; -}; - - -#endif // MUTEX_H diff --git a/src/common/util/Path.h b/src/common/util/Path.h deleted file mode 100644 index bcb047ad..00000000 --- a/src/common/util/Path.h +++ /dev/null @@ -1,251 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef PATH_H -#define PATH_H - -#include <cctype> -#include <string> -#include <cassert> -using std::string; - - -/** Simple wrapper around standard string with useful path-specific methods. - * - * This enforces that a Path is a valid OSC path (though it is used for - * GraphObject paths, which aren't directly OSC paths but a portion of one). - * - * A path is divided by slashes (/). The first character MUST be a slash, and - * the last character MUST NOT be a slash (except in the special case of the - * root path "/", which is the only valid single-character path). - * - * Valid characters are the 95 printable ASCII characters (32-126), excluding: - * space # * , ? [ ] { } - */ -class Path : public std::basic_string<char> { -public: - - /** Construct a Path from an std::string. - * - * It is a fatal error to construct a Path from an invalid string, - * use @ref is_valid first to check. - */ - Path(const std::basic_string<char>& path) - : std::basic_string<char>(path) - { - assert(is_valid(path)); - } - - - /** Construct a Path from a C string. - * - * It is a fatal error to construct a Path from an invalid string, - * use @ref is_valid first to check. - */ - Path(const char* cpath) - : std::basic_string<char>(cpath) - { - assert(is_valid(cpath)); - } - - - static bool is_valid(const std::basic_string<char>& path) - { - if (path.length() == 0) - return false; - - // Must start with a / - if (path.at(0) != '/') - return false; - - // Must not end with a slash unless "/" - if (path.length() > 1 && path.at(path.length()-1) == '/') - return false; - - assert(path.find_last_of("/") != string::npos); - - // Double slash not allowed - if (path.find("//") != string::npos) - return false; - - // All characters must be printable ASCII - for (size_t i=0; i < path.length(); ++i) - if (path.at(i) < 32 || path.at(i) > 126) - return false; - - // Disallowed characters - if ( path.find(" ") != string::npos - || path.find("#") != string::npos - || path.find("*") != string::npos - || path.find(",") != string::npos - || path.find("?") != string::npos - || path.find("[") != string::npos - || path.find("]") != string::npos - || path.find("{") != string::npos - || path.find("}") != string::npos) - return false; - - return true; - } - - static bool is_valid_name(const std::basic_string<char>& path) - { - return is_valid(string("/").append(path)); - } - - - /** Convert a string to a valid full path. - * - * This will make a best effort at turning @a str into a complete, valid - * Path, and will always return one. - */ - static string pathify(const std::basic_string<char>& str) - { - string path = str; - - if (path.length() == 0) - return "/"; // this might not be wise - - // Must start with a / - if (path.at(0) != '/') - path = string("/").append(path); - - // Must not end with a slash unless "/" - if (path.length() > 1 && path.at(path.length()-1) == '/') - path = path.substr(0, path.length()-1); // chop trailing slash - - assert(path.find_last_of("/") != string::npos); - - replace_invalid_chars(path, false); - - assert(is_valid(path)); - - return path; - } - - /** Convert a string to a valid name (or "method" - tokens between slashes) - * - * This will strip all slashes, etc, and always return a valid name/method. - */ - static string nameify(const std::basic_string<char>& str) - { - string name = str; - - if (name.length() == 0) - return "."; // this might not be wise - - replace_invalid_chars(name, true); - - assert(is_valid(string("/") + name)); - - return name; - } - - - /** Replace any invalid characters in @a str with a suitable replacement. - * - * Makes a pretty name - underscores are a valid character, but this chops - * both spaces and underscores, uppercasing the next letter, to create - * uniform CamelCase names that look nice - */ - static void replace_invalid_chars(string& str, bool replace_slash = false) - { - for (size_t i=0; i < str.length(); ++i) { - if (str[i] == ' ' || str[i] == '_') { - str[i+1] = std::toupper(str[i+1]); // capitalize next char - str = str.substr(0, i) + str.substr(i+1); // chop space/underscore - } else if (str[i] == '[' || str[i] == '{') { - str[i] = '('; - } else if (str[i] == ']' || str[i] == '}') { - str[i] = ')'; - } else if (str[i] < 32 || str.at(i) > 126 - || str[i] == '#' - || str[i] == '*' - || str[i] == ',' - || str[i] == '?' - || (replace_slash && str[i] == '/')) { - str[i] = '.'; - } - } - - // Chop brackets - while (true) { - - const string::size_type open = str.find("("); - const string::size_type close = str.find(")"); - - if (open != string::npos) { - if (close != string::npos) - str.erase(open, (close - open) + 1); - } else { - break; - } - - } - } - - - /** Return the name of this object (everything after the last '/'). - * This is the "method name" for OSC paths. - */ - inline std::basic_string<char> name() const - { - if ((*this) == "/") - return ""; - else - return substr(find_last_of("/")+1); - } - - - /** Return the parent's path. - * - * Calling this on the path "/" will return "/". - * This is the (deepest) "container path" for OSC paths. - */ - inline Path parent() const - { - std::basic_string<char> parent = substr(0, find_last_of("/")); - return (parent == "") ? "/" : parent; - } - - /** Parent path with a "/" appended. - * - * This exists to avoid needing to be careful about the special case of "/". - * To create a child of a path, use parent.base() + child_name. - * Returned value is always a valid path, with the single exception that - * the last character is "/". - */ - inline string base() const - { - if ((*this) == "/") - return *this; - else - return (*this) + "/"; - } - - inline bool is_child_of(const Path& parent) const - { - return (length() > parent.length() && substr(0, parent.length()) == parent); - } - - inline bool is_parent_of(const Path& child) const - { - return child.is_child_of(*this); - } -}; - - -#endif // PATH_H diff --git a/src/common/util/Queue.h b/src/common/util/Queue.h deleted file mode 100644 index a4c34222..00000000 --- a/src/common/util/Queue.h +++ /dev/null @@ -1,160 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef QUEUE_H -#define QUEUE_H - -#include <cassert> -#include <cstdlib> - - -/** Realtime-safe single-reader single-writer queue (aka lock-free ringbuffer) - * - * Implemented as a dequeue in a fixed array. This is read/write thread-safe, - * pushing and popping may occur simultaneously by seperate threads, but - * the push and pop operations themselves are not thread-safe. - * - * FIXME: Verify atomicity of everything here. - */ -template <typename T> -class Queue -{ -public: - Queue(size_t size); - ~Queue(); - - inline bool is_empty() const; - inline bool is_full() const; - - inline size_t capacity() const { return m_size-1; } - inline size_t fill() const; - - inline T& front() const; - - inline bool push(T obj); - inline T& pop(); - -private: - // Prevent copies (these are undefined) - Queue(const Queue& copy); - Queue& operator=(const Queue& copy); - - volatile size_t m_front; ///< Index to front of queue (circular) - volatile size_t m_back; ///< Index to back of queue (one past last element) (circular) - const size_t m_size; ///< Size of @ref m_objects (you can store m_size-1 objects) - T* const m_objects; ///< Fixed array containing queued elements -}; - - -template<typename T> -Queue<T>::Queue(size_t size) -: m_front(0), - m_back(0), - m_size(size+1), - m_objects((T*)calloc(m_size, sizeof(T))) -{ - assert(size > 1); -} - - -template <typename T> -Queue<T>::~Queue() -{ - free(m_objects); -} - - -/** Return whether or not the queue is empty. - */ -template <typename T> -inline bool -Queue<T>::is_empty() const -{ - return (m_back == m_front); -} - - -/** Return whether or not the queue is full. - */ -template <typename T> -inline bool -Queue<T>::is_full() const -{ - // FIXME: This can probably be faster - return (fill() == capacity()); -} - - -/** Returns how many elements are currently in the queue. - */ -template <typename T> -inline size_t -Queue<T>::fill() const -{ - return (m_back + m_size - m_front) % m_size; -} - - -/** Return the element at the front of the queue without removing it - */ -template <typename T> -inline T& -Queue<T>::front() const -{ - return m_objects[m_front]; -} - - -/** Push an item onto the back of the Queue - realtime-safe, not thread-safe. - * - * @returns true if @a elem was successfully pushed onto the queue, - * false otherwise (queue is full). - */ -template <typename T> -inline bool -Queue<T>::push(T elem) -{ - if (is_full()) { - return false; - } else { - m_objects[m_back] = elem; - m_back = (m_back + 1) % (m_size); - return true; - } -} - - -/** Pop an item off the front of the queue - realtime-safe, not thread-safe. - * - * It is a fatal error to call pop() when the queue is empty. - * - * @returns the element popped. - */ -template <typename T> -inline T& -Queue<T>::pop() -{ - assert(!is_empty()); - assert(m_size > 0); - - T& r = m_objects[m_front]; - m_front = (m_front + 1) % (m_size); - - return r; -} - - -#endif // QUEUE_H diff --git a/src/common/util/RedlandAtom.h b/src/common/util/RedlandAtom.h deleted file mode 100644 index 8d2456a5..00000000 --- a/src/common/util/RedlandAtom.h +++ /dev/null @@ -1,93 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef REDLAND_ATOM_H -#define REDLAND_ATOM_H - -#include <sstream> -#include <cstring> -#include <raptor.h> -#include "util/Atom.h" - -#define U(x) ((const unsigned char*)(x)) - -/** Support for serializing an Atom to/from RDF (via redland aka librdf). - * - * (Here to prevent a unnecessary redland dependency for Atom). - */ -class RedlandAtom { -public: - /** Set this atom's value to the object (value) portion of an RDF triple. - * - * Caller is responsible for calling free(triple->object). - */ - static void atom_to_triple_object(raptor_statement* triple, const Atom& atom) { - std::ostringstream os; - - triple->object_literal_language = NULL; - - switch (atom.type()) { - case Atom::INT: - os << atom.get_int32(); - triple->object = (unsigned char*)strdup(os.str().c_str()); - triple->object_type = RAPTOR_IDENTIFIER_TYPE_LITERAL; - triple->object_literal_datatype = raptor_new_uri( - U("http://www.w3.org/2001/XMLSchema#integer")); - break; - case Atom::FLOAT: - os << atom.get_float(); - triple->object = (unsigned char*)strdup(os.str().c_str()); - triple->object_type = RAPTOR_IDENTIFIER_TYPE_LITERAL; - triple->object_literal_datatype = raptor_new_uri( - U("http://www.w3.org/2001/XMLSchema#float")); - break; - case Atom::STRING: - triple->object = strdup(atom.get_string()); - triple->object_type = RAPTOR_IDENTIFIER_TYPE_LITERAL; - break; - case Atom::BLOB: - case Atom::NIL: - default: - cerr << "WARNING: Unserializable Atom!" << endl; - triple->object = NULL; - triple->object_type = RAPTOR_IDENTIFIER_TYPE_UNKNOWN; - } - } - -#if 0 - static Atom node_to_atom(librdf_node* node) { - /*switch (type) { - case 'i': - return Atom(arg->i); - case 'f': - return Atom(arg->f); - case 's': - return Atom(&arg->s); - //case 'b' - // FIXME: How to get a blob from a lo_arg? - //return Atom(arg->b); - default: - return Atom(); - }*/ - cerr << "FIXME: node_to_atom\n"; - return Atom(); - } -#endif - -}; - - -#endif // REDLAND_ATOM_H diff --git a/src/common/util/Semaphore.h b/src/common/util/Semaphore.h deleted file mode 100644 index 168a7059..00000000 --- a/src/common/util/Semaphore.h +++ /dev/null @@ -1,63 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef SEMAPHORE_H -#define SEMAPHORE_H - -#include <semaphore.h> - - -/** Trivial wrapper around POSIX semaphores (zero memory overhead). - * - * This was created to provide an alternative debuggable implementation of - * semaphores based on a cond/mutex pair because semaphore's appeared not to - * work in GDB. Turns out sem_wait can fail when run in GDB, and Debian - * really needs to update it's man pages. - * - * This class remains as a trivial (yet pretty) wrapper/abstraction. - */ -class Semaphore { -public: - inline Semaphore(unsigned int initial) { sem_init(&m_sem, 0, initial); } - - inline ~Semaphore() { sem_destroy(&m_sem); } - - /** Increment (and signal any waiters). - * - * Realtime safe. - */ - inline void post() { sem_post(&m_sem); } - - /** Wait until count is > 0, then decrement. - * - * Note that sem_wait always returns 0 in practise. It returns nonzero - * when run in GDB, so the while is necessary to allow debugging. - * - * Obviously not realtime safe. - */ - inline void wait() { while (sem_wait(&m_sem) != 0) ; } - - /** Non-blocking version of wait(). - * - * Realtime safe? - */ - inline int try_wait() { return sem_trywait(&m_sem); } -private: - sem_t m_sem; -}; - - -#endif // SEMAPHORE_H diff --git a/src/common/util/Slave.h b/src/common/util/Slave.h deleted file mode 100644 index 0d0976b8..00000000 --- a/src/common/util/Slave.h +++ /dev/null @@ -1,57 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef SLAVE_H -#define SLAVE_H - -#include <pthread.h> -#include "util/Semaphore.h" -#include "util/Thread.h" - - -/** Thread driven by (realtime safe) signals. - * - * \ingroup engine - */ -class Slave : public Thread -{ -public: - Slave() : _whip(0) {} - - /** Tell the slave to do whatever work it does. Realtime safe. */ - inline void whip() { _whip.post(); } - -protected: - virtual void _whipped() = 0; - - Semaphore _whip; - -private: - // Prevent copies (undefined) - Slave(const Slave&); - Slave& operator=(const Slave&); - - inline void _run() - { - while (true) { - _whip.wait(); - _whipped(); - } - } -}; - - -#endif // SLAVE_H diff --git a/src/common/util/Thread.h b/src/common/util/Thread.h deleted file mode 100644 index c139434b..00000000 --- a/src/common/util/Thread.h +++ /dev/null @@ -1,100 +0,0 @@ -/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. - * - * Ingen 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 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef THREAD_H -#define THREAD_H - -#include <string> -#include <iostream> -#include <pthread.h> - - -/** Abstract base class for a thread. - * - * Extend this and override the _run method to easily create a thread - * to perform some task. - * - * \ingroup engine - */ -class Thread -{ -public: - Thread() : _pthread_exists(false) {} - - virtual ~Thread() { stop(); } - - void set_name(const std::string& name) { _name = name; } - - virtual void start() { - std::cout << "[" << _name << " Thread] Starting." << std::endl; - - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 1500000); - - pthread_create(&_pthread, &attr, _static_run, this); - _pthread_exists = true; - } - - virtual void stop() { - if (_pthread_exists) { - pthread_cancel(_pthread); - pthread_join(_pthread, NULL); - _pthread_exists = false; - } - } - - void set_scheduling(int policy, unsigned int priority) { - sched_param sp; - sp.sched_priority = priority; - int result = pthread_setschedparam(_pthread, SCHED_FIFO, &sp); - if (!result) { - std::cout << "[" << _name << "] Set scheduling policy to "; - switch (policy) { - case SCHED_FIFO: std::cout << "SCHED_FIFO"; break; - case SCHED_RR: std::cout << "SCHED_RR"; break; - case SCHED_OTHER: std::cout << "SCHED_OTHER"; break; - default: std::cout << "UNKNOWN"; break; - } - std::cout << ", priority " << sp.sched_priority << std::endl; - } else { - std::cout << "[" << _name << "] Unable to set scheduling policy (" - << strerror(result) << ")" << std::endl; - } - } - - -protected: - virtual void _run() = 0; - -private: - // Prevent copies (undefined) - Thread(const Thread&); - Thread& operator=(const Thread&); - - inline static void* _static_run(void* me) { - Thread* myself = (Thread*)me; - myself->_run(); - return NULL; // and I - } - - std::string _name; - bool _pthread_exists; - pthread_t _pthread; -}; - - -#endif // THREAD_H |