-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,859 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014-2020 Ole Christian Eidheim | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef SIMPLE_WEB_ASIO_COMPATIBILITY_HPP | ||
#define SIMPLE_WEB_ASIO_COMPATIBILITY_HPP | ||
|
||
#include <memory> | ||
|
||
#ifdef ASIO_STANDALONE | ||
#include <asio.hpp> | ||
#include <asio/steady_timer.hpp> | ||
namespace SimpleWeb { | ||
namespace error = asio::error; | ||
using error_code = std::error_code; | ||
using errc = std::errc; | ||
using system_error = std::system_error; | ||
namespace make_error_code = std; | ||
} // namespace SimpleWeb | ||
#else | ||
#include <boost/asio.hpp> | ||
#include <boost/asio/steady_timer.hpp> | ||
namespace SimpleWeb { | ||
namespace asio = boost::asio; | ||
namespace error = asio::error; | ||
using error_code = boost::system::error_code; | ||
namespace errc = boost::system::errc; | ||
using system_error = boost::system::system_error; | ||
namespace make_error_code = boost::system::errc; | ||
} // namespace SimpleWeb | ||
#endif | ||
|
||
namespace SimpleWeb { | ||
#if(ASIO_STANDALONE && ASIO_VERSION >= 101300) || BOOST_ASIO_VERSION >= 101300 | ||
using io_context = asio::io_context; | ||
using resolver_results = asio::ip::tcp::resolver::results_type; | ||
using async_connect_endpoint = asio::ip::tcp::endpoint; | ||
|
||
template <typename handler_type> | ||
inline void post(io_context &context, handler_type &&handler) { | ||
asio::post(context, std::forward<handler_type>(handler)); | ||
} | ||
inline void restart(io_context &context) noexcept { | ||
context.restart(); | ||
} | ||
inline asio::ip::address make_address(const std::string &str) noexcept { | ||
return asio::ip::make_address(str); | ||
} | ||
template <typename socket_type, typename duration_type> | ||
std::unique_ptr<asio::steady_timer> make_steady_timer(socket_type &socket, std::chrono::duration<duration_type> duration) { | ||
return std::unique_ptr<asio::steady_timer>(new asio::steady_timer(socket.get_executor(), duration)); | ||
} | ||
template <typename handler_type> | ||
void async_resolve(asio::ip::tcp::resolver &resolver, const std::pair<std::string, std::string> &host_port, handler_type &&handler) { | ||
resolver.async_resolve(host_port.first, host_port.second, std::forward<handler_type>(handler)); | ||
} | ||
inline asio::executor_work_guard<io_context::executor_type> make_work_guard(io_context &context) { | ||
return asio::make_work_guard(context); | ||
} | ||
#else | ||
using io_context = asio::io_service; | ||
using resolver_results = asio::ip::tcp::resolver::iterator; | ||
using async_connect_endpoint = asio::ip::tcp::resolver::iterator; | ||
|
||
template <typename handler_type> | ||
inline void post(io_context &context, handler_type &&handler) { | ||
context.post(std::forward<handler_type>(handler)); | ||
} | ||
inline void restart(io_context &context) noexcept { | ||
context.reset(); | ||
} | ||
inline asio::ip::address make_address(const std::string &str) noexcept { | ||
return asio::ip::address::from_string(str); | ||
} | ||
template <typename socket_type, typename duration_type> | ||
std::unique_ptr<asio::steady_timer> make_steady_timer(socket_type &socket, std::chrono::duration<duration_type> duration) { | ||
return std::unique_ptr<asio::steady_timer>(new asio::steady_timer(socket.get_io_service(), duration)); | ||
} | ||
template <typename handler_type> | ||
void async_resolve(asio::ip::tcp::resolver &resolver, const std::pair<std::string, std::string> &host_port, handler_type &&handler) { | ||
resolver.async_resolve(asio::ip::tcp::resolver::query(host_port.first, host_port.second), std::forward<handler_type>(handler)); | ||
} | ||
inline io_context::work make_work_guard(io_context &context) { | ||
return io_context::work(context); | ||
} | ||
#endif | ||
} // namespace SimpleWeb | ||
|
||
#endif /* SIMPLE_WEB_ASIO_COMPATIBILITY_HPP */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Based on https://clang.llvm.org/docs/ThreadSafetyAnalysis.html | ||
#ifndef SIMPLE_WEB_MUTEX_HPP | ||
#define SIMPLE_WEB_MUTEX_HPP | ||
|
||
#include <mutex> | ||
|
||
// Enable thread safety attributes only with clang. | ||
#if defined(__clang__) && (!defined(SWIG)) | ||
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) | ||
#else | ||
#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op | ||
#endif | ||
|
||
#define CAPABILITY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) | ||
|
||
#define SCOPED_CAPABILITY \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) | ||
|
||
#define GUARDED_BY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) | ||
|
||
#define PT_GUARDED_BY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) | ||
|
||
#define ACQUIRED_BEFORE(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) | ||
|
||
#define ACQUIRED_AFTER(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) | ||
|
||
#define REQUIRES(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) | ||
|
||
#define REQUIRES_SHARED(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) | ||
|
||
#define ACQUIRE(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) | ||
|
||
#define ACQUIRE_SHARED(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) | ||
|
||
#define RELEASE(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) | ||
|
||
#define RELEASE_SHARED(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) | ||
|
||
#define TRY_ACQUIRE(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) | ||
|
||
#define TRY_ACQUIRE_SHARED(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) | ||
|
||
#define EXCLUDES(...) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) | ||
|
||
#define ASSERT_CAPABILITY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) | ||
|
||
#define ASSERT_SHARED_CAPABILITY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) | ||
|
||
#define RETURN_CAPABILITY(x) \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) | ||
|
||
#define NO_THREAD_SAFETY_ANALYSIS \ | ||
THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) | ||
|
||
namespace SimpleWeb { | ||
/// Mutex class that is annotated for Clang Thread Safety Analysis. | ||
class CAPABILITY("mutex") Mutex { | ||
std::mutex mutex; | ||
|
||
public: | ||
void lock() ACQUIRE() { | ||
mutex.lock(); | ||
} | ||
|
||
void unlock() RELEASE() { | ||
mutex.unlock(); | ||
} | ||
}; | ||
|
||
/// Scoped mutex guard class that is annotated for Clang Thread Safety Analysis. | ||
class SCOPED_CAPABILITY LockGuard { | ||
Mutex &mutex; | ||
bool locked = true; | ||
|
||
public: | ||
LockGuard(Mutex &mutex_) ACQUIRE(mutex_) : mutex(mutex_) { | ||
mutex.lock(); | ||
} | ||
void unlock() RELEASE() { | ||
mutex.unlock(); | ||
locked = false; | ||
} | ||
~LockGuard() RELEASE() { | ||
if(locked) | ||
mutex.unlock(); | ||
} | ||
}; | ||
|
||
} // namespace SimpleWeb | ||
|
||
#endif // SIMPLE_WEB_MUTEX_HPP |
Oops, something went wrong.