aboutsummaryrefslogtreecommitdiff
path: root/src/util/container.h
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2015-04-07 06:13:12 -0400
committerShadowNinja <shadowninja@minetest.net>2015-08-23 22:04:06 -0400
commite4bff8be94c0db4f94e63ad448d0eeb869ccdbbd (patch)
tree7935586e79da5c8c7144e345a8c0fc1cda53beed /src/util/container.h
parent6a1047d8c116f793890b63427d53f04ceca95d54 (diff)
downloadminetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.tar.gz
minetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.tar.bz2
minetest-e4bff8be94c0db4f94e63ad448d0eeb869ccdbbd.zip
Clean up threading
* Rename everything. * Strip J prefix. * Change UpperCamelCase functions to lowerCamelCase. * Remove global (!) semaphore count mutex on OSX. * Remove semaphore count getter (unused, unsafe, depended on internal API functions on Windows, and used a hack on OSX). * Add `Atomic<type>`. * Make `Thread` handle thread names. * Add support for C++11 multi-threading. * Combine pthread and win32 sources. * Remove `ThreadStarted` (unused, unneeded). * Move some includes from the headers to the sources. * Move all of `Event` into its header (allows inlining with no new includes). * Make `Event` use `Semaphore` (except on Windows). * Move some porting functions into `Thread`. * Integrate logging with `Thread`. * Add threading test.
Diffstat (limited to 'src/util/container.h')
-rw-r--r--src/util/container.h171
1 files changed, 48 insertions, 123 deletions
diff --git a/src/util/container.h b/src/util/container.h
index 267d54c16..7f66b89ac 100644
--- a/src/util/container.h
+++ b/src/util/container.h
@@ -22,9 +22,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../irrlichttypes.h"
#include "../exceptions.h"
-#include "../jthread/jmutex.h"
-#include "../jthread/jmutexautolock.h"
-#include "../jthread/jsemaphore.h"
+#include "../threading/mutex.h"
+#include "../threading/mutex_auto_lock.h"
+#include "../threading/semaphore.h"
#include <list>
#include <vector>
#include <map>
@@ -81,111 +81,47 @@ template<typename Key, typename Value>
class MutexedMap
{
public:
- MutexedMap()
- {
- }
+ MutexedMap() {}
void set(const Key &name, const Value &value)
{
- JMutexAutoLock lock(m_mutex);
-
+ MutexAutoLock lock(m_mutex);
m_values[name] = value;
}
- bool get(const Key &name, Value *result)
+ bool get(const Key &name, Value *result) const
{
- JMutexAutoLock lock(m_mutex);
-
- typename std::map<Key, Value>::iterator n;
- n = m_values.find(name);
-
- if(n == m_values.end())
+ MutexAutoLock lock(m_mutex);
+ typename std::map<Key, Value>::const_iterator n =
+ m_values.find(name);
+ if (n == m_values.end())
return false;
-
- if(result != NULL)
+ if (result)
*result = n->second;
-
return true;
}
- std::vector<Value> getValues()
+ std::vector<Value> getValues() const
{
+ MutexAutoLock lock(m_mutex);
std::vector<Value> result;
- for(typename std::map<Key, Value>::iterator
- i = m_values.begin();
- i != m_values.end(); ++i){
- result.push_back(i->second);
+ for (typename std::map<Key, Value>::const_iterator
+ it = m_values.begin();
+ it != m_values.end(); ++it){
+ result.push_back(it->second);
}
return result;
}
- void clear ()
- {
- m_values.clear();
- }
+ void clear() { m_values.clear(); }
private:
std::map<Key, Value> m_values;
- JMutex m_mutex;
+ mutable Mutex m_mutex;
};
-/*
-Generates ids for comparable values.
-Id=0 is reserved for "no value".
-Is fast at:
-- Returning value by id (very fast)
-- Returning id by value
-- Generating a new id for a value
-
-Is not able to:
-- Remove an id/value pair (is possible to implement but slow)
-*/
-template<typename T>
-class MutexedIdGenerator
-{
-public:
- MutexedIdGenerator()
- {
- }
-
- // Returns true if found
- bool getValue(u32 id, T &value)
- {
- if(id == 0)
- return false;
- JMutexAutoLock lock(m_mutex);
- if(m_id_to_value.size() < id)
- return false;
- value = m_id_to_value[id-1];
- return true;
- }
-
- // If id exists for value, returns the id.
- // Otherwise generates an id for the value.
- u32 getId(const T &value)
- {
- JMutexAutoLock lock(m_mutex);
- typename std::map<T, u32>::iterator n;
- n = m_value_to_id.find(value);
- if(n != m_value_to_id.end())
- return n->second;
- m_id_to_value.push_back(value);
- u32 new_id = m_id_to_value.size();
- m_value_to_id.insert(value, new_id);
- return new_id;
- }
-
-private:
- JMutex m_mutex;
- // Values are stored here at id-1 position (id 1 = [0])
- std::vector<T> m_id_to_value;
- std::map<T, u32> m_value_to_id;
-};
-
-/*
-Thread-safe FIFO queue (well, actually a FILO also)
-*/
+// Thread-safe Double-ended queue
template<typename T>
class MutexedQueue
@@ -194,19 +130,18 @@ public:
template<typename Key, typename U, typename Caller, typename CallerData>
friend class RequestQueue;
- MutexedQueue()
- {
- }
- bool empty()
+ MutexedQueue() {}
+ bool empty() const
{
- JMutexAutoLock lock(m_mutex);
- return (m_queue.size() == 0);
+ MutexAutoLock lock(m_mutex);
+ return m_queue.empty();
}
+
void push_back(T t)
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
m_queue.push_back(t);
- m_size.Post();
+ m_signal.post();
}
/* this version of pop_front returns a empty element of T on timeout.
@@ -214,37 +149,35 @@ public:
*/
T pop_frontNoEx(u32 wait_time_max_ms)
{
- if (m_size.Wait(wait_time_max_ms)) {
- JMutexAutoLock lock(m_mutex);
+ if (m_signal.wait(wait_time_max_ms)) {
+ MutexAutoLock lock(m_mutex);
T t = m_queue.front();
m_queue.pop_front();
return t;
- }
- else {
+ } else {
return T();
}
}
T pop_front(u32 wait_time_max_ms)
{
- if (m_size.Wait(wait_time_max_ms)) {
- JMutexAutoLock lock(m_mutex);
+ if (m_signal.wait(wait_time_max_ms)) {
+ MutexAutoLock lock(m_mutex);
T t = m_queue.front();
m_queue.pop_front();
return t;
- }
- else {
+ } else {
throw ItemNotFoundException("MutexedQueue: queue is empty");
}
}
T pop_frontNoEx()
{
- m_size.Wait();
+ m_signal.wait();
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
T t = m_queue.front();
m_queue.pop_front();
@@ -253,14 +186,13 @@ public:
T pop_back(u32 wait_time_max_ms=0)
{
- if (m_size.Wait(wait_time_max_ms)) {
- JMutexAutoLock lock(m_mutex);
+ if (m_signal.wait(wait_time_max_ms)) {
+ MutexAutoLock lock(m_mutex);
T t = m_queue.back();
m_queue.pop_back();
return t;
- }
- else {
+ } else {
throw ItemNotFoundException("MutexedQueue: queue is empty");
}
}
@@ -268,25 +200,24 @@ public:
/* this version of pop_back returns a empty element of T on timeout.
* Make sure default constructor of T creates a recognizable "empty" element
*/
- T pop_backNoEx(u32 wait_time_max_ms=0)
+ T pop_backNoEx(u32 wait_time_max_ms)
{
- if (m_size.Wait(wait_time_max_ms)) {
- JMutexAutoLock lock(m_mutex);
+ if (m_signal.wait(wait_time_max_ms)) {
+ MutexAutoLock lock(m_mutex);
T t = m_queue.back();
m_queue.pop_back();
return t;
- }
- else {
+ } else {
return T();
}
}
T pop_backNoEx()
{
- m_size.Wait();
+ m_signal.wait();
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
T t = m_queue.back();
m_queue.pop_back();
@@ -294,19 +225,13 @@ public:
}
protected:
- JMutex & getMutex()
- {
- return m_mutex;
- }
+ Mutex &getMutex() { return m_mutex; }
- std::deque<T> & getQueue()
- {
- return m_queue;
- }
+ std::deque<T> &getQueue() { return m_queue; }
std::deque<T> m_queue;
- JMutex m_mutex;
- JSemaphore m_size;
+ mutable Mutex m_mutex;
+ Semaphore m_signal;
};
template<typename K, typename V>