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

Shorten duration while base connection locks are held #43

Merged
merged 7 commits into from May 17, 2016

Conversation

grddev
Copy link
Collaborator

@grddev grddev commented May 9, 2016

See individual commits for more details.

This includes debug logging because we really need to test this in a real system and see that we don't get errors when reading and writing.
The worst case scenario (assuming that Pipe is thread safe) is that 65537 threads call #unblock and don't see the updated value of @State and we get an exception, but we catch that exception so it's not catastrophic.
The only thread that calls #read is the IO reactor thread, so locking is unnecessary.

There is a hidden assumption in this: the unblocker socket MUST be the first socket to be read after the IO reactor comes out of select, otherwise unblocks might be missed.
All the other uses of the lock have been removed, so what would this lock lock against? #close will only be called from the IO reactor thread.
This, along with a test, will make it clear that the unblocker must be the first readable passed to select.
#
# This method differs from #cheap_peek in that it might return an empty
# result even when the buffer is non-empty. It is primarily useful when
# feeding bytes to a socket from a single reactor thread.
Copy link
Owner

Choose a reason for hiding this comment

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

I think this description should make it more clear that a single reader is assumed (for example: "It is only safe when there is only one reader, and it's primarily useful when feeding bytes to a socket from a single reactor thread").

@iconara
Copy link
Owner

iconara commented May 10, 2016

👍

@grddev grddev force-pushed the improve-base-connection-locks branch from 0f5a487 to 009f4a5 Compare May 10, 2016 07:24
iconara and others added 2 commits May 17, 2016 10:27
This avoids performing the nonblocking write while holding the lock.
While it seems counter-intuitive, it seems this operation frequently
takes a lot of time. I don't know whether it is due to a profiling
artifact or not, but it shouldn't hurt in any case.

Since reads from the write buffer only happen from the single reactor
thread, we could actually perform (some) flushes without locking before
performing the non-blocking write. This uses a new byte-buffer accessor
that allows peeking at the contents of the byte buffer without taking
any lock.
@iconara iconara force-pushed the improve-base-connection-locks branch from 009f4a5 to 0fc72e5 Compare May 17, 2016 08:33
@iconara iconara merged commit 0fc72e5 into master May 17, 2016
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

2 participants