Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional concurrency primitives #1555

Merged
merged 11 commits into from
Sep 5, 2022
Merged

Conversation

gebner
Copy link
Member

@gebner gebner commented Sep 1, 2022

This PR adds several additional concurrency primitives, as described in RFC #1280.

  • BaseMutex is a mutex
  • Mutex α is a mutex with guarded state α (like in Rust)
  • Condvar is a condition variable
  • Promise creates a task whose value will be set later

There's also two very unoptimized queue-like primitives building on top of these:

  • Channel with ch.recv? : BaseIO (Option α)
  • AsyncChannel with ch.recvAsync : BaseIO (Task (Option α))

The channels don't perform too badly, based on some extremly quick benchmarking results. Channel transports messages at 400kHz, AsyncChannel at 700kHz on a slow machine. This seems in the same ballpark as Rust's MPSC channels, which seem to be roughly at 900kHz (I didn't run the benchmark locally).

Maybe it's worth removing Channel and only keep the AsyncChannel as it is faster (and more general).

prelude
import Init.Data.List

namespace Std
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@digama0 I'll defer to your judgement on the naming here.

Locks a `BaseMutex`. Waits until no other thread has locked the mutex.

The current thread must not have already locked the mutex.
Reentrant locking is undefined behavior (inherited from the C++ implementation).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fun

abbrev AtomicT := StateRefT' IO.RealWorld

/-- `mutex.atomically k` runs `k` with access to the mutex's state while locking the mutex. -/
def Mutex.atomically [Monad M] [MonadLiftT BaseIO M] [MonadFinally M]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is uppercase M a Scala influence 😄 ? (As I said before I would even be in favor of switching to this naming style, but that's a different discussion)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's keep the current naming convention for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I wrote this a couple weeks back when Monad M was just gaining traction. It's uppercase because it's a type. I've changed it to m in this PR. But there's plenty of other occurrences, check out rg 'Monad M'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems they all come from @EdAyers :)

src/runtime/object.cpp Outdated Show resolved Hide resolved
5. `promise.result.get` now returns `a`

Every promise must eventually be resolved.
Otherwise the memory used for the promise will be leaked,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we skip the Deactivated state in this case? We would need more data in the lean_task_impl so that deactivate_task can identify this case I suppose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we'd need more data. Right now, a Promised task looks the same as a Running task. And Running tasks are owned by the worker (and thus can't be freed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants