Linked by Hadrien Grasland on Thu 19th May 2011 21:31 UTC
Hardware, Embedded Systems Having read the feedback resulting from my previous post on interrupts (itself resulting from an earlier OSnews Asks item on the subject), I've had a look at the way interrupts work on PowerPC v2.02, SPARC v9, Alpha and IA-64 (Itanium), and contribute this back to anyone who's interested (or willing to report any blatant flaw found in my posts). I've also tried to rework a bit my interrupt handling model to make it significantly clearer and have it look more like a design doc and less like a code draft.
Permalink for comment 474252
To read all comments associated with this story, please click here.
RE[4]: Locking benchmarks
by Alfman on Mon 23rd May 2011 16:57 UTC in reply to "RE[3]: Locking benchmarks"
Alfman
Member since:
2011-01-28

jal_,

"What I'm wondering about though, is how do you sync all these asynchronous calls that have a linear dependancy. E.g. when opening a file, then reading, then closing it asynchronusly, since the asynchronous nature does not say anything about order of execution"

That's a good question, although somewhat complex to describe abstractly.

Remember that async functions can be called synchronously at any time, since they never block by design.

If there is a dependency between the start of an async request and the result of another, then the second async request should be executed within the callback of the first instead of simultaneously.

All async objects have state within them indicating whether they are busy or ready if that is needed. However since the callbacks only trigger on transition from busy to ready, it's not typically necessary for an explicit check.


"you could have an attempt to read a unopened file"

In this case, the file open callback tells you that the file is open (or that there was an error). It's not until this point that it is valid to read the file.

"or a file that has been closed before it has been read."

While I actually do have an event for file closed by the OS, this is actually not supported by *nix or windows. What happens is a read/write failure instead. The file is always closed by the user code, therefor it should never happen at an unanticipated time.


"So you'd need some synchronization primitives, which typically means blocking (or spinning, which may be as bad)."

When an IO event occurs, we handle it immediately in our callback. If that means initiating another async IO request, then it is done without blocking. If it means initiating a CPU bound task, then we fire off a thread. Then our async callback function returns without ever having blocked. So we're always ready to handle more IO. Since I use eventfd and pollfd to retrieve IO status from the kernel, I can even retrieve several IO events with a single kernel syscall.

I don't know if I addressed your concern or not?

There are no thread synchronization primitives necessary since every async callback can safely assume that no other async callbacks are running in parallel. The state of the objects it sees will never change behind it's back, so there's no need for a mutex to protect anything.


It's often referred to as a giant state machine, which sounds complex, and it could be if it were designed like a kitchen sink win32 event loop. My implementations encapsulates all that complexity such that tasks don't need to know or care about other tasks, only handling the IO they are concerned with. So the code is the same if it's handling one client or 10K.



"Also, in your resolve_cb, there's a while-loop that tests for the result of an async call. Since it's async, it cannot actually wait for the result of the call, so all it can do is check the result of the invocation of the call, right?"


Right and wrong.

The async_resolve calls are implemented on top of the blocking "gethostbyname2_r" implemented in libc. So when the resolve call is initiated, the async library initializes a proxy thread and executes gethostbyname2_r in the background (this is completely under the hood and does not concern the async users. In theory a native asynchronous gethostbyname function could be written for libc...but realistically it may never happen).

In the meantime, the async program can continue handling events, once gethostbyname2_r is done, the user's callback is executed, in this case resolve_cb.

async_resolve_get_address is a helper function which retrieves all the IPs which were fetched by the libc call, like other async fucntions, it doesn't block since the IPs are already fetched.


"Or do you have some mechanism that blocks the while-loopthread until an answer arrives? I guess not, as that more or less defeates the purpose?"

No, it doesn't block, resolve_cb (callback) isn't executed in the first place until data is available.

Reply Parent Score: 1