Multithreaded Drivers

Sean Hinde <>
Mon Jul 23 17:53:41 CEST 2001


I'm digging through the existing drivers trying to figure out how they
work.. Particularly with regard to multithreaded drivers.

So far I have figured out from studying efile_drv that:

The start function should return a pointer to the struct which holds all the
state for the thread. This is so that each call to open_port will generate
it's own state which is passed to whichever thread happens to be used to
process the call. This pointer is cast to a long to keep the type system
happy :) 

Q. Can I use the new erl_driver.h functions with multithreaded drivers (I
notice that not all the functions in driver.h are included in the new one,
particularly driver_async)?

Q. I presume if so I can just cast the pointer to my state into a

The start function in efile_drv uses the function sys_alloc_from(200,
sizeof(file_descriptor_state_struct)). I can't figure out the point of the
200, or how to decide what it should be in my case. As far as I can make out
it only has an effect in an instrumented system (from looking at the def in

Q. What is the approved way to assign and clear memory in this case and in
drivers in general? 

Q. Under what circumstances is the free function passed to driver_async
called (I couldn't find any from a quick look)?

Q. What is the key parameter intended for and do I ever need to worry about

I've got the hang of the idea that the main function driven by
port_command/2 should just schedule the work to be done in a thread sometime
later with the driver_async function, and that when the thread has done it's
work for this time it puts itself into the queue to call the async_ready
callback which sends the result back to erlang. This looks fine..

Q. What happens if I send another port_command/2 to the port before the
result of the possibly time consuming last operation has returned?

Q. I wonder if I can use this to call the driver_cancel function sometime
later to abort the operation?

Q. What is the best way to timeout the operation which is being carried out
in a thread so I can return the resource to useful state (in case it is in
an endless loop, or the far end of some link has died leaving the thread
waiting forever..)?

My head is gradually turning to jelly here.. All comments on my
understanding (or answer to my questions) are welcome!


This email (including attachments) is confidential.  If you have received
this email in error please notify the sender immediately and delete this
email from your system without copying or disseminating it or placing any
reliance upon its contents.  We cannot accept liability for any breaches of
confidence arising through use of email.  Any opinions expressed in this
email (including attachments) are those of the author and do not necessarily
reflect our opinions.  We will not accept responsibility for any commitments
made by our employees outside the scope of our business.  We do not warrant
the accuracy or completeness of such information.

More information about the erlang-questions mailing list