Skip to content

Commit

Permalink
Basic infrastructure has been committed
Browse files Browse the repository at this point in the history
  • Loading branch information
leventkaragol committed May 21, 2024
1 parent 8e8cde0 commit 3d840c4
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: linux

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
container:
image: ubuntu:24.04

steps:
- name: Enable core dumps
run: ulimit -c unlimited
- uses: actions/checkout@v3
- name: Install prerequisites
run: |
apt update
apt install -y build-essential g++ make cmake pkg-config git wget curl zip unzip tar gdb
- name: Install vcpkg
run: |
git clone https://github.com/microsoft/vcpkg.git /opt/vcpkg
/opt/vcpkg/bootstrap-vcpkg.sh
ln -s /opt/vcpkg/vcpkg /usr/local/bin/vcpkg
- name: Clone project repository
run: |
git clone https://github.com/leventkaragol/libcpp-channel.git /root/libcpp-channel
- name: Run cmake with vcpkg toolchain
run: |
cd /root/libcpp-channel
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/opt/vcpkg/scripts/buildsystems/vcpkg.cmake
- name: Build the project
run: |
cd /root/libcpp-channel
cmake --build build --config Release
- name: Run tests with gdb
run: |
cd /root/libcpp-channel
gdb -ex "run" -ex "bt" -ex "quit" --args ./build/test/test
61 changes: 61 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: windows

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
test:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3

- name: Set up environment
run: |
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
choco install git
choco install ninja
- name: Install vcpkg
run: |
if (Test-Path "C:\vcpkg") {
Remove-Item "C:\vcpkg" -Recurse -Force
}
git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
& "C:\vcpkg\bootstrap-vcpkg.bat"
echo "VCPKG_ROOT=C:\vcpkg" | Out-File -Append -Encoding ascii $env:GITHUB_ENV
- name: Integrate vcpkg
run: |
$env:VCPKG_ROOT = "C:\vcpkg"
& "$env:VCPKG_ROOT\vcpkg" integrate install
- name: Install dependencies
run: |
$env:VCPKG_ROOT = "C:\vcpkg"
cd $env:GITHUB_WORKSPACE
& "$env:VCPKG_ROOT\vcpkg" install
- name: Prepare build environment
run: |
$env:CMAKE_TOOLCHAIN_FILE="${env:VCPKG_ROOT}\scripts\buildsystems\vcpkg.cmake"
echo "CMAKE_TOOLCHAIN_FILE=${env:CMAKE_TOOLCHAIN_FILE}" | Out-File -Append -Encoding ascii $env:GITHUB_ENV
echo "CMAKE_TOOLCHAIN_FILE is set to ${env:CMAKE_TOOLCHAIN_FILE}"
- name: Configure CMake project
run: |
echo "Using CMAKE_TOOLCHAIN_FILE: ${env:CMAKE_TOOLCHAIN_FILE}"
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE="${env:CMAKE_TOOLCHAIN_FILE}"
- name: Build the project
run: |
cmake --build build --config Release
- name: Run tests
run: |
& "D:\a\libcpp-channel\libcpp-channel\build\test\Release\test.exe"
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@
*.exe
*.out
*.app

## JetBrains IDE artifacts
.idea
cmake-build-*

## Visual Studio artifacts
.vs
ipch
*.opensdf
*.log
*.pdb
*.ilk
*.user
*.sdf
*.suo
*.VC.db
*.VC.VC.opendb

## OSX artifacts
.DS_Store
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.14)

project(libcpp-channel)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(libcpp-channel INTERFACE)

target_include_directories(libcpp-channel INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)

add_subdirectory(examples)
add_subdirectory(test)
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.14)

project(examples)

add_executable(examples single-producer-single-consumer.cpp)

target_link_libraries(examples PRIVATE libcpp-channel)
8 changes: 8 additions & 0 deletions examples/multiple-producer-multiple-consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "libcpp-channel.hpp"

using namespace lklibs;

int main()
{
return 0;
}
8 changes: 8 additions & 0 deletions examples/multiple-producer-single-consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "libcpp-channel.hpp"

