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

The concept of mutexes and locks #499

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

The concept of mutexes and locks #499

wants to merge 2 commits into from

Conversation

roxblnfk
Copy link
Collaborator

@roxblnfk roxblnfk commented Sep 9, 2024

Related to #462

Several methods have been added to the Workflow facade:

Workflow::mutex(): MutexInterface creates a mutex. The mutex can be yielded from a Workflow, meaning execution will continue if the mutex is not locked.

The Mutex class has a lock() method that returns a Promise. This means yield $mutex->lock() will wait for the lock is acquired.

The Mutex class also has tryLock(), unlock(), and isLocked() methods, which do not imply Await behavior.


Additional syntactic sugar:

Run locked

Workflow::runLocked(MutexInterface $mutex, callable $callable): PromiseInterface

Executes a function if the given Mutex is not locked and locks the Mutex for the duration of the function's execution.

Copy link

vercel bot commented Sep 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
php ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 10, 2024 11:06am

@roxblnfk roxblnfk linked an issue Sep 9, 2024 that may be closed by this pull request
@roxblnfk roxblnfk added the Feature New feature or request label Sep 9, 2024
@wolfy-j
Copy link
Collaborator

wolfy-j commented Sep 9, 2024

Maybe we can make name optional?

@wolfy-j
Copy link
Collaborator

wolfy-j commented Sep 9, 2024

I would avoid having conditional mutexes, we can always achieve that by using await + mutex

@Sushisource
Copy link
Member

Sushisource commented Sep 9, 2024

Maybe we can make name optional?

I would avoid having conditional mutexes, we can always achieve that by using await + mutex

+1 to both of these. I was going to say don't do name at all, but then I saw PHP's built-in mutex takes a name which I find really odd, but, I agree it makes sense to match that API.

As for the conditional, yeah, user can always use await & we haven't provided that in other langs either so we can stick with the basic APIs.

The only other thing I would add is a tryLock method which the other SDKs support.

@roxblnfk
Copy link
Collaborator Author

I was going to say don't do name at all, but then I saw PHP's built-in mutex takes a name which I find really odd

We can definitely get rid of the name. SyncMutex is not a PHP standard; it's just a third-party extension, and we don't have to consider it.

@roxblnfk
Copy link
Collaborator Author

roxblnfk commented Sep 10, 2024

PR updated according to the review


If we don't use the mutex name, we can simplify everything:

  • Remove the MutexInterface
  • Remove the Workflow::mutex() method, and create Mutex using the constructor new Mutex()

@Sushisource
Copy link
Member

This approach is looking good to me 👍

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

Successfully merging this pull request may close these issues.

Workflow-friendly concurrency control
3 participants