9
3

MutexMap.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef MUTEXMAP_H
  17. #define MUTEXMAP_H
  18. #include <map>
  19. #include "MutexHelper.h"
  20. #define MUTEXMAP_DELETE_TYPE_KEY 1
  21. #define MUTEXMAP_DELETE_TYPE_VALUE 2
  22. template <typename KeyT, typename ValueT>
  23. class MutexMap{
  24. public:
  25. MutexMap(){
  26. has_pending_data = false;
  27. pending_clear = false;
  28. changing = false;
  29. access_count = 0;
  30. has_pending_deletes = false;
  31. next_delete_attempt = 0;
  32. delete_all = false;
  33. }
  34. ~MutexMap(){
  35. update(true);
  36. PendingLock.lock();
  37. pending_add.clear();
  38. pending_remove.clear();
  39. PendingLock.unlock();
  40. }
  41. class iterator {
  42. private:
  43. typename std::map<KeyT, ValueT>::iterator itr; // Current element
  44. MutexMap* map;
  45. bool first_itr;
  46. public:
  47. iterator(){
  48. }
  49. iterator(MutexMap* map){
  50. this->map = map;
  51. map->update();
  52. map->SetChanging();
  53. this->map->AddAccess();
  54. map->SetNotChanging();
  55. first_itr = true;
  56. itr = map->current_data.begin();
  57. if(itr != map->current_data.end()){
  58. first = itr->first;
  59. second = itr->second;
  60. }
  61. }
  62. ~iterator(){
  63. map->RemoveAccess();
  64. }
  65. bool HasNext(){
  66. return itr != map->current_data.end();
  67. }
  68. bool Next(){
  69. if(map->pending_clear)
  70. return false;
  71. if(first_itr)
  72. first_itr = false;
  73. else
  74. itr++;
  75. if(itr != map->current_data.end()){
  76. first = itr->first;
  77. second = itr->second;
  78. map->PendingLock.lock();
  79. if(map->pending_remove.count(first) > 0){
  80. map->PendingLock.unlock();
  81. return Next();
  82. }
  83. map->PendingLock.unlock();
  84. return true;
  85. }
  86. return false;
  87. }
  88. iterator* operator->() {
  89. return this;
  90. }
  91. KeyT first;
  92. ValueT second;
  93. };
  94. int count(KeyT key, bool include_pending = false){
  95. while(changing){
  96. Sleep(1);
  97. }
  98. AddAccess();
  99. int ret = current_data.count(key);
  100. if(include_pending){
  101. PendingLock.lock();
  102. ret += pending_add.count(key);
  103. PendingLock.unlock();
  104. }
  105. RemoveAccess();
  106. return ret;
  107. }
  108. void clear(bool delete_all = false){
  109. pending_clear = true;
  110. if(delete_all){
  111. SetChanging();
  112. while(access_count > 0){
  113. Sleep(1);
  114. }
  115. AddAccess();
  116. PendingLock.lock();
  117. typename std::map<KeyT, ValueT>::iterator itr;
  118. for(itr = current_data.begin(); itr != current_data.end(); itr++){
  119. deleteData(itr->first, MUTEXMAP_DELETE_TYPE_VALUE);
  120. }
  121. PendingLock.unlock();
  122. RemoveAccess();
  123. SetNotChanging();
  124. }
  125. update();
  126. }
  127. unsigned int size(bool include_pending = false){
  128. if(include_pending)
  129. return current_data.size() + pending_add.size();
  130. return current_data.size();
  131. }
  132. void deleteData(KeyT key, int8 type, int32 erase_time = 0){
  133. DeleteData<KeyT, ValueT>* del = new DeleteData<KeyT, ValueT>();
  134. del->SetData(type, key, current_data[key], Timer::GetCurrentTime2() + erase_time);
  135. pending_deletes[del] = true;
  136. has_pending_deletes = true;
  137. }
  138. void erase(KeyT key, bool erase_key = false, bool erase_value = false, int32 erase_time = 0){
  139. while(changing){
  140. Sleep(1);
  141. }
  142. AddAccess();
  143. if(current_data.count(key) != 0){
  144. PendingLock.lock();
  145. pending_remove[key] = true;
  146. if(erase_key || erase_value){
  147. int type = 0;
  148. if(erase_key)
  149. type = MUTEXMAP_DELETE_TYPE_KEY;
  150. if(erase_value)
  151. type += MUTEXMAP_DELETE_TYPE_VALUE;
  152. deleteData(key, type, erase_time);
  153. }
  154. has_pending_data = true;
  155. PendingLock.unlock();
  156. }
  157. RemoveAccess();
  158. update();
  159. }
  160. iterator begin(){
  161. return iterator(this);
  162. }
  163. void Put(KeyT key, ValueT value){
  164. while(changing){
  165. Sleep(1);
  166. }
  167. AddAccess();
  168. PendingLock.lock();
  169. pending_add[key] = value;
  170. has_pending_data = true;
  171. PendingLock.unlock();
  172. RemoveAccess();
  173. update();
  174. }
  175. ValueT& Get(KeyT key){
  176. while(changing){
  177. Sleep(1);
  178. }
  179. AddAccess();
  180. if(current_data.count(key) > 0 || pending_add.count(key) == 0){
  181. RemoveAccess();
  182. return current_data[key];
  183. }
  184. RemoveAccess();
  185. return pending_add[key];
  186. }
  187. private:
  188. void AddAccess(){
  189. AccessLock.lock();
  190. ++access_count;
  191. AccessLock.unlock();
  192. }
  193. void RemoveAccess(){
  194. AccessLock.lock();
  195. --access_count;
  196. AccessLock.unlock();
  197. }
  198. void SetChanging(){
  199. ChangingLock.lock();
  200. changing = true;
  201. }
  202. void SetNotChanging(){
  203. changing = false;
  204. ChangingLock.unlock();
  205. }
  206. void update(bool force = false){
  207. if(pending_clear && (force || access_count == 0)){
  208. SetChanging();
  209. while(access_count > 0){
  210. Sleep(1);
  211. }
  212. AddAccess();
  213. PendingLock.lock();
  214. current_data.clear();
  215. has_pending_data = (pending_add.size() > 0 || pending_remove.size() > 0);
  216. pending_clear = false;
  217. PendingLock.unlock();
  218. RemoveAccess();
  219. SetNotChanging();
  220. }
  221. if(!pending_clear && has_pending_data && (force || access_count == 0)){
  222. SetChanging();
  223. while(access_count > 0){
  224. Sleep(1);
  225. }
  226. AddAccess();
  227. PendingLock.lock();
  228. typename std::map<KeyT, bool>::iterator remove_itr;
  229. for(remove_itr = pending_remove.begin(); remove_itr != pending_remove.end(); remove_itr++){
  230. current_data.erase(remove_itr->first);
  231. }
  232. typename std::map<KeyT, ValueT>::iterator add_itr;
  233. for(add_itr = pending_add.begin(); add_itr != pending_add.end(); add_itr++){
  234. current_data[add_itr->first] = add_itr->second;
  235. }
  236. pending_add.clear();
  237. pending_remove.clear();
  238. has_pending_data = false;
  239. PendingLock.unlock();
  240. RemoveAccess();
  241. SetNotChanging();
  242. }
  243. if(has_pending_deletes && (force || (Timer::GetCurrentTime2() > next_delete_attempt && access_count == 0))){
  244. SetChanging();
  245. while(access_count > 0){
  246. Sleep(1);
  247. }
  248. AddAccess();
  249. PendingLock.lock();
  250. unsigned int time = Timer::GetCurrentTime2();
  251. typename std::list<DeleteData<KeyT, ValueT>*> deleteData;
  252. typename std::map<DeleteData<KeyT, ValueT>*, bool>::iterator remove_itr;
  253. for(remove_itr = pending_deletes.begin(); remove_itr != pending_deletes.end(); remove_itr++){
  254. if(force || time >= remove_itr->first->GetTime())
  255. deleteData.push_back(remove_itr->first);
  256. }
  257. DeleteData<KeyT, ValueT>* data = 0;
  258. typename std::list<DeleteData<KeyT, ValueT>*>::iterator remove_data_itr;
  259. for(remove_data_itr = deleteData.begin(); remove_data_itr != deleteData.end(); remove_data_itr++){
  260. data = *remove_data_itr;
  261. if((data->GetType() & MUTEXMAP_DELETE_TYPE_KEY) == MUTEXMAP_DELETE_TYPE_KEY){
  262. data->DeleteKey();
  263. }
  264. if((data->GetType() & MUTEXMAP_DELETE_TYPE_VALUE) == MUTEXMAP_DELETE_TYPE_VALUE){
  265. data->DeleteValue();
  266. }
  267. pending_deletes.erase(data);
  268. delete data;
  269. }
  270. next_delete_attempt = Timer::GetCurrentTime2() + 1000;
  271. PendingLock.unlock();
  272. RemoveAccess();
  273. SetNotChanging();
  274. has_pending_deletes = (pending_deletes.size() > 0);
  275. }
  276. }
  277. Locker PendingLock;
  278. Locker AccessLock;
  279. Locker ChangingLock;
  280. std::map<KeyT, ValueT> current_data;
  281. std::map<KeyT, ValueT> pending_add;
  282. std::map<DeleteData<KeyT, ValueT>*, bool > pending_deletes;
  283. std::map<KeyT, bool> pending_remove;
  284. volatile unsigned int next_delete_attempt;
  285. volatile int access_count;
  286. volatile bool delete_all;
  287. volatile bool changing;
  288. volatile bool has_pending_data;
  289. volatile bool has_pending_deletes;
  290. volatile bool pending_clear;
  291. };
  292. #endif