-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMapLockManager.cc
171 lines (137 loc) · 3.04 KB
/
MapLockManager.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// -*- c++ -*-
// Copyright 2009 Isis Innovation Limited
/********************************************************************
A Class to manage the locking and unlocking of maps
Author: Robert Castle, 2008, [email protected]
********************************************************************/
#include "MapLockManager.h"
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#endif
namespace PTAMM {
/**
* add thread to the usage map list
* @param t thread to add
*/
void MapLockManager::Register( void * t )
{
mLockRecords[ t ] = false;
}
/**
* remove a thread from the map usage list
* @param t thread to remove
*/
void MapLockManager::UnRegister( void * t)
{
mLockRecords.erase( t );
}
/**
* is a thread registered
* @param t is thread registered
* @return true/false
*/
bool MapLockManager::IsRegistered( void * t)
{
std::map< void *, bool >::iterator i;
i = mLockRecords.find( t );
if( i != mLockRecords.end() ) {
return true;
}
else {
return false;
}
}
/**
* Threads call this to see if the map is locked
* @param t checking thread
* @param nTimeout timeout before returning
* @return false if no lock, or true if locked
* @TODO implement the timeout
*/
bool MapLockManager::CheckLockAndWait( void * t, int nTimeout)
{
//not locked continue
if( !mbLocked ) {
return false;
}
//we are locked so ack
mLockRecords[ t ] = true;
//if locked do we wait.
while( mbLocked )
{
#ifdef WIN32
Sleep(1);
#else
usleep(300);
#endif
}
mLockRecords[ t ] = false;
return true;
}
/**
* lock the map
* @param t locing thread
* @param nTimeout timeout before aborting
* @return sucess
* @TODO implement timeout
*/
bool MapLockManager::LockMap( void * t, int nTimeout )
{
if( mbLocked && t != mLockingThread ) {
std::cerr << "Map is already locked by another thread." << std::endl;
return false;
}
mbLocked = true;
mLockingThread = t;
bool bAllAck = true;
std::map< void *, bool >::iterator i;
//if thread is the only user then you have got lock
if( mLockRecords.size() == 1 ) {
if( mLockRecords.find( t ) != mLockRecords.end() ) {
std::cerr << "only map user. lock granted" << std::endl;
return true;
}
else {
std::cerr << "WARNING: This thread is not registered to this map! Another thread is so waiting for them." << std::endl;
}
}
//wait for other threads to ack.
do
{
bAllAck = true;
for( i = mLockRecords.begin(); i != mLockRecords.end(); i++ )
{
if( (*i).first == t ) {
//locking yourself out is stupid
continue;
}
//will only go true when all have ackknowledged
bAllAck = bAllAck && (*i).second;
}
//wait for a bit
if(!bAllAck) {
#ifdef WIN32
Sleep(1);
#else
usleep(300);
#endif
}
} while( !bAllAck );
return true;
}
/**
* Unlock a map
* @param t thread
* @return success
*/
bool MapLockManager::UnlockMap( void * t)
{
if(t == mLockingThread) {
mbLocked = false;
mLockingThread = NULL;
return true;
}
return false;
}
}