Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add github workflow #12

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CMake

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Debug

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DENABLE_TESTING=ON

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
# Build your program with the given configuration
run: |
cd build
ctest -C ${{env.BUILD_TYPE}} -L test --force-new-test-process -V --timeout 1800
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ set(CPP_ORDERBOOK orderbook)
project(${CPP_ORDERBOOK} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -std=c++20 -stdlib=libc++ -lc++abi")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -stdlib=libc++")


set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY ON)
Expand Down
9 changes: 5 additions & 4 deletions include/orderbook.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <atomic>
#include <cstdint>
#include <map>
#include <sstream>
Expand Down Expand Up @@ -48,9 +49,9 @@ class OrderBook {

Notification& notification_;

std::atomic_uint64_t last_token_ = 0;
std::atomic<uint64_t> last_token_ = 0;

std::atomic_uint64_t matching_ = 1;
std::atomic<uint64_t> matching_ = 1;

Decimal cancelOrder(OrderID id);
void addTrigOrder(OrderID id, Type type, Side side, Decimal qty, Decimal price, Decimal trigPrice, Flag flag);
Expand All @@ -63,7 +64,7 @@ class OrderBook {
template <class Notification>
void OrderBook<Notification>::addOrder(uint64_t tok, OrderID id, Type type, Side side, Decimal qty, Decimal price, Decimal trigPrice, Flag flag) {
uint64_t exp = tok - 1; // technically this should always be single threaded, but just in case.
if (!last_token_.compare_exchange_strong(exp, tok)) {
if (!last_token_.compare_exchange_strong(exp, tok, std::memory_order_acq_rel, std::memory_order_acquire)) {
throw std::invalid_argument("invalid token received: cannot maintain determinism");
}

Expand All @@ -72,7 +73,7 @@ void OrderBook<Notification>::addOrder(uint64_t tok, OrderID id, Type type, Side
return;
}

if (!matching_) {
if (!matching_.load(std::memory_order_acquire)) {
if (type == Type::Market) {
notification_.putOrder(MsgType::CreateOrder, OrderStatus::Rejected, id, qty, Error::NoMatching);
}
Expand Down
11 changes: 6 additions & 5 deletions include/orderqueue.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <functional>

#include "boost/intrusive/set_hook.hpp"
#include "order.hpp"
Expand All @@ -19,12 +20,12 @@ class OrderQueue : public boost::intrusive::set_base_hook<boost::intrusive::opti
uint64_t size_ = 0;

public:
OrderQueue(Decimal price) : price_(price){};
Decimal price() const;
OrderQueue(const Decimal &price) : price_(price){};
[[nodiscard]] Decimal price() const;
uint64_t len();
Decimal totalQty() const;
Order *head() const;
Order *tail() const;
[[nodiscard]] Decimal totalQty() const;
[[nodiscard]] Order *head() const;
[[nodiscard]] Order *tail() const;
void append(Order *o);
void remove(Order *o);
Decimal process(const TradeNotification &tn, const PostOrderFill &postFill, OrderID takerOrderID, Decimal qty);
Expand Down
8 changes: 4 additions & 4 deletions test/orderbook_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LimitOrderTest : public ::testing::Test {
protected:
Notification n;
std::shared_ptr<OrderBook<Notification>> ob;
std::atomic_uint64_t tok = 0;
std::atomic<uint64_t> tok = 0;

void SetUp() override {
tok = 0;
Expand All @@ -19,7 +19,7 @@ class LimitOrderTest : public ::testing::Test {

void TearDown() override {}

void processLine(std::shared_ptr<OrderBook<Notification>> ob, const std::string& line) {
void processLine(std::shared_ptr<OrderBook<Notification>>& ob, const std::string& line) {
std::vector<std::string> parts;
boost::split(parts, line, boost::is_any_of("\t"));
if (parts.empty()) {
Expand All @@ -44,7 +44,7 @@ class LimitOrderTest : public ::testing::Test {
ob->addOrder(++tok, oid, type, side, qty, price, trigPrice, flag);
}

void processOrders(std::shared_ptr<OrderBook<Notification>> ob, const std::string& input, int prefix) {
void processOrders(std::shared_ptr<OrderBook<Notification>>& ob, const std::string& input, int prefix) {
std::stringstream ss(input);
std::string line;

Expand All @@ -60,7 +60,7 @@ class LimitOrderTest : public ::testing::Test {
}
}

void addDepth(std::shared_ptr<OrderBook<Notification>> ob, int prefix = 0) {
void addDepth(std::shared_ptr<OrderBook<Notification>>& ob, int prefix = 0) {
const static std::string depth = R"(
# add depth to the orderbook
1 L B 2 50 0 N
Expand Down