[PATCH] Use libpcl for portable coroutines

Sam Liddicott sam at liddicott.com
Thu May 14 16:30:15 GMT 2009


This is a proof of concept patch, to make using libpcl easy for smb
clients, so that sync functions can easily be converted to async functions.

composite_wait and smbcli_request_receive have been modified to detect
if they are being called on a private stack (not the main application
stack).

If so, and if async.fn==NULL && async.private_data==NULL then a
*_receive function is being called synchronously on a private stack -
and so instead of calling a nested event loop, it sets the callback to
return to this stack and then switches back to the main stack, and
(presumably very soon) enters the main event loop.

When the awaited response is received, the stack is switched back and
execution continues as normal, and the message is received.

This is a way of getting rid of nested event loops, and also coding
asynchronous tasks synchronously.

It merely becomes a matter of whatever launches the task, to do so on a
new stack, and the rest of it will take care of itself!

Below is an example conversion of a ntvfs connect function that was
synchronous, to make it asynchronous using co-routines. It is much
simpler than the example I sent Metze during sambaXP, and depends on the
attached patch.


*** trampoline function to get args back
void proxy_connect_trampoline(va_list args)
{
       co_get_va(struct ntvfs_module_context *, ntvfs);
       co_get_va(struct ntvfs_request *, req);
       co_get_va(union smb_tcon *, tcon);

       ntvfs_co_passthrough_exit(proxy_connect(ntvfs, req, tcon));
}

*** AND then insert this at the top of the ntvfs connect function:

       /* Convert to async coroutine */
       if ((req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC) &&
           co_create_va(NULL, 16384, proxy_connect_trampoline, ntvfs,
req, tcon) &&
           /* Did it accept? - check if it marked the async flag */
           (req->async_states->state & NTVFS_ASYNC_STATE_ASYNC)) {
               return NT_STATUS_OK;
       }

*** AND then carry on as normal




Signed-off-by: Sam Liddicott <sam at liddicott.com>
---
 lib/coroutine/config.m4              |    9 +++
 lib/coroutine/config.mk              |    6 ++
 lib/coroutine/coroutine.c            |  113
++++++++++++++++++++++++++++++++++
 lib/coroutine/coroutine.h            |   79 +++++++++++++++++++++++
 packaging4/RPM/samba.spec            |    2 +
 source4/configure.ac                 |    1 +
 source4/libcli/composite/composite.c |    5 ++
 source4/libcli/config.mk             |    4 +-
 source4/libcli/raw/rawrequest.c      |    5 ++
 source4/main.mk                      |    1 +
 10 files changed, 223 insertions(+), 2 deletions(-)
 create mode 100644 lib/coroutine/config.m4
 create mode 100644 lib/coroutine/config.mk
 create mode 100644 lib/coroutine/coroutine.c
 create mode 100644 lib/coroutine/coroutine.h


-------------- next part --------------
A non-text attachment was scrubbed...
Name: 75b2cbeb318866ffae13d82edefb8db7f6a4ccec.diff
Type: text/x-patch
Size: 11344 bytes
Desc: not available
Url : http://lists.samba.org/archive/samba-technical/attachments/20090514/d1a448ef/75b2cbeb318866ffae13d82edefb8db7f6a4ccec.bin


More information about the samba-technical mailing list