/asio-mutex

Awaitable Mutex compatible with Boost.Asio

Primary LanguageC++

Async Mutex for Boost Asio

This repository contains an implementation of an asynchronous mutex for Boos Asio. Asynchronous here means that the locking operation can be co_awaited, thus avoiding potential deadlocks in multithreaded coroutine code.

Credits: the implementation of the mutex itself is heavily inspired by Lewis Bakers's async_mutex from cppcoro, published under the MIT license.

Dependencies

  • Boost >= 1.80.0 - for obvious reasons
  • Doxygen - optional, use make doc to generate doxygen documentation (in build/doc/html)
  • Catch2 - optional, use make check or make checkVerbose to run tests

CMake Options

  • -DENABLE_TESTING=ON|OFF - default ON, set to off to not build tests
  • -DSANITIZER=asan|tsan|ubsan|msan - default empty, set to one of the values to build with the given sanitizer

API

namespace avast::asio {

/**
 * A basic mutex that can acquire lock asynchronously using coroutines.
 **/
class async_mutex {
public:
    /**
     * Constructs a new, unlocked mutex.
     **/
    explicit async_mutex() noexcept;

    /**
     * Destroys the mutex.
     *
     * The mutex must be in unlocked state, otherwise the behavior is undefined.
     **/
    ~async_mutex();

    /**
     * Attempts to acquire a lock without blocking.
     *
     * Returns `true` if lock has been acquired successfuly, `false` otherwise.
     **/
    bool try_lock();

    /**
     * Asynchronously acquires a lock.
     *
     * When the returned awaitable is `co_await`ed it initiates the process
     * of acquiring a lock. The awaiter is suspended. Once the lock is acquired
     * (which can be immediately if nothing else holds the lock currently) the
     * awaiter is resumed and is now holding the lock.
     *
     * It's awaiter's responsibility to release the lock by calling `unlock()`.
     **/
    boost::asio::awaitable<> async_lock(LockToken);

    /**
     * Asynchronously acquires a lock and returns a scoped lock.
     *
     * Behaves exactly like `async_lock()`, except that the result of `co_await`ing the
     * returned awaitable is a scoped lock object, which will automatically release the
     * lock when destroyed.
     **/
    boost::asio::awaitable<avast::asio::async_mutex_lock> async_scoped_lock(LockToken);


    /**
     * Unlocks the mutex.
     *
     * Calling this method on an unlocked mutex is undefined behavior.
     **/
    void unlock();
};

/**
 * A RAII-style lock for async_mutex which automatically unlocks the mutex when destroyed.
 **/
class async_mutex_lock {
public:
    /**
     * Constructs a new async_mutex_lock, taking ownership of the `mutex`.
     *
     * The mutex must be in a locked state.
     **/
    explicit async_mutex_lock(async_mutex &mutex, std::adopt_lock_t) noexcept;

    async_mutex_lock(async_mutex_lock &&) noexcept;

    async_mutex_lock(const async_mutex_lock &) = delete;
    async_mutex_lock &operator=(const async_mutex_lock &) = delete;

    /**
     * Unlocks the held mutex and destroys the lock.
     **/
    ~async_mutex_lock();
};

Authors

License

All code is licensed under Boost Software License 1.0

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.