Coder Social home page Coder Social logo

asyncextensions's Introduction

AsyncExtensions

This is an experimental repo for solving efficient recursive async locking in .NET.

Example: Simple async locking

var mutex = new AsyncLock();

using (await mutex.LockAsync()) 
{
    // ...
}

Example: Recursive async locking

Let me know if you have a solution for flowing back the execution context to the awaiter, without an additional method call.

var mutex = new AsyncLock(new AsyncLockOptions { AllowReentrancy = true });

await Outer();

async ValueTask Outer() 
{
    using ((await mutex.LockAsync()).WithReentrancy()) 
    {
        await Inner();
    }
}

async ValueTask Inner()
{
    using (await mutex.LockAsync())
    {
        // ...
    }
}

Example: Recursive async locking with shorthand syntax

Here you don't call LockAsync at all, the lock itself wraps your method that wants to acquire it, allowing reentrancy if configured.

var mutex = new AsyncLock(new AsyncLockOptions { AllowReentrancy = true });

await mutex.RunAsync(Outer);

async ValueTask Outer() 
{
    await mutex.RunAsync(Inner);
}

async ValueTask Inner()
{
    await mutex.RunAsync(
        () => 
        {
            // ...
        });
}

Example: Recursive async locking using a token

This method is faster and allocates zero memory on the happy path (when the lock is not already owned by someone else).

var mutex = new AsyncLock();

await Outer();

async ValueTask Outer() 
{
    using (var token = await mutex.LockAsync()) 
    {
        await Inner(token);
    }
}

async ValueTask Inner(AsyncLockToken token = default)
{
    using (await mutex.LockAsync(token))
    {
        // ...
    }
}

asyncextensions's People

Contributors

balassamarton avatar

Stargazers

Victor Lee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.