Added a really simple thread-safe queue. It will still need a lot of work.

This commit is contained in:
Relintai 2019-12-03 03:39:53 +01:00
parent 2b369f5be8
commit c386b6850b
4 changed files with 208 additions and 0 deletions

2
SCsub
View File

@ -2,6 +2,8 @@ Import('env')
env.add_source_files(env.modules_sources,"register_types.cpp")
env.add_source_files(env.modules_sources,"containers/voxelman_queue.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library_simple.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library_merger.cpp")

View File

@ -0,0 +1,145 @@
#include "voxelman_queue.h"
template<typename T>
void VMQueue<T>::enqueue(T obj) {
_lock->write_lock();
if (_size == _count) {
_resize(_size + 10);
}
_items[(_head + _count) % _size] = obj;
++_count;
_lock->write_unlock();
}
template<typename T>
T VMQueue<T>::dequeue() {
if (_size == 0)
return T();
if (_count == 0)
return T();
T obj;
_lock->write_lock();
obj = _items[_head];
_items[_head] = T();
++_head;
--_count;
if (_head >= _size)
_head -= _size;
_lock->write_unlock();
return obj;
}
template<typename T>
T VMQueue<T>::peek() {
T obj;
_lock->read_lock();
obj = _items[_head];
_lock->read_unlock();
return obj;
}
template<typename T>
void VMQueue<T>::resize(int new_size) {
_lock->write_lock();
_resize(new_size);
_lock->write_unlock();
}
template<typename T>
void VMQueue<T>::_resize(int new_size) {
if (new_size == _size)
return;
if (new_size > _size) {
T *narr = memnew_arr(T, new_size);
//copymem(narr, _items, sizeof(T) * _size);
for (int i = 0; i < _size; ++i) {
narr[i] = _items[i];
}
memdelete_arr(_items);
_items = narr;
_size = new_size;
return;
}
}
template<typename T>
VMQueue<T>::VMQueue(int initial_size) {
_items = NULL;
_head = 0;
_count = 0;
_size = initial_size;
_lock = RWLock::create();
_items = memnew_arr(T, _size);
}
template<typename T>
VMQueue<T>::VMQueue() {
_items = NULL;
_head = 0;
_count = 0;
_size = 10;
_lock = RWLock::create();
_items = memnew_arr(T, _size);
}
template<typename T>
VMQueue<T>::~VMQueue() {
if (_items)
memdelete_arr(_items);
memdelete(_lock);
}
void VoxelmanQueue::enqueue(Variant obj) {
_queue.enqueue(obj);
}
Variant VoxelmanQueue::dequeue() {
return _queue.dequeue();
}
Variant VoxelmanQueue::peek() {
return _queue.peek();
}
void VoxelmanQueue::resize(int new_size) {
_queue.resize(new_size);
}
VoxelmanQueue::VoxelmanQueue() {
}
VoxelmanQueue::~VoxelmanQueue() {
}
void VoxelmanQueue::_bind_methods() {
ClassDB::bind_method(D_METHOD("enqueue", "obj"), &VoxelmanQueue::enqueue);
ClassDB::bind_method(D_METHOD("dequeue"), &VoxelmanQueue::dequeue);
ClassDB::bind_method(D_METHOD("peek"), &VoxelmanQueue::peek);
ClassDB::bind_method(D_METHOD("resize", "new_size"), &VoxelmanQueue::resize);
}

View File

@ -0,0 +1,57 @@
#ifndef VOXELMAN_QUEUE_H
#define VOXELMAN_QUEUE_H
#include "core/reference.h"
#include "core/os/copymem.h"
#include "core/os/memory.h"
#include "core/os/rw_lock.h"
#include "core/variant.h"
template<class T>
class VMQueue {
public:
void enqueue(T obj);
T dequeue();
T peek();
void resize(int new_size);
VMQueue(int initial_size);
VMQueue();
~VMQueue();
protected:
void _resize(int new_size);
private:
RWLock *_lock;
int _head;
int _count;
int _size;
T *_items;
};
class VoxelmanQueue : public Reference {
GDCLASS(VoxelmanQueue, Reference);
public:
void enqueue(Variant obj);
Variant dequeue();
Variant peek();
void resize(int new_size);
VoxelmanQueue();
~VoxelmanQueue();
protected:
static void _bind_methods();
private:
VMQueue<Variant> _queue;
};
#endif

View File

@ -1,5 +1,7 @@
#include "register_types.h"
#include "containers/voxelman_queue.h"
#include "library/voxel_surface.h"
#include "library/voxel_surface_simple.h"
#include "library/voxel_surface_merger.h"
@ -39,6 +41,8 @@
void register_voxelman_types() {
ClassDB::register_class<VoxelmanQueue>();
ClassDB::register_class<VoxelMesher>();
ClassDB::register_class<VoxelMesherTransvoxel>();
ClassDB::register_class<TransvoxelCellData>();