Skip to content

Commit

Permalink
Version 0.1.3 (#53)
Browse files Browse the repository at this point in the history
 - `resume_on` improvements
 - `binary_semaphore` polyfill for appleclang 
-  enable dynamic linking on Windows
- typo fixes

Co-authored-by: chausner <[email protected]>
Co-authored-by: autoantwort <[email protected]>
  • Loading branch information
3 people authored Sep 8, 2021
1 parent cbfc4a1 commit 93890e3
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 95 deletions.
19 changes: 10 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)

project(concurrencpp
VERSION 0.1.2
VERSION 0.1.3
LANGUAGES CXX)

include(cmake/coroutineOptions.cmake)
Expand All @@ -15,6 +15,7 @@ if(concurrencpp_INCLUDE_WITHOUT_SYSTEM)
endif()

# ---- Declare library ----
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)

set(concurrencpp_sources
source/task.cpp
Expand All @@ -28,6 +29,7 @@ set(concurrencpp_sources
source/results/impl/shared_result_state.cpp
source/results/promises.cpp
source/runtime/runtime.cpp
source/threads/binary_semaphore.cpp
source/threads/thread.cpp
source/timers/timer.cpp
source/timers/timer_queue.cpp)
Expand All @@ -36,7 +38,7 @@ set(concurrencpp_headers
include/concurrencpp/concurrencpp.h
include/concurrencpp/errors.h
include/concurrencpp/task.h
include/concurrencpp/forward_declerations.h
include/concurrencpp/forward_declarations.h
include/concurrencpp/platform_defs.h
include/concurrencpp/coroutines/coroutine.h
include/concurrencpp/executors/constants.h
Expand All @@ -62,18 +64,19 @@ set(concurrencpp_headers
include/concurrencpp/results/shared_result.h
include/concurrencpp/results/result_awaitable.h
include/concurrencpp/results/shared_result_awaitable.h
include/concurrencpp/results/result_fwd_declerations.h
include/concurrencpp/results/result_fwd_declarations.h
include/concurrencpp/results/when_result.h
include/concurrencpp/results/resume_on.h
include/concurrencpp/runtime/constants.h
include/concurrencpp/runtime/runtime.h
include/concurrencpp/threads/binary_semaphore.h
include/concurrencpp/threads/thread.h
include/concurrencpp/timers/constants.h
include/concurrencpp/timers/timer.h
include/concurrencpp/timers/timer_queue.h
include/concurrencpp/utils/bind.h)

add_library(concurrencpp STATIC ${concurrencpp_headers} ${concurrencpp_sources})
add_library(concurrencpp ${concurrencpp_headers} ${concurrencpp_sources})
add_library(concurrencpp::concurrencpp ALIAS concurrencpp)

target_include_directories(concurrencpp
Expand All @@ -98,11 +101,9 @@ set(concurrencpp_include_directory "${CMAKE_INSTALL_INCLUDEDIR}/${concurrencpp_d

install(TARGETS concurrencpp
EXPORT concurrencppTargets
ARCHIVE #
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
COMPONENT concurrencpp_Development
INCLUDES #
DESTINATION "${concurrencpp_include_directory}")
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT concurrencpp_Development
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT concurrencpp_Development
INCLUDES DESTINATION "${concurrencpp_include_directory}")

set(concurrencpp_install_cmakedir
"${CMAKE_INSTALL_LIBDIR}/cmake/${concurrencpp_directory}")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ Task objects apply the short-buffer-optimization (sbo) for regular, small callab
/*
Returns true if *this stores a callable. false otherwise.
*/
expliit operator bool() const noexcept;
explicit operator bool() const noexcept;
/*
Returns true if *this stores a callable,
Expand Down
2 changes: 1 addition & 1 deletion include/concurrencpp/concurrencpp.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONCURRENCPP_H
#define CONCURRENCPP_H

#include "concurrencpp/forward_declerations.h"
#include "concurrencpp/forward_declarations.h"
#include "concurrencpp/platform_defs.h"

#include "concurrencpp/timers/timer.h"
Expand Down
4 changes: 2 additions & 2 deletions include/concurrencpp/executors/thread_pool_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define CONCURRENCPP_THREAD_POOL_EXECUTOR_H

#include "concurrencpp/threads/thread.h"
#include "concurrencpp/threads/binary_semaphore.h"
#include "concurrencpp/executors/derivable_executor.h"

#include <deque>
#include <mutex>
#include <semaphore>

namespace concurrencpp::details {
class idle_worker_set {
Expand Down Expand Up @@ -49,7 +49,7 @@ namespace concurrencpp::details {
const std::string m_worker_name;
alignas(64) std::mutex m_lock;
std::deque<task> m_public_queue;
std::binary_semaphore m_semaphore;
binary_semaphore m_semaphore;
bool m_idle;
bool m_abort;
std::atomic_bool m_event_found;
Expand Down
4 changes: 2 additions & 2 deletions include/concurrencpp/executors/worker_thread_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define CONCURRENCPP_WORKER_THREAD_EXECUTOR_H

#include "concurrencpp/threads/thread.h"
#include "concurrencpp/threads/binary_semaphore.h"
#include "concurrencpp/executors/derivable_executor.h"

#include <deque>
#include <mutex>
#include <semaphore>

namespace concurrencpp {
class alignas(64) worker_thread_executor final : public derivable_executor<worker_thread_executor> {
Expand All @@ -17,7 +17,7 @@ namespace concurrencpp {
details::thread m_thread;
alignas(64) std::mutex m_lock;
std::deque<task> m_public_queue;
std::binary_semaphore m_semaphore;
details::binary_semaphore m_semaphore;
std::atomic_bool m_atomic_abort;
bool m_abort;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CONCURRENCPP_FORWARD_DECLERATIONS_H
#define CONCURRENCPP_FORWARD_DECLERATIONS_H
#ifndef CONCURRENCPP_FORWARD_DECLARATIONS_H
#define CONCURRENCPP_FORWARD_DECLARATIONS_H

namespace concurrencpp {
struct null_result;
Expand Down Expand Up @@ -29,4 +29,4 @@ namespace concurrencpp {
class manual_executor;
} // namespace concurrencpp

#endif // FORWARD_DECLERATIONS_H
#endif // FORWARD_DECLARATIONS_H
6 changes: 6 additions & 0 deletions include/concurrencpp/platform_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
# define CRCPP_DEBUG_MODE
#endif

#include <exception>

#if defined(_LIBCPP_VERSION)
# define CRCPP_LIBCPP_LIB
#endif

#endif // PLATFORM_DEFS_H
2 changes: 1 addition & 1 deletion include/concurrencpp/results/impl/consumer_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define CONCURRENCPP_CONSUMER_CONTEXT_H

#include "concurrencpp/coroutines/coroutine.h"
#include "concurrencpp/results/result_fwd_declerations.h"
#include "concurrencpp/results/result_fwd_declarations.h"

#include <mutex>
#include <condition_variable>
Expand Down
2 changes: 1 addition & 1 deletion include/concurrencpp/results/impl/lazy_result_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "concurrencpp/coroutines/coroutine.h"
#include "concurrencpp/results/impl/producer_context.h"
#include "concurrencpp/results/result_fwd_declerations.h"
#include "concurrencpp/results/result_fwd_declarations.h"

namespace concurrencpp::details {
struct lazy_final_awaiter : public suspend_always {
Expand Down
2 changes: 1 addition & 1 deletion include/concurrencpp/results/impl/producer_context.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONCURRENCPP_PRODUCER_CONTEXT_H
#define CONCURRENCPP_PRODUCER_CONTEXT_H

#include "concurrencpp/results/result_fwd_declerations.h"
#include "concurrencpp/results/result_fwd_declarations.h"

#include <exception>

Expand Down
2 changes: 1 addition & 1 deletion include/concurrencpp/results/impl/shared_result_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define CONCURRENCPP_SHARED_RESULT_STATE_H

#include "concurrencpp/coroutines/coroutine.h"
#include "concurrencpp/forward_declerations.h"
#include "concurrencpp/forward_declarations.h"
#include "concurrencpp/results/impl/producer_context.h"
#include "concurrencpp/results/impl/return_value_struct.h"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONCURRENCPP_RESULT_FWD_DECLERATIONS_H
#define CONCURRENCPP_RESULT_FWD_DECLERATIONS_H
#ifndef CONCURRENCPP_RESULT_FWD_DECLARATIONS_H
#define CONCURRENCPP_RESULT_FWD_DECLARATIONS_H

#include "concurrencpp/forward_declerations.h"
#include "concurrencpp/forward_declarations.h"
#include "concurrencpp/coroutines/coroutine.h"

#include <memory>
Expand Down
12 changes: 11 additions & 1 deletion include/concurrencpp/results/resume_on.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "concurrencpp/executors/executor.h"
#include "concurrencpp/results/impl/consumer_context.h"

#include <type_traits>

namespace concurrencpp::details {
template<class executor_type>
class resume_on_awaitable : public suspend_always {
Expand All @@ -23,7 +25,12 @@ namespace concurrencpp::details {

void await_suspend(coroutine_handle<void> handle) {
m_await_ctx.set_coro_handle(handle);
m_executor.template post<await_via_functor>(&m_await_ctx);

try {
m_executor.template post<await_via_functor>(&m_await_ctx);
} catch (...) {
// the exception caused the enqeueud task to be broken and resumed with an interrupt, no need to do anything here.
}
}

void await_resume() const {
Expand All @@ -35,6 +42,9 @@ namespace concurrencpp::details {
namespace concurrencpp {
template<class executor_type>
auto resume_on(std::shared_ptr<executor_type> executor) {
static_assert(std::is_base_of_v<concurrencpp::executor, executor_type>,
"concurrencpp::resume_on() - given executor does not derive from concurrencpp::executor");

if (!static_cast<bool>(executor)) {
throw std::invalid_argument(details::consts::k_resume_on_null_exception_err_msg);
}
Expand Down
2 changes: 1 addition & 1 deletion include/concurrencpp/runtime/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace concurrencpp::details::consts {

constexpr static unsigned int k_concurrencpp_version_major = 0;
constexpr static unsigned int k_concurrencpp_version_minor = 1;
constexpr static unsigned int k_concurrencpp_version_revision = 2;
constexpr static unsigned int k_concurrencpp_version_revision = 3;
} // namespace concurrencpp::details::consts

#endif
2 changes: 1 addition & 1 deletion include/concurrencpp/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define CONCURRENCPP_RUNTIME_H

#include "concurrencpp/runtime/constants.h"
#include "concurrencpp/forward_declerations.h"
#include "concurrencpp/forward_declarations.h"

#include <memory>
#include <mutex>
Expand Down
59 changes: 59 additions & 0 deletions include/concurrencpp/threads/binary_semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef CONCURRENCPP_BINARY_SEMAPHORE_H
#define CONCURRENCPP_BINARY_SEMAPHORE_H

#include "concurrencpp/platform_defs.h"

#if defined(CRCPP_MAC_OS) && defined(CRCPP_LIBCPP_LIB)

# include <mutex>
# include <chrono>
# include <condition_variable>

# include <cstddef>

namespace concurrencpp::details {

class binary_semaphore {

private:
std::mutex m_lock;
std::condition_variable m_condition;
bool m_is_signaled;

bool try_acquire_until_impl(const std::chrono::time_point<std::chrono::system_clock>& abs_time);

public:
binary_semaphore(std::ptrdiff_t desired);

void release(std::ptrdiff_t update = 1);
void acquire();
bool try_acquire() noexcept;

template<class Rep, class Period>
bool try_acquire_for(const std::chrono::duration<Rep, Period>& rel_time) {
const auto deadline = std::chrono::system_clock::now() + rel_time;
return try_acquire_until_impl(deadline);
}

template<class Clock, class Duration>
bool try_acquire_until(const std::chrono::time_point<Clock, Duration>& abs_time) {
const auto src_now = Clock::now();
const auto dst_now = std::chrono::system_clock::now();
const auto deadline = dst_now + std::chrono::duration_cast<std::chrono::milliseconds>(abs_time - src_now);
return try_acquire_until_impl(deadline);
}
};

} // namespace concurrencpp::details

#else

# include <semaphore>

namespace concurrencpp::details {
using binary_semaphore = std::binary_semaphore;
}

#endif

#endif
2 changes: 1 addition & 1 deletion include/concurrencpp/timers/timer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONCURRENCPP_TIMER_H
#define CONCURRENCPP_TIMER_H

#include "concurrencpp/forward_declerations.h"
#include "concurrencpp/forward_declarations.h"

#include <atomic>
#include <memory>
Expand Down
59 changes: 59 additions & 0 deletions source/threads/binary_semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "concurrencpp/threads/binary_semaphore.h"

#if defined(CRCPP_MAC_OS) && defined(CRCPP_LIBCPP_LIB)

# include <cassert>

using concurrencpp::details::binary_semaphore;

binary_semaphore::binary_semaphore(std::ptrdiff_t desired) : m_is_signaled(desired != 0) {}

void binary_semaphore::release(std::ptrdiff_t update) {
auto was_signaled = false;

{
std::unique_lock<std::mutex> lock(m_lock);
was_signaled = m_is_signaled;
m_is_signaled = true;
}

if (!was_signaled) {
m_condition.notify_one();
}
}

void binary_semaphore::acquire() {
std::unique_lock<std::mutex> lock(m_lock);
m_condition.wait(lock, [this] {
return m_is_signaled;
});

assert(m_is_signaled);
m_is_signaled = false;
}

bool binary_semaphore::try_acquire() noexcept {
std::unique_lock<std::mutex> lock(m_lock);
if (m_is_signaled) {
m_is_signaled = false;
return true;
}

return false;
}

bool binary_semaphore::try_acquire_until_impl(const std::chrono::time_point<std::chrono::system_clock>& abs_time) {
std::unique_lock<std::mutex> lock(m_lock);
m_condition.wait_until(lock, abs_time, [this] {
return m_is_signaled;
});

if (m_is_signaled) {
m_is_signaled = false;
return true;
}

return false;
}

#endif
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ add_test(NAME make_result_tests PATH source/tests/result_tests/make_result_tests
add_test(NAME result_promise_tests PATH source/tests/result_tests/result_promise_tests.cpp)
add_test(NAME when_all_tests PATH source/tests/result_tests/when_all_tests.cpp)
add_test(NAME when_any_tests PATH source/tests/result_tests/when_any_tests.cpp)
add_test(NAME resume_on PATH source/tests/result_tests/resume_on_tests.cpp)
add_test(NAME resume_on_tests PATH source/tests/result_tests/resume_on_tests.cpp)

add_test(NAME coroutine_promise_tests PATH source/tests/coroutine_tests/coroutine_promise_tests.cpp)
add_test(NAME coroutine_tests PATH source/tests/coroutine_tests/coroutine_tests.cpp)
Expand Down
Loading

0 comments on commit 93890e3

Please sign in to comment.