summaryrefslogtreecommitdiffstats
path: root/src/WorldStorage/WorldStorage.cpp
diff options
context:
space:
mode:
authorTycho Bickerstaff <work.tycho@gmail.com>2013-12-31 16:48:57 +0100
committerTycho Bickerstaff <work.tycho@gmail.com>2013-12-31 16:48:57 +0100
commitf3736b1eb7bf698518cdb853ee29ee96b9c24a52 (patch)
treeb5a9c4f14e44bc0fd9b88c9d30bc4036c06eff5e /src/WorldStorage/WorldStorage.cpp
parentadded link dependency between WorldStorage and OSSupport (diff)
downloadcuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.gz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.bz2
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.lz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.xz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.zst
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.zip
Diffstat (limited to 'src/WorldStorage/WorldStorage.cpp')
-rw-r--r--src/WorldStorage/WorldStorage.cpp136
1 files changed, 44 insertions, 92 deletions
diff --git a/src/WorldStorage/WorldStorage.cpp b/src/WorldStorage/WorldStorage.cpp
index f290ec128..c3bfbd4f6 100644
--- a/src/WorldStorage/WorldStorage.cpp
+++ b/src/WorldStorage/WorldStorage.cpp
@@ -13,7 +13,7 @@
#include "../Generating/ChunkGenerator.h"
#include "../Entities/Entity.h"
#include "../BlockEntities/BlockEntity.h"
-
+#include "../OSSupport/Promise.h"
@@ -63,8 +63,6 @@ cWorldStorage::~cWorldStorage()
{
delete *itr;
} // for itr - m_Schemas[]
- m_LoadQueue.clear();
- m_SaveQueue.clear();
}
@@ -98,9 +96,7 @@ void cWorldStorage::WaitForFinish(void)
LOG("Waiting for the world storage to finish saving");
{
- // Cancel all loading requests:
- cCSLock Lock(m_CSQueues);
- m_LoadQueue.clear();
+ m_LoadQueue.Clear();
}
// Wait for the saving to finish:
@@ -120,32 +116,36 @@ void cWorldStorage::WaitForFinish(void)
void cWorldStorage::WaitForQueuesEmpty(void)
{
- cCSLock Lock(m_CSQueues);
- while (!m_ShouldTerminate && (!m_LoadQueue.empty() || !m_SaveQueue.empty()))
- {
- cCSUnlock Unlock(Lock);
- m_evtRemoved.Wait();
- }
+
+ cPromise * LoadPromise = m_LoadQueue.BlockTillEmpty();
+ cPromise * SavePromise = m_SaveQueue.BlockTillEmpty();
+ cPromise * QueuePromise = LoadPromise->WaitFor(SavePromise);
+ cPromise * CancelPromise = QueuePromise->CancelOn(m_ShouldTerminate);
+ CancelPromise->Wait();
+ delete CancelPromise;
+ delete QueuePromise;
+ delete SavePromise;
+ delete LoadPromise;
}
-int cWorldStorage::GetLoadQueueLength(void)
+size_t cWorldStorage::GetLoadQueueLength(void)
{
cCSLock Lock(m_CSQueues);
- return (int)m_LoadQueue.size();
+ return m_LoadQueue.Size();
}
-int cWorldStorage::GetSaveQueueLength(void)
+size_t cWorldStorage::GetSaveQueueLength(void)
{
cCSLock Lock(m_CSQueues);
- return (int)m_SaveQueue.size();
+ return m_SaveQueue.Size();
}
@@ -154,22 +154,7 @@ int cWorldStorage::GetSaveQueueLength(void)
void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ, bool a_Generate)
{
- // Queues the chunk for loading; if not loaded, the chunk will be generated
- {
- cCSLock Lock(m_CSQueues);
-
- // Check if already in the queue:
- for (sChunkLoadQueue::iterator itr = m_LoadQueue.begin(); itr != m_LoadQueue.end(); ++itr)
- {
- if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkY == a_ChunkY) && (itr->m_ChunkZ == a_ChunkZ) && (itr->m_Generate == a_Generate))
- {
- return;
- }
- }
- m_LoadQueue.push_back(sChunkLoad(a_ChunkX, a_ChunkY, a_ChunkZ, a_Generate));
- }
-
- m_Event.Set();
+ m_LoadQueue.EnqueueItemIfNotPresent(sChunkLoad(a_ChunkX, a_ChunkY, a_ChunkZ, a_Generate));
}
@@ -178,12 +163,7 @@ void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ, boo
void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
{
- {
- cCSLock Lock(m_CSQueues);
- m_SaveQueue.remove (cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ)); // Don't add twice
- m_SaveQueue.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
- }
- m_Event.Set();
+ m_SaveQueue.EnqueueItemIfNotPresent(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
}
@@ -192,12 +172,8 @@ void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
void cWorldStorage::QueueSavedMessage(void)
{
- // Pushes a special coord pair into the queue, signalizing a message instead:
- {
- cCSLock Lock(m_CSQueues);
- m_SaveQueue.push_back(cChunkCoords(0, CHUNK_Y_MESSAGE, 0));
- }
- m_Event.Set();
+ // Pushes a special coord pair into the queue, signalizing a message instead
+ m_SaveQueue.EnqueueItem(cChunkCoords(0, CHUNK_Y_MESSAGE, 0));
}
@@ -206,7 +182,7 @@ void cWorldStorage::QueueSavedMessage(void)
void cWorldStorage::UnqueueLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
{
- cCSLock Lock(m_CSQueues);
+ /*cCSLock Lock(m_CSQueues);
for (sChunkLoadQueue::iterator itr = m_LoadQueue.begin(); itr != m_LoadQueue.end(); ++itr)
{
if ((itr->m_ChunkX != a_ChunkX) || (itr->m_ChunkY != a_ChunkY) || (itr->m_ChunkZ != a_ChunkZ))
@@ -217,7 +193,8 @@ void cWorldStorage::UnqueueLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
Lock.Unlock();
m_evtRemoved.Set();
return;
- } // for itr - m_LoadQueue[]
+ } // for itr - m_LoadQueue[]*/
+ m_LoadQueue.Remove(sChunkLoad(a_ChunkX, a_ChunkY, a_ChunkZ,true));
}
@@ -226,11 +203,7 @@ void cWorldStorage::UnqueueLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
void cWorldStorage::UnqueueSave(const cChunkCoords & a_Chunk)
{
- {
- cCSLock Lock(m_CSQueues);
- m_SaveQueue.remove(a_Chunk);
- }
- m_evtRemoved.Set();
+ m_SaveQueue.Remove(a_Chunk);
}
@@ -281,19 +254,19 @@ void cWorldStorage::Execute(void)
m_Event.Wait();
// Process both queues until they are empty again:
- bool HasMore;
+ bool Success;
do
{
- HasMore = false;
+ Success = false;
if (m_ShouldTerminate)
{
return;
}
- HasMore = LoadOneChunk();
- HasMore = HasMore | SaveOneChunk();
+ Success = LoadOneChunk();
+ Success |= SaveOneChunk();
m_evtRemoved.Set();
- } while (HasMore);
+ } while (Success);
}
}
@@ -304,19 +277,7 @@ void cWorldStorage::Execute(void)
bool cWorldStorage::LoadOneChunk(void)
{
sChunkLoad ToLoad(0, 0, 0, false);
- bool HasMore;
- bool ShouldLoad = false;
- {
- cCSLock Lock(m_CSQueues);
- if (!m_LoadQueue.empty())
- {
- ToLoad = m_LoadQueue.front();
- m_LoadQueue.pop_front();
- ShouldLoad = true;
- }
- HasMore = !m_LoadQueue.empty();
- }
-
+ bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad);
if (ShouldLoad && !LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkY, ToLoad.m_ChunkZ))
{
if (ToLoad.m_Generate)
@@ -330,7 +291,7 @@ bool cWorldStorage::LoadOneChunk(void)
// m_World->ChunkLoadFailed(ToLoad.m_ChunkX, ToLoad.m_ChunkY, ToLoad.m_ChunkZ);
}
}
- return HasMore;
+ return ShouldLoad;
}
@@ -339,33 +300,24 @@ bool cWorldStorage::LoadOneChunk(void)
bool cWorldStorage::SaveOneChunk(void)
{
- cChunkCoords Save(0, 0, 0);
- bool HasMore;
- bool ShouldSave = false;
- {
- cCSLock Lock(m_CSQueues);
- if (!m_SaveQueue.empty())
+ cChunkCoords ToSave(0, 0, 0);
+ bool ShouldSave = m_SaveQueue.TryDequeueItem(ToSave);
+ if(ShouldSave) {
+ if (ToSave.m_ChunkY == CHUNK_Y_MESSAGE)
{
- Save = m_SaveQueue.front();
- m_SaveQueue.pop_front();
- ShouldSave = true;
+ LOGINFO("Saved all chunks in world %s", m_World->GetName().c_str());
+ return ShouldSave;
}
- HasMore = !m_SaveQueue.empty();
- }
- if (Save.m_ChunkY == CHUNK_Y_MESSAGE)
- {
- LOGINFO("Saved all chunks in world %s", m_World->GetName().c_str());
- return HasMore;
- }
- if (ShouldSave && m_World->IsChunkValid(Save.m_ChunkX, Save.m_ChunkZ))
- {
- m_World->MarkChunkSaving(Save.m_ChunkX, Save.m_ChunkZ);
- if (m_SaveSchema->SaveChunk(Save))
+ if (ShouldSave && m_World->IsChunkValid(ToSave.m_ChunkX, ToSave.m_ChunkZ))
{
- m_World->MarkChunkSaved(Save.m_ChunkX, Save.m_ChunkZ);
+ m_World->MarkChunkSaving(ToSave.m_ChunkX, ToSave.m_ChunkZ);
+ if (m_SaveSchema->SaveChunk(ToSave))
+ {
+ m_World->MarkChunkSaved(ToSave.m_ChunkX, ToSave.m_ChunkZ);
+ }
}
}
- return HasMore;
+ return ShouldSave;
}