-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathOgrLoader.cpp
More file actions
135 lines (117 loc) · 3.33 KB
/
Copy pathOgrLoader.cpp
File metadata and controls
135 lines (117 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "stdafx.h"
#include "OgrLoader.h"
#include "LabelsHelper.h"
#include "windows.h"
long OgrLoadingTask::SeedId = 0;
// **********************************************
// Restart()
// **********************************************
void OgrDynamicLoader::Restart()
{
if (!_stop) CancelAllTasks();
_lockCounter = 0L;
_stop = false;
}
// **********************************************
// PutData()
// **********************************************
void OgrDynamicLoader::PutData(vector<ShapeRecordData*> shapeData)
{ // Locking data in this function
CSingleLock lock(&DataLock, TRUE);
Data.insert(Data.end(), shapeData.begin(), shapeData.end());
hasData = true;
}
// **********************************************
// FetchData()
// **********************************************
vector<ShapeRecordData*> OgrDynamicLoader::FetchData()
{ // Locking data in this function
vector<ShapeRecordData*> data;
CSingleLock lock(&DataLock, TRUE);
if (Data.size() > 0) {
data.insert(data.end(), Data.begin(), Data.end());
Data.clear();
}
hasData = false;
return data;
}
// **********************************************
// FetchData()
// **********************************************
bool OgrDynamicLoader::HasData()
{
return hasData;
}
// **********************************************
// EnqueueTask()
// **********************************************
void OgrDynamicLoader::EnqueueTask(OgrLoadingTask * task)
{
CSingleLock (&QueueLock, TRUE);
Queue.push(task);
}
// **********************************************
// SignalWaitingTask()
// **********************************************
bool OgrDynamicLoader::SignalWaitingTask()
{
if (_stop) return false;
InterlockedIncrement(&_lockCounter);
return true;
};
// **********************************************
// CancelAllTasks()
// **********************************************
void OgrDynamicLoader::CancelAllTasks()
{
if (_stop) return;
_stop = true; // any newly arrived threads won't reach the lock
_lockCounter += (ULONG)1e4; // any threads awaiting the lock will be aborted immediately after acquiring it
InterlockedIncrement(&_lockCounter);
return;
}
// **********************************************
// ClearFinishedTasks()
// **********************************************
void OgrDynamicLoader::ClearFinishedTasks()
{
CSingleLock queueLock(&QueueLock, TRUE);
std::queue<OgrLoadingTask*> unfqueue;
while (!Queue.empty())
{
OgrLoadingTask* task = Queue.front();
Queue.pop();
if (!task->Finished) {
unfqueue.push(task);
continue;
}
if (!task->Cancelled) { // If succesful return a clone:
OgrLoadingTask* infoClone = task->Clone();
}
delete task;
}
// Replace queue with the unfinished items queue
Queue.swap(unfqueue);
}
// **********************************************
// AwaitTasks()
// **********************************************
void OgrDynamicLoader::AwaitTasks()
{
CSingleLock queueLock(&QueueLock, TRUE);
while (!Queue.empty())
{
OgrLoadingTask* task = Queue.front();
Queue.pop();
if (!task->Finished) {
queueLock.Unlock(); // Unlock briefly so other threads can fetch
Sleep(10);
queueLock.Lock();
continue; // Try next
}
else
{
delete task;
}
}
}