Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Resolves #1511. We actually didn't keep track of the number of permits before because it didn't matter as long as we started out with a certain number and didn't leak them. I added a value to record the number of permits. Change required was very minimal.

Only other thing to think about is in the case of TRef I had to abandon it extending AnyVal because now it has two parameters. We could try go put the value in the TRef instead or put them both in a case class but I think we lose as much as we gain that way.

@adamgfraser adamgfraser changed the title Implement Semaphore#Permits and TSemaphore#Permits Implement Semaphore#permits and TSemaphore#permits Aug 30, 2019
@adamgfraser adamgfraser changed the title Implement Semaphore#permits and TSemaphore#permits Prototype Safe Semaphore Sep 14, 2019
@adamgfraser
Copy link
Contributor Author

This could use some feedback if we want to do anything on it for the 1.0 release.

semaphore <- Semaphore.make(n)
available <- IO.foreach((0L until n).toList)(_ => semaphore.acquire) *> semaphore.available
} yield available must_=== 0)
available <- IO.foreach((0L until n).toList)(_ => semaphore.withPermit(semaphore.available))
Copy link
Member

Choose a reason for hiding this comment

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

Makes me want to add a semaphore.withAllPermits

Copy link
Member

Choose a reason for hiding this comment

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

... nothing to do with the test though, just thought that one might want a monopoly over all permits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if we went with this approach we could definitely maintain a val with the total number of permits and expose a withAllPermits method.

---

A `Semaphore` datatype which allows synchronization between fibers with `acquire` and `release` operations.
A `Semaphore` datatype which allows synchronization between fibers with the `withPermit` operation, which safely acquires and releases a permit.
Copy link
Member

Choose a reason for hiding this comment

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

Note that it is still possible to trick the system by asking for a negative number of permits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't assertNonNegative cause the fiber to die at that point?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, on release, which seems wrong 🤔 It's like catching a bad guy when it's too late 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Yes we could definitely fix that.

@regiskuckaertz
Copy link
Member

You have my vote, I'd just like to clarify the behaviour when passing a negative number (right now prepare deals with the case where n == 0)

@adamgfraser
Copy link
Contributor Author

@regiskuckaertz I added a fix for this.

regiskuckaertz
regiskuckaertz previously approved these changes Sep 21, 2019
Copy link
Member

@regiskuckaertz regiskuckaertz left a comment

Choose a reason for hiding this comment

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

I love it. I think I would move the assertNonNegative out of prepare and release now, as ostensibly the latter would never get evaluated anyway.

@adamgfraser
Copy link
Contributor Author

@regiskuckaertz Sorry, don't we need it in prepare? Agree we can move it out of releaseN.

regiskuckaertz
regiskuckaertz previously approved these changes Sep 21, 2019
Copy link
Member

@regiskuckaertz regiskuckaertz left a comment

Choose a reason for hiding this comment

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

Yes of course 🤦‍♂️ This is great!

@adamgfraser
Copy link
Contributor Author

@jdegoes When you have the chance could I get some feedback on whether you want to move forward with this? If so I think the next steps would be adding a couple of convenience methods on Semaphore (maxPermits and withAllPermits) and deciding how, if at all, we want to change TSemaphore based on this.

@adamgfraser adamgfraser requested a review from jdegoes September 23, 2019 19:50
* If the specified number of permits are not available, the fiber invoking
* this method will be suspended until the permits are available.
*
* Ported from @mpilquist work in Cats Effect (https://github.com/typelevel/cats-effect/pull/403)
Copy link
Member

Choose a reason for hiding this comment

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

We should preserve this attribution comment in any case.

/**
* Releases a single permit.
*/
final def release: UIO[Unit] = releaseN(1)
Copy link
Member

Choose a reason for hiding this comment

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

Because code uses these functions, we should at least deprecate them until 1.0.

@adamgfraser
Copy link
Contributor Author

I incorporated your comments. The changes are pretty small at this point, just deprecating the methods on Semaphore and updating the tests to note use those methods, but good to deprecate them now if we are going to take them out. This is ready for another review.

jdegoes
jdegoes previously approved these changes Nov 2, 2019
@adamgfraser
Copy link
Contributor Author

@jdegoes Sorry could you approve one more time? Had to resolve a merge conflict.

@adamgfraser adamgfraser merged commit 0c32182 into zio:master Nov 7, 2019
@adamgfraser adamgfraser deleted the 1511 branch November 7, 2019 16:38
@adamgfraser
Copy link
Contributor Author

@regiskuckaertz Thanks!

Twizty pushed a commit to Twizty/zio that referenced this pull request Nov 13, 2019
* implement permits

* make TRef state private

* prototype safe semaphore

* implement permits

* make TRef state private

* zio#1219 - add doUntilEquals and doWhileEquals methods (zio#1634)

* Update zio-interop-reactivestreams to 1.0.3.1-RC1 (zio#1665)

* Add ZStream#cross, crossWith, <*>, <*, *>, <&>, zipLeft, zipRight, <&,  &> (zio#1510)

* Add ZStrem#zip left and right operators and alias for all zip operators

* formatted code to make scalafmtCheck pass.

* change for <*> variants with flatmap and <&> with zipWith.

* adding crossWith and cross functions to ZStream.

* merge conflict change.

* formatted naming conventions.

* Update the encoding of ZSink (zio#1560)

* prototype safe semaphore

* cleanup

* uodate documentation

* address review comments

* cleanup

* add back and deprecate methods

* cleanup

* make method private
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.

Add Semaphore#permits

7 participants