using namespace lklibs;

int main()
{
return 0;
}
8 changes: 8 additions & 0 deletions examples/single-producer-multiple-consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "libcpp-channel.hpp"

using namespace lklibs;

int main()
{
return 0;
}
55 changes: 55 additions & 0 deletions examples/single-producer-single-consumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "libcpp-channel.hpp"
#include <iostream>
#include <thread>

using namespace lklibs;

void producer(Channel<std::string>::Producer producer)
{
auto i = 0;

while (true)
{
i++;

// Sending string message to the consumer
producer.send("Message " + std::to_string(i));

std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

void consumer(Channel<std::string>::Consumer consumer)
{
while (true)
{
// Receiving message from the producer
auto message = consumer.receive();

if (message.has_value())
{
std::cout << "Received: " << message.value() << std::endl;
}
}
}

int main()
{
// Creating a string channel
Channel<std::string> channel;

// Getting producer and consumer objects
auto producer = channel.getProducer();
auto consumer = channel.getConsumer();

// Passing producer object to the first thread
std::thread producer_thread(::producer, std::move(producer));

// Passing consumer object to the second thread
std::thread consumer_thread(::consumer, std::move(consumer));

producer_thread.join();
consumer_thread.join();

return 0;
}
114 changes: 114 additions & 0 deletions src/libcpp-channel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Thread safe message channel library for C++ (17+)
version 1.0.0
https://github.com/leventkaragol/libcpp-channel
If you encounter any issues, please submit a ticket at https://github.com/leventkaragol/libcpp-channel/issues
Copyright (c) 2024 Levent KARAGÖL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef LIBCPP_CHANNEL_HPP
#define LIBCPP_CHANNEL_HPP

#include <queue>
#include <mutex>
#include <condition_variable>
#include <optional>
#include <memory>
#include <vector>

namespace lklibs {

template <typename T>
class Channel {
private:
struct Data {
std::queue<T> queue_;
std::mutex mutex_;
std::condition_variable cond_var_;
};

std::shared_ptr<Data> data_;

public:
Channel() : data_(std::make_shared<Data>()) {}

class Producer {
public:
explicit Producer(std::shared_ptr<Data> data) : data_(std::move(data)) {}

void send(T value) {

std::unique_lock<std::mutex> lock(data_->mutex_);

data_->queue_.push(std::move(value));

lock.unlock();

data_->cond_var_.notify_one();
}

private:
std::shared_ptr<Data> data_;
};

class Consumer {
public:
explicit Consumer(std::shared_ptr<Data> data) : data_(std::move(data)) {}

std::optional<T> receive() {

std::unique_lock<std::mutex> lock(data_->mutex_);

data_->cond_var_.wait(lock, [this]() { return !data_->queue_.empty(); });

if (data_->queue_.empty()) {

return std::nullopt; // Spurious wakeup protection
}

T value = std::move(data_->queue_.front());

data_->queue_.pop();

return value;
}

private:
std::shared_ptr<Data> data_;
};

Producer getProducer()
{
return Producer(data_);
}

Consumer getConsumer()
{
return Consumer(data_);
}
};
}

#endif // LIBCPP_CHANNEL_HPP
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.14)

project(test)

find_package(GTest CONFIG REQUIRED)

add_executable(test test.cpp)

target_link_libraries(test PRIVATE libcpp-channel GTest::gtest GTest::gtest_main)
16 changes: 16 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "libcpp-channel.hpp"
#include <gtest/gtest.h>

using namespace lklibs;

TEST(SPSC, SingleProducerMustSendMessageToSingleConsumerSuccessfully)
{
ASSERT_EQ(1,1);
}

int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}
9 changes: 9 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name" : "libcpp-channel",
"version-string" : "1.0.0",
"builtin-baseline" : "7eb700c9688daed6d8bdcdc571ebe3eedea6a774",
"dependencies" : [ {
"name" : "gtest",
"version>=" : "1.14.0#1"
} ]
}

0 comments on commit 3d840c4

Please sign in to comment.