Guest User

Untitled

a guest
Dec 14th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #define GROW_BY_SIZE 16
  2.  
  3. template<class T>
  4. class LockFreeMemCache
  5. {
  6. public:
  7. LockFreeMemCache()
  8. {
  9. Head=NULL;
  10. FreeStack=NULL;
  11. AddSomeCache();
  12. }
  13. ~LockFreeMemCache()
  14. {
  15.  
  16. }
  17. T *Malloc()
  18. {
  19. T *returnValue = NULL;
  20. returnValue=Pop();
  21. while(returnValue==NULL)
  22. {
  23. AddSomeCache();
  24. returnValue=Pop();
  25. }
  26. return returnValue;
  27. }
  28. void Free(T *ptr)
  29. {
  30. Push(ptr);
  31. }
  32.  
  33. private:
  34. void AddSomeCache()
  35. {
  36. for(int i=0; i < GROW_BY_SIZE; i++)
  37. {
  38. T *tmp = new T();
  39. Push(tmp);
  40. }
  41. }
  42.  
  43. private:
  44. void Push(T *item)
  45. {
  46. Node<T> * new_node = PopNode();
  47. new_node->Data=item;
  48.  
  49. bool done = false;
  50. while(!done)
  51. {
  52. Node<T>* head = Head;
  53. new_node->Next = head;
  54. if(__sync_bool_compare_and_swap(&Head,head,new_node))
  55. {
  56. done = true;
  57. }
  58. }
  59.  
  60. }
  61. T *Pop()
  62. {
  63. T *returnValue = NULL;
  64. bool done = false;
  65. while(!done)
  66. {
  67. Node<T> * head= Head;
  68. if(Head == NULL)
  69. {
  70. done=true;
  71. }
  72. else
  73. {
  74. Node<T> *curr = head;
  75. if(__sync_bool_compare_and_swap(&Head,head,curr->Next))
  76. {
  77. done = true;
  78. returnValue = curr->Data;
  79. PushNode(curr);
  80. }
  81.  
  82. }
  83. }
  84. return returnValue;
  85. }
  86. void PushNode(Node<T> *item)
  87. {
  88. item->Next = NULL;
  89. item->Data = NULL;
  90. bool done = false;
  91. while(!done)
  92. {
  93. Node<T>* fs = FreeStack;
  94. item->Next = fs;
  95. if(__sync_bool_compare_and_swap(&FreeStack,fs,item))
  96. {
  97. done = true;
  98. }
  99. }
  100. }
  101. Node<T> *PopNode()
  102. {
  103. Node<T> *returnValue = NULL;
  104. bool done = false;
  105. while(!done)
  106. {
  107. Node<T> *fs = FreeStack;
  108. if(fs == NULL)
  109. {
  110. done=true;
  111. }
  112. else
  113. {
  114. Node<T> *curr = fs;
  115. if(__sync_bool_compare_and_swap(&FreeStack,fs,curr->Next))
  116. {
  117. done = true;
  118. returnValue = curr;
  119. }
  120.  
  121. }
  122. }
  123. if (returnValue == NULL)
  124. {
  125. returnValue = new Node<T>();
  126. returnValue->Data = NULL;
  127. returnValue->Next = NULL;
  128. }
  129. return returnValue;
  130. }
  131. Node<T> *Head;
  132. Node<T> *FreeStack;
  133. };
  134.  
  135. template<class T>
  136. class LockFreeStack
  137. {
  138. public:
  139. LockFreeStack()
  140. {
  141. Head = NULL;
  142. }
  143. ~LockFreeStack(){
  144. }
  145. void Push(T *item)
  146. {
  147. Node<T> * new_node = Cache.Malloc();
  148. new_node->Data=item;
  149.  
  150. bool done = false;
  151. while(!done)
  152. {
  153. Node<T>* head = Head;
  154. new_node->Next = head;
  155. if(__sync_bool_compare_and_swap(&Head,head,new_node))
  156. {
  157. done = true;
  158. }
  159. }
  160. }
  161.  
  162. T *Pop()
  163. {
  164. T *returnValue = NULL;
  165. bool done = false;
  166. while(!done)
  167. {
  168. Node<T> *head = Head;
  169. if(head == NULL)
  170. {
  171. done=true;
  172. }
  173. else
  174. {
  175. Node<T> *curr = head;
  176. if(__sync_bool_compare_and_swap(&Head,head,curr->Next))
  177. {
  178. done = true;
  179. returnValue = curr->Data;
  180. curr->Next =NULL;
  181. Cache.Free(curr);
  182. }
  183.  
  184. }
  185. }
  186. return returnValue;
  187. }
  188. public:
  189.  
  190. Node<T> *Head;
  191.  
  192. private:
  193. LockFreeMemCache<Node<T> > Cache;
  194. };
  195.  
  196. template<class T>
  197. class LockFreeQueue
  198. {
  199. public:
  200. LockFreeQueue()
  201. {
  202. Head=NULL;
  203. }
  204. ~LockFreeQueue(){
  205. }
  206. void Enqueue(T *item)
  207. {
  208. Node<T> * new_node = Cache.Malloc();
  209. new_node->Data=item;
  210.  
  211. bool done = false;
  212. while(!done)
  213. {
  214. Node<T>* head = Head;
  215. new_node->Next = head;
  216. if(__sync_bool_compare_and_swap(&Head,head,new_node))
  217. {
  218. done = true;
  219. }
  220. }
  221. }
  222. T *Dequeue()
  223. {
  224. T *returnValue=NULL;
  225. bool done = false;
  226. while(!done)
  227. {
  228. if(Head == NULL)
  229. {
  230. done = true;
  231. }
  232. else
  233. {
  234. volatile Node<T> *prev = NULL, *curr = Head, *head = Head;
  235. bool found = false;
  236. while(!found)
  237. {
  238. if(curr->Next == NULL)
  239. {
  240. found=true;
  241. break;
  242. }
  243. prev = curr;
  244. curr = curr->Next;
  245. }
  246. if(prev == NULL)
  247. {
  248. if(__sync_bool_compare_and_swap(&Head,head,NULL))
  249. {
  250. done = true;
  251. }
  252. }
  253. else
  254. {
  255. if(__sync_bool_compare_and_swap(&prev->Next,prev->Next,NULL))
  256. {
  257. done = true;
  258. }
  259. }
  260. if(done)
  261. {
  262. returnValue = curr->Data;
  263. curr->Data = NULL;
  264. curr->Next= NULL;
  265. Cache.Free(curr);
  266. }
  267.  
  268. }
  269. }
  270.  
  271. return returnValue;
  272. }
  273. public:
  274.  
  275.  
  276. private:
  277. Node<T> *Head;
  278. LockFreeMemCache<Node<T> > Cache;
  279. };
Add Comment
Please, Sign In to add comment