-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordermap.h
32 lines (30 loc) · 835 Bytes
/
ordermap.h
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
#pragma once
#include "order.h"
#include <thread>
#include <vector>
#include <array>
/**
* @brief lock-free map of external order ID -> Order
*/
class OrderMap {
static const int TABLE_SIZE = 1000000;
std::atomic<Order*> table[TABLE_SIZE];
public:
void add(Order *order){
int bucket = order->exchangeId % TABLE_SIZE;
Order* expected = table[bucket].load();
order->next = expected;
while(!table[bucket].compare_exchange_weak(expected,order)) {
order->next = expected;
}
}
Order* get(long exchangeId) {
int bucket = exchangeId % TABLE_SIZE;
Order *order = table[bucket].load();
while(order!=nullptr) {
if(order->exchangeId==exchangeId) return order;
order = order->next;
}
return nullptr;
}
};