diff --git a/projects/VC90/SimGear.vcproj b/projects/VC90/SimGear.vcproj
index 34aa5752..077af7ae 100644
--- a/projects/VC90/SimGear.vcproj
+++ b/projects/VC90/SimGear.vcproj
@@ -1487,6 +1487,14 @@
RelativePath="..\..\simgear\threads\SGQueue.hxx"
>
+
+
+
+
# include
# include
-# include
+# include
# include
# include
# include
@@ -61,40 +61,43 @@
#include
#include
-
-#include
-#include
-#include
+#include
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 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, "") == 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 -
diff --git a/simgear/structure/StringTable.cxx b/simgear/structure/StringTable.cxx
index 81afc69b..5dba1848 100644
--- a/simgear/structure/StringTable.cxx
+++ b/simgear/structure/StringTable.cxx
@@ -1,6 +1,6 @@
#include "StringTable.hxx"
-#include
+#include
namespace simgear
{
@@ -8,8 +8,7 @@ using namespace std;
const string* StringTable::insert(const string& str)
{
- using namespace OpenThreads;
- ScopedLock lock(_mutex);
+ SGGuard lock(_mutex);
StringContainer::iterator it = _strings.insert(str).first;
return &*it;
}
diff --git a/simgear/structure/StringTable.hxx b/simgear/structure/StringTable.hxx
index 9e5700d3..8f5d010a 100644
--- a/simgear/structure/StringTable.hxx
+++ b/simgear/structure/StringTable.hxx
@@ -3,7 +3,7 @@
#include
-#include
+#include
#include
#include
#include
@@ -21,7 +21,7 @@ class StringTable
{
const std::string* insert(const std::string& str);
private:
- OpenThreads::Mutex _mutex;
+ SGMutex _mutex;
StringContainer _strings;
};
}
diff --git a/simgear/structure/commands.cxx b/simgear/structure/commands.cxx
index fd67ae4f..6b4b11ea 100644
--- a/simgear/structure/commands.cxx
+++ b/simgear/structure/commands.cxx
@@ -11,13 +11,12 @@
#include
#include
-#include
-#include
-
#include "commands.hxx"
#include
#include
+#include
+#include
#include
@@ -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 lock(_instanceMutex);
+ SGGuard 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) {
diff --git a/simgear/structure/commands.hxx b/simgear/structure/commands.hxx
index 3f8a248f..09f1c1bc 100644
--- a/simgear/structure/commands.hxx
+++ b/simgear/structure/commands.hxx
@@ -17,8 +17,7 @@
#include