Revert to simgears thread support.

For all source directories outside of simgear/scene,
which really depends very hard on osg, avoid using osg
classes. This should reenable the use of some basic and
scenegraph independent parts of simgear without the
the need for osg.
This commit is contained in:
Mathias Froehlich
2011-09-01 21:00:48 +02:00
parent 059db643bc
commit 08003ceba9
7 changed files with 117 additions and 114 deletions

View File

@@ -2,7 +2,7 @@
include (SimGearComponent)
set(HEADERS
set(HEADERS
iochannel.hxx
lowlevel.hxx
raw_socket.hxx
@@ -18,7 +18,7 @@ set(HEADERS
HTTPRequest.hxx
)
set(SOURCES
set(SOURCES
iochannel.cxx
lowlevel.cxx
raw_socket.cxx
@@ -37,19 +37,20 @@ set(SOURCES
simgear_component(io io "${SOURCES}" "${HEADERS}")
add_executable(test_sock socktest.cxx)
target_link_libraries(test_sock sgio sgstructure sgdebug ${OPENTHREADS_LIBRARY})
target_link_libraries(test_sock sgio sgstructure sgthreads sgdebug
${CMAKE_THREAD_LIBS_INIT}
${RT_LIBRARY})
add_executable(test_http test_HTTP.cxx)
target_link_libraries(test_http
sgio sgstructure sgtiming sgmisc sgdebug
${RT_LIBRARY}
${OPENTHREADS_LIBRARY})
target_link_libraries(test_http
sgio sgstructure sgthreads sgtiming sgmisc sgdebug
${CMAKE_THREAD_LIBS_INIT}
${RT_LIBRARY})
add_test(http ${EXECUTABLE_OUTPUT_PATH}/test_http)
add_executable(httpget httpget.cxx)
target_link_libraries(httpget
sgio sgstructure sgtiming sgmisc sgdebug
${RT_LIBRARY}
${OPENTHREADS_LIBRARY})
target_link_libraries(httpget
sgio sgstructure sgthreads sgtiming sgmisc sgdebug
${CMAKE_THREAD_LIBS_INIT}
${RT_LIBRARY})

View File

@@ -1,20 +1,20 @@
/*
simgear::Socket, adapted from PLIB Socket by James Turner
Copyright (C) 2010 James Turner
PLIB - A Suite of Portable Game Libraries
Copyright (C) 1998,2002 Steve Baker
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -47,7 +47,7 @@
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/time.h>
# include <sys/time.h>
# include <unistd.h>
# include <netdb.h>
# include <fcntl.h>
@@ -61,40 +61,43 @@
#include <simgear/debug/logstream.hxx>
#include <simgear/structure/exception.hxx>
#include <OpenThreads/Thread>
#include <OpenThreads/Mutex>
#include <OpenThreads/Condition>
#include <simgear/threads/SGThread.hxx>
namespace {
class Resolver : public OpenThreads::Thread
class Resolver : public SGThread
{
public:
static Resolver* instance()
{
if (!static_instance) {
OpenThreads::Thread::Init();
static_instance = new Resolver;
atexit(&Resolver::cleanup);
static_instance->start();
}
return static_instance;
}
static void cleanup()
{
static_instance->cancel();
static_instance->shutdown();
static_instance->join();
}
Resolver()
Resolver() :
_done(false)
{
// take the lock initially, thread will wait upon it once running
_lock.lock();
}
void shutdown()
{
_lock.lock();
_done = true;
_wait.signal();
_lock.unlock();
}
simgear::IPAddress* lookup(const string& host)
{
simgear::IPAddress* result = NULL;
@@ -109,7 +112,7 @@ public:
_lock.unlock();
return result;
}
simgear::IPAddress* lookupSync(const string& host)
{
simgear::IPAddress* result = NULL;
@@ -140,31 +143,33 @@ protected:
*/
virtual void run()
{
while (true) {
_wait.wait(&_lock);
_lock.lock();
while (!_done) {
AddressCache::iterator it;
for (it = _cache.begin(); it != _cache.end(); ++it) {
if (it->second == NULL) {
string h = it->first;
_lock.unlock();
simgear::IPAddress* addr = new simgear::IPAddress;
// may take seconds or even minutes!
lookupHost(h.c_str(), *addr);
_lock.lock();
// cahce may have changed while we had the lock released -
// so iterators may be invalid: restart the traversal
it = _cache.begin();
_cache[h] = addr;
} // of found un-resolved entry
} // of un-resolved address iteration
} // of un-resolved address iteration
_wait.wait(_lock);
} // of thread run loop
_lock.unlock();
}
private:
static Resolver* static_instance;
/**
* The actual synchronous, blocking host lookup function
* do *not* call this with any locks (mutexs) held, since depending
@@ -177,7 +182,7 @@ private:
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
bool ok = false;
struct addrinfo* result0 = NULL;
int err = getaddrinfo(host, NULL, &hints, &result0);
if (err) {
@@ -205,21 +210,22 @@ private:
freeaddrinfo(result0);
return ok;
}
OpenThreads::Mutex _lock;
OpenThreads::Condition _wait;
SGMutex _lock;
SGWaitCondition _wait;
typedef std::map<string, simgear::IPAddress*> AddressCache;
AddressCache _cache;
bool _done;
};
Resolver* Resolver::static_instance = NULL;
} // of anonymous namespace
namespace simgear
{
IPAddress::IPAddress ( const char* host, int port )
{
set ( host, port ) ;
@@ -266,18 +272,18 @@ void IPAddress::set ( const char* host, int port )
addr->sin_addr.s_addr = INADDR_ANY;
return;
}
if (strcmp(host, "<broadcast>") == 0) {
addr->sin_addr.s_addr = INADDR_BROADCAST;
return;
}
// check the cache
IPAddress* cached = Resolver::instance()->lookupSync(host);
if (cached) {
memcpy(addr, cached->getAddr(), cached->getAddrLen());
}
addr->sin_port = htons (port); // fix up port after getaddrinfo
}
@@ -289,12 +295,12 @@ IPAddress::~IPAddress()
}
bool IPAddress::lookupNonblocking(const char* host, IPAddress& addr)
{
{
IPAddress* cached = Resolver::instance()->lookup(host);
if (!cached) {
return false;
}
addr = *cached;
return true;
}
@@ -313,9 +319,9 @@ const char* IPAddress::getHost () const
return buf;
}
unsigned int IPAddress::getIP () const
{
return addr->sin_addr.s_addr;
unsigned int IPAddress::getIP () const
{
return addr->sin_addr.s_addr;
}
unsigned int IPAddress::getPort() const
@@ -328,9 +334,9 @@ void IPAddress::setPort(int port)
addr->sin_port = htons(port);
}
unsigned int IPAddress::getFamily () const
{
return addr->sin_family;
unsigned int IPAddress::getFamily () const
{
return addr->sin_family;
}
const char* IPAddress::getLocalHost ()
@@ -371,7 +377,7 @@ struct sockaddr* IPAddress::getAddr() const
addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
memset(addr, 0, sizeof(struct sockaddr_in));
}
return (struct sockaddr*) addr;
}
@@ -456,7 +462,7 @@ void Socket::setBroadcast ( bool broadcast )
} else {
result = ::setsockopt( handle, SOL_SOCKET, SO_BROADCAST, NULL, 0 );
}
if ( result < 0 ) {
throw sg_exception("Socket::setBroadcast failed");
}
@@ -476,7 +482,7 @@ int Socket::bind ( const char* host, int port )
}
#endif
// 224.0.0.0 - 239.255.255.255 are multicast
// 224.0.0.0 - 239.255.255.255 are multicast
// Usage of 239.x.x.x is recommended for local scope
// Reference: http://tools.ietf.org/html/rfc5771
if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) {
@@ -486,7 +492,7 @@ int Socket::bind ( const char* host, int port )
a.sin_addr.S_un.S_addr = INADDR_ANY;
a.sin_family = AF_INET;
a.sin_port = htons(port);
if( (result = ::bind(handle,(const sockaddr*)&a,sizeof(a))) < 0 ) {
SG_LOG(SG_IO, SG_ALERT, "bind(any:" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
return result;
@@ -636,7 +642,7 @@ int Socket::select ( Socket** reads, Socket** writes, int timeout )
{
fd_set r,w;
int retval;
FD_ZERO (&r);
FD_ZERO (&w);
@@ -674,7 +680,7 @@ int Socket::select ( Socket** reads, Socket** writes, int timeout )
// It bothers me that select()'s first argument does not appear to
// work as advertised... [it hangs like this if called with
// anything less than FD_SETSIZE, which seems wasteful?]
// Note: we ignore the 'exception' fd_set - I have never had a
// need to use it. The name is somewhat misleading - the only
// thing I have ever seen it used for is to detect urgent data -

View File

@@ -1,6 +1,6 @@
#include "StringTable.hxx"
#include <OpenThreads/ScopedLock>
#include <simgear/threads/SGGuard.hxx>
namespace simgear
{
@@ -8,8 +8,7 @@ using namespace std;
const string* StringTable::insert(const string& str)
{
using namespace OpenThreads;
ScopedLock<Mutex> lock(_mutex);
SGGuard<SGMutex> lock(_mutex);
StringContainer::iterator it = _strings.insert(str).first;
return &*it;
}

View File

@@ -3,7 +3,7 @@
#include <string>
#include <OpenThreads/Mutex>
#include <simgear/threads/SGThread.hxx>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
@@ -21,7 +21,7 @@ class StringTable
{
const std::string* insert(const std::string& str);
private:
OpenThreads::Mutex _mutex;
SGMutex _mutex;
StringContainer _strings;
};
}

View File

@@ -11,13 +11,12 @@
#include <memory>
#include <simgear/props/props_io.hxx>
#include <OpenThreads/Mutex>
#include <OpenThreads/ScopedLock>
#include "commands.hxx"
#include <simgear/math/SGMath.hxx>
#include <simgear/structure/exception.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/debug/logstream.hxx>
@@ -36,7 +35,7 @@ SGCommandMgr::~SGCommandMgr ()
// no-op
}
OpenThreads::Mutex SGCommandMgr::_instanceMutex;
SGMutex SGCommandMgr::_instanceMutex;
SGCommandMgr*
SGCommandMgr::instance()
@@ -45,7 +44,7 @@ SGCommandMgr::instance()
if (mgr.get())
return mgr.get();
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
SGGuard<SGMutex> lock(_instanceMutex);
if (mgr.get())
return mgr.get();
@@ -85,8 +84,8 @@ SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) cons
command_t command = getCommand(name);
if (command == 0)
return false;
try {
return (*command)(arg);
} catch (sg_exception& e) {

View File

@@ -17,8 +17,7 @@
#include <map>
#include <vector>
#include <OpenThreads/Mutex>
#include <simgear/threads/SGThread.hxx>
#include <simgear/math/sg_types.hxx>
#include <simgear/props/props.hxx>
@@ -107,7 +106,7 @@ private:
typedef std::map<std::string,command_t> command_map;
command_map _commands;
static OpenThreads::Mutex _instanceMutex;
static SGMutex _instanceMutex;
};

View File

@@ -5,9 +5,8 @@
#include <cassert>
#include <queue>
#include <OpenThreads/Mutex>
#include <OpenThreads/ScopedLock>
#include <OpenThreads/Condition>
#include "SGGuard.hxx"
#include "SGThread.hxx"
/**
* SGQueue defines an interface for a FIFO.
@@ -66,7 +65,7 @@ public:
protected:
/**
*
*
*/
std::queue<T> fifo;
};
@@ -74,7 +73,7 @@ protected:
/**
* A simple thread safe queue. All access functions are guarded with a mutex.
*/
template<class T, class SGLOCK=OpenThreads::Mutex>
template<class T>
class SGLockedQueue : public SGQueue<T>
{
public:
@@ -95,7 +94,7 @@ public:
* @return bool True if queue is empty, otherwisr false.
*/
virtual bool empty() {
OpenThreads::ScopedLock<SGLOCK> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->fifo.empty();
}
@@ -105,7 +104,7 @@ public:
* @param T object to add.
*/
virtual void push( const T& item ) {
OpenThreads::ScopedLock<SGLOCK> g(mutex);
SGGuard<SGMutex> g(mutex);
this->fifo.push( item );
}
@@ -115,7 +114,7 @@ public:
* @return T next available object.
*/
virtual T front() {
OpenThreads::ScopedLock<SGLOCK> g(mutex);
SGGuard<SGMutex> g(mutex);
assert( ! this->fifo.empty() );
T item = this->fifo.front();
return item;
@@ -127,7 +126,7 @@ public:
* @return T next available object.
*/
virtual T pop() {
OpenThreads::ScopedLock<SGLOCK> g(mutex);
SGGuard<SGMutex> g(mutex);
//if (fifo.empty()) throw NoSuchElementException();
assert( ! this->fifo.empty() );
// if (fifo.empty())
@@ -146,7 +145,7 @@ public:
* @return size_t size of queue.
*/
virtual size_t size() {
OpenThreads::ScopedLock<SGLOCK> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->fifo.size();
}
@@ -155,7 +154,7 @@ private:
/**
* Mutex to serialise access.
*/
SGLOCK mutex;
SGMutex mutex;
private:
// Prevent copying.
@@ -182,10 +181,10 @@ public:
~SGBlockingQueue() {}
/**
*
*
*/
virtual bool empty() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->fifo.empty();
}
@@ -195,7 +194,7 @@ public:
* @param T object to add.
*/
virtual void push( const T& item ) {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
this->fifo.push( item );
not_empty.signal();
}
@@ -207,7 +206,7 @@ public:
* @return T next available object.
*/
virtual T front() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
assert(this->fifo.empty() != true);
//if (fifo.empty()) throw ??
@@ -223,10 +222,10 @@ public:
* @return T next available object.
*/
virtual T pop() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
while (this->fifo.empty())
not_empty.wait(&mutex);
not_empty.wait(mutex);
assert(this->fifo.empty() != true);
//if (fifo.empty()) throw ??
@@ -242,7 +241,7 @@ public:
* @return size_t size of queue.
*/
virtual size_t size() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->fifo.size();
}
@@ -251,12 +250,12 @@ private:
/**
* Mutex to serialise access.
*/
OpenThreads::Mutex mutex;
SGMutex mutex;
/**
* Condition to signal when queue not empty.
*/
OpenThreads::Condition not_empty;
SGWaitCondition not_empty;
private:
// Prevent copying.
@@ -284,18 +283,18 @@ public:
~SGBlockingDeque() {}
/**
*
*
*/
virtual void clear() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
this->queue.clear();
}
/**
*
*
*/
virtual bool empty() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->queue.empty();
}
@@ -305,7 +304,7 @@ public:
* @param T object to add.
*/
virtual void push_front( const T& item ) {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
this->queue.push_front( item );
not_empty.signal();
}
@@ -316,7 +315,7 @@ public:
* @param T object to add.
*/
virtual void push_back( const T& item ) {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
this->queue.push_back( item );
not_empty.signal();
}
@@ -328,7 +327,7 @@ public:
* @return T next available object.
*/
virtual T front() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
assert(this->queue.empty() != true);
//if (queue.empty()) throw ??
@@ -344,10 +343,10 @@ public:
* @return T next available object.
*/
virtual T pop_front() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
while (this->queue.empty())
not_empty.wait(&mutex);
not_empty.wait(mutex);
assert(this->queue.empty() != true);
//if (queue.empty()) throw ??
@@ -364,10 +363,10 @@ public:
* @return T next available object.
*/
virtual T pop_back() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
while (this->queue.empty())
not_empty.wait(&mutex);
not_empty.wait(mutex);
assert(this->queue.empty() != true);
//if (queue.empty()) throw ??
@@ -383,7 +382,7 @@ public:
* @return size_t size of queue.
*/
virtual size_t size() {
OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
SGGuard<SGMutex> g(mutex);
return this->queue.size();
}
@@ -392,12 +391,12 @@ private:
/**
* Mutex to serialise access.
*/
OpenThreads::Mutex mutex;
SGMutex mutex;
/**
* Condition to signal when queue not empty.
*/
OpenThreads::Condition not_empty;
SGWaitCondition not_empty;
private:
// Prevent copying.