.TH erl_driver 3erl "erts 8.2.1" "Ericsson AB" "C Library Functions" .SH NAME erl_driver \- API functions for an Erlang driver. .SH DESCRIPTION .LP An Erlang driver is a library containing a set of native driver callback functions that the Erlang Virtual Machine calls when certain events occur\&. There can be multiple instances of a driver, each instance is associated with an Erlang port\&. .LP .RS -4 .B Warning: .RE \fIUse this functionality with extreme care\&.\fR\& .LP A driver callback is executed as a direct extension of the native code of the VM\&. Execution is not made in a safe environment\&. The VM \fIcannot\fR\& provide the same services as provided when executing Erlang code, such as pre-emptive scheduling or memory protection\&. If the driver callback function does not behave well, the whole VM will misbehave\&. .RS 2 .TP 2 * A driver callback that crash will crash the whole VM\&. .LP .TP 2 * An erroneously implemented driver callback can cause a VM internal state inconsistency, which can cause a crash of the VM, or miscellaneous misbehaviors of the VM at any point after the call to the driver callback\&. .LP .TP 2 * A driver callback doing \fBlengthy work\fR\& before returning degrades responsiveness of the VM and can cause miscellaneous strange behaviors\&. Such strange behaviors include, but are not limited to, extreme memory usage and bad load balancing between schedulers\&. Strange behaviors that can occur because of lengthy work can also vary between Erlang/OTP releases\&. .LP .RE .LP As from ERTS 5\&.5\&.3 the driver interface has been extended (see \fB\fIextended marker\fR\&\fR\&)\&. The extended interface introduces \fBversion management\fR\&, the possibility to pass capability flags (see \fB\fIdriver_flags\fR\&\fR\&) to the runtime system at driver initialization, and some new driver API functions\&. .LP .RS -4 .B Note: .RE As from ERTS 5\&.9 old drivers must be recompiled and use the extended interface\&. They must also be adjusted to the \fB 64-bit capable driver interface\fR\&\&. .LP The driver calls back to the emulator, using the API functions declared in \fIerl_driver\&.h\fR\&\&. They are used for outputting data from the driver, using timers, and so on\&. .LP Each driver instance is associated with a port\&. Every port has a port owner process\&. Communication with the port is normally done through the port owner process\&. Most of the functions take the \fIport\fR\& handle as an argument\&. This identifies the driver instance\&. Notice that this port handle must be stored by the driver, it is not given when the driver is called from the emulator (see \fB\fIdriver_entry\fR\&\fR\&)\&. .LP Some of the functions take a parameter of type \fIErlDrvBinary\fR\&, a driver binary\&. It is to be both allocated and freed by the caller\&. Using a binary directly avoids one extra copying of data\&. .LP Many of the output functions have a "header buffer", with \fIhbuf\fR\& and \fIhlen\fR\& parameters\&. This buffer is sent as a list before the binary (or list, depending on port mode) that is sent\&. This is convenient when matching on messages received from the port\&. (Although in the latest Erlang versions there is the binary syntax, which enables you to match on the beginning of a binary\&.) .LP In the runtime system with SMP support, drivers are locked either on driver level or port level (driver instance level)\&. By default driver level locking will be used, that is, only one emulator thread will execute code in the driver at a time\&. If port level locking is used, multiple emulator threads can execute code in the driver at the same time\&. Only one thread at a time will call driver callbacks corresponding to the same port, though\&. To enable port level locking, set the \fIERL_DRV_FLAG_USE_PORT_LOCKING\fR\& \fBdriver flag\fR\& in the \fB\fIdriver_entry\fR\&\fR\& used by the driver\&. When port level locking is used, the driver writer is responsible for synchronizing all accesses to data shared by the ports (driver instances)\&. .LP Most drivers written before the runtime system with SMP support existed can run in the runtime system with SMP support, without being rewritten, if driver level locking is used\&. .LP .RS -4 .B Note: .RE It is assumed that drivers do not access other drivers\&. If drivers access each other, they must provide their own mechanism for thread-safe synchronization\&. Such "inter-driver communication" is strongly discouraged\&. .LP Previously, in the runtime system without SMP support, specific driver callbacks were always called from the same thread\&. This is \fInot\fR\& the case in the runtime system with SMP support\&. Regardless of locking scheme used, calls to driver callbacks can be made from different threads\&. For example, two consecutive calls to exactly the same callback for exactly the same port can be made from two different threads\&. This is for \fImost\fR\& drivers not a problem, but it can be\&. Drivers that depend on all callbacks that are called in the same thread, \fImust\fR\& be rewritten before they are used in the runtime system with SMP support\&. .LP .RS -4 .B Note: .RE Regardless of locking scheme used, calls to driver callbacks can be made from different threads\&. .LP Most functions in this API are \fInot\fR\& thread-safe, that is, they \fIcannot\fR\& be called from any thread\&. Functions that are not documented as thread-safe can only be called from driver callbacks or function calls descending from a driver callback call\&. Notice that driver callbacks can be called from different threads\&. This, however, is not a problem for any function in this API, as the emulator has control over these threads\&. .LP .RS -4 .B Warning: .RE Functions not explicitly documented as thread-safe are \fInot\fR\& thread safe\&. Also notice that some functions are \fIonly\fR\& thread-safe when used in a runtime system with SMP support\&. .LP A function not explicitly documented as thread-safe can, at some point in time, have a thread-safe implementation in the runtime system\&. Such an implementation can however change to a thread \fIunsafe\fR\& implementation at any time \fIwithout any notice\fR\&\&. .LP \fIOnly use functions explicitly documented as thread-safe from arbitrary threads\&.\fR\& .LP As mentioned in the \fBwarning\fR\& text at the beginning of this section, it is of vital importance that a driver callback returns relatively fast\&. It is difficult to give an exact maximum amount of time that a driver callback is allowed to work, but usually a well-behaving driver callback is to return within 1 millisecond\&. This can be achieved using different approaches\&. If you have full control over the code to execute in the driver callback, the best approach is to divide the work into multiple chunks of work, and trigger multiple calls to the \fBtime-out callback\fR\& using zero time-outs\&. Function \fB\fIerl_drv_consume_timeslice\fR\&\fR\& can be useful to determine when to trigger such time-out callback calls\&. However, sometimes it cannot be implemented this way, for example when calling third-party libraries\&. In this case, you typically want to dispatch the work to another thread\&. Information about thread primitives is provided below\&. .SH "FUNCTIONALITY" .LP All functions that a driver needs to do with Erlang are performed through driver API functions\&. Functions exist for the following functionality: .RS 2 .TP 2 .B Timer functions: Control the timer that a driver can use\&. The timer has the emulator call the \fB\fItimeout\fR\&\fR\& entry function after a specified time\&. Only one timer is available for each driver instance\&. .TP 2 .B Queue handling: Every driver instance has an associated queue\&. This queue is a \fISysIOVec\fR\&, which works as a buffer\&. It is mostly used for the driver to buffer data that is to be written to a device, it is a byte stream\&. If the port owner process closes the driver, and the queue is not empty, the driver is not closed\&. This enables the driver to flush its buffers before closing\&. .RS 2 .LP The queue can be manipulated from any threads if a port data lock is used\&. For more information, see \fB\fIErlDrvPDL\fR\&\fR\&\&. .RE .TP 2 .B Output functions: With these functions, the driver sends data back to the emulator\&. The data is received as messages by the port owner process, see \fB\fIerlang:open_port/2\fR\&\fR\&\&. The vector function and the function taking a driver binary are faster, as they avoid copying the data buffer\&. There is also a fast way of sending terms from the driver, without going through the binary term format\&. .TP 2 .B Failure: The driver can exit and signal errors up to Erlang\&. This is only for severe errors, when the driver cannot possibly keep open\&. .TP 2 .B Asynchronous calls: Erlang/OTP R7B and later versions have provision for asynchronous function calls, using a thread pool provided by Erlang\&. There is also a select call, which can be used for asynchronous drivers\&. .TP 2 .B Multi-threading: A POSIX thread like API for multi-threading is provided\&. The Erlang driver thread API only provides a subset of the functionality provided by the POSIX thread API\&. The subset provided is more or less the basic functionality needed for multi-threaded programming: .RS 2 .TP 2 * \fBThreads\fR\& .LP .TP 2 * \fBMutexes\fR\& .LP .TP 2 * \fB Condition variables\fR\& .LP .TP 2 * \fB Read/write locks\fR\& .LP .TP 2 * \fB Thread-specific data\fR\& .LP .RE .RS 2 .LP The Erlang driver thread API can be used in conjunction with the POSIX thread API on UN-ices and with the Windows native thread API on Windows\&. The Erlang driver thread API has the advantage of being portable, but there can exist situations where you want to use functionality from the POSIX thread API or the Windows native thread API\&. .RE .RS 2 .LP The Erlang driver thread API only returns error codes when it is reasonable to recover from an error condition\&. If it is not reasonable to recover from an error condition, the whole runtime system is terminated\&. For example, if a create mutex operation fails, an error code is returned, but if a lock operation on a mutex fails, the whole runtime system is terminated\&. .RE .RS 2 .LP Notice that there is no "condition variable wait with time-out" in the Erlang driver thread API\&. This because of issues with \fIpthread_cond_timedwait\fR\&\&. When the system clock suddenly is changed, it is not always guaranteed that you will wake up from the call as expected\&. An Erlang runtime system must be able to cope with sudden changes of the system clock\&. Therefore, we have omitted it from the Erlang driver thread API\&. In the Erlang driver case, time-outs can and are to be handled with the timer functionality of the Erlang driver API\&. .RE .RS 2 .LP In order for the Erlang driver thread API to function, thread support must be enabled in the runtime system\&. An Erlang driver can check if thread support is enabled by use of \fB\fIdriver_system_info\fR\&\fR\&\&. Notice that some functions in the Erlang driver API are thread-safe only when the runtime system has SMP support, also this information can be retrieved through \fB\fIdriver_system_info\fR\&\fR\&\&. Also notice that many functions in the Erlang driver API are \fInot\fR\& thread-safe, regardless of whether SMP support is enabled or not\&. If a function is not documented as thread-safe, it is \fInot\fR\& thread-safe\&. .RE .LP .RS -4 .B Note: .RE When executing in an emulator thread, it is \fIvery important\fR\& that you unlock \fIall\fR\& locks you have locked before letting the thread out of your control; otherwise you are \fIvery likely\fR\& to deadlock the whole emulator\&. .LP If you need to use thread-specific data in an emulator thread, only have the thread-specific data set while the thread is under your control, and clear the thread-specific data before you let the thread out of your control\&. .RS 2 .LP In the future, debug functionality will probably be integrated with the Erlang driver thread API\&. All functions that create entities take a \fIname\fR\& argument\&. Currently the \fIname\fR\& argument is unused, but it will be used when the debug functionality is implemented\&. If you name all entities created well, the debug functionality will be able to give you better error reports\&. .RE .TP 2 .B Adding/removing drivers: A driver can add and later remove drivers\&. .TP 2 .B Monitoring processes: A driver can monitor a process that does not own a port\&. .TP 2 .B Version management: Version management is enabled for drivers that have set the \fB\fIextended_marker\fR\&\fR\& field of their \fB\fIdriver_entry\fR\&\fR\& to \fIERL_DRV_EXTENDED_MARKER\fR\&\&. \fIerl_driver\&.h\fR\& defines: .RS 2 .TP 2 * \fIERL_DRV_EXTENDED_MARKER\fR\& .LP .TP 2 * \fIERL_DRV_EXTENDED_MAJOR_VERSION\fR\&, which is incremented when driver incompatible changes are made to the Erlang runtime system\&. Normally it suffices to recompile drivers when \fIERL_DRV_EXTENDED_MAJOR_VERSION\fR\& has changed, but it can, under rare circumstances, mean that drivers must be slightly modified\&. If so, this will of course be documented\&. .LP .TP 2 * \fIERL_DRV_EXTENDED_MINOR_VERSION\fR\&, which is incremented when new features are added\&. The runtime system uses the minor version of the driver to determine what features to use\&. .LP .RE .RS 2 .LP The runtime system normally refuses to load a driver if the major versions differ, or if the major versions are equal and the minor version used by the driver is greater than the one used by the runtime system\&. Old drivers with lower major versions are however allowed after a bump of the major version during a transition period of two major releases\&. Such old drivers can, however, fail if deprecated features are used\&. .RE .RS 2 .LP The emulator refuses to load a driver that does not use the extended driver interface, to allow for 64-bit capable drivers, as incompatible type changes for the callbacks \fB\fIoutput\fR\&\fR\&, \fB\fIcontrol\fR\&\fR\&, and \fB\fIcall\fR\&\fR\& were introduced in Erlang/OTP R15B\&. A driver written with the old types would compile with warnings and when called return garbage sizes to the emulator, causing it to read random memory and create huge incorrect result blobs\&. .RE .RS 2 .LP Therefore it is not enough to only recompile drivers written with version management for pre R15B types; the types must be changed in the driver suggesting other rewrites, especially regarding size variables\&. \fIInvestigate all warnings when recompiling\&.\fR\& .RE .RS 2 .LP Also, the API driver functions \fIdriver_output*\fR\& and \fIdriver_vec_to_buf\fR\&, \fIdriver_alloc/realloc*\fR\&, and the \fIdriver_*\fR\& queue functions were changed to have larger length arguments and return values\&. This is a lesser problem, as code that passes smaller types gets them auto-converted in the calls, and as long as the driver does not handle sizes that overflow an \fIint\fR\&, all will work as before\&. .RE .TP 2 .B Time measurement: Support for time measurement in drivers: .RS 2 .TP 2 * \fB\fIErlDrvTime\fR\&\fR\& .LP .TP 2 * \fB\fIErlDrvTimeUnit\fR\&\fR\& .LP .TP 2 * \fB\fIerl_drv_monotonic_time\fR\&\fR\& .LP .TP 2 * \fB\fIerl_drv_time_offset\fR\&\fR\& .LP .TP 2 * \fB\fIerl_drv_convert_time_unit\fR\&\fR\& .LP .RE .RE .SH "REWRITES FOR 64-BIT DRIVER INTERFACE" .LP ERTS 5\&.9 introduced two new integer types, \fB\fIErlDrvSizeT\fR\&\fR\& and \fB\fIErlDrvSSizeT\fR\&\fR\&, which can hold 64-bit sizes if necessary\&. .LP To not update a driver and only recompile, it probably works when building for a 32-bit machine creating a false sense of security\&. Hopefully that will generate many important warnings\&. But when recompiling the same driver later on for a 64-bit machine, there \fIwill\fR\& be warnings and almost certainly crashes\&. So it is a \fIbad\fR\& idea to postpone updating the driver and not fixing the warnings\&. .LP When recompiling with \fIgcc\fR\&, use flag \fI-Wstrict-prototypes\fR\& to get better warnings\&. Try to find a similar flag if you use another compiler\&. .LP The following is a checklist for rewriting a pre ERTS 5\&.9 driver, most important first: .RS 2 .TP 2 .B Return types for driver callbacks: Rrewrite driver callback \fB\fIcontrol\fR\&\fR\& to use return type \fIErlDrvSSizeT\fR\& instead of \fIint\fR\&\&. .RS 2 .LP Rewrite driver callback \fB\fIcall\fR\&\fR\& to use return type \fIErlDrvSSizeT\fR\& instead of \fIint\fR\&\&. .RE .LP .RS -4 .B Note: .RE These changes are essential not to crash the emulator or worse cause malfunction\&. Without them a driver can return garbage in the high 32 bits to the emulator, causing it to build a huge result from random bytes, either crashing on memory allocation or succeeding with a random result from the driver call\&. .TP 2 .B Arguments to driver callbacks: Driver callback \fB\fIoutput\fR\&\fR\& now gets \fIErlDrvSizeT\fR\& as 3rd argument instead of previously \fIint\fR\&\&. .RS 2 .LP Driver callback \fB\fIcontrol\fR\&\fR\& now gets \fIErlDrvSizeT\fR\& as 4th and 6th arguments instead of previously \fIint\fR\&\&. .RE .RS 2 .LP Driver callback \fB\fIcall\fR\&\fR\& now gets \fIErlDrvSizeT\fR\& as 4th and 6th arguments instead of previously \fIint\fR\&\&. .RE .RS 2 .LP Sane compiler\&'s calling conventions probably make these changes necessary only for a driver to handle data chunks that require 64-bit size fields (mostly larger than 2 GB, as that is what an \fIint\fR\& of 32 bits can hold)\&. But it is possible to think of non-sane calling conventions that would make the driver callbacks mix up the arguments causing malfunction\&. .RE .LP .RS -4 .B Note: .RE The argument type change is from signed to unsigned\&. This can cause problems for, for example, loop termination conditions or error conditions if you only change the types all over the place\&. .TP 2 .B Larger \fIsize\fR\& field in \fIErlIOVec\fR\&: The \fIsize\fR\& field in \fB\fIErlIOVec\fR\&\fR\& has been changed to \fIErlDrvSizeT\fR\& from \fIint\fR\&\&. Check all code that use that field\&. .RS 2 .LP Automatic type-casting probably makes these changes necessary only for a driver that encounters sizes > 32 bits\&. .RE .LP .RS -4 .B Note: .RE The \fIsize\fR\& field changed from signed to unsigned\&. This can cause problems for, for example, loop termination conditions or error conditions if you only change the types all over the place\&. .TP 2 .B Arguments and return values in the driver API: Many driver API functions have changed argument type and/or return value to \fIErlDrvSizeT\fR\& from mostly \fIint\fR\&\&. Automatic type-casting probably makes these changes necessary only for a driver that encounters sizes > 32 bits\&. .RS 2 .TP 2 .B \fB\fIdriver_output\fR\&\fR\&: 3rd argument .TP 2 .B \fB\fIdriver_output2\fR\&\fR\&: 3rd and 5th arguments .TP 2 .B \fB\fIdriver_output_binary\fR\&\fR\&: 3rd, 5th, and 6th arguments .TP 2 .B \fB\fIdriver_outputv\fR\&\fR\&: 3rd and 5th arguments .TP 2 .B \fB\fIdriver_vec_to_buf\fR\&\fR\&: 3rd argument and return value .TP 2 .B \fB\fIdriver_alloc\fR\&\fR\&: 1st argument .TP 2 .B \fB\fIdriver_realloc\fR\&\fR\&: 2nd argument .TP 2 .B \fB\fIdriver_alloc_binary\fR\&\fR\&: 1st argument .TP 2 .B \fB\fIdriver_realloc_binary\fR\&\fR\&: 2nd argument .TP 2 .B \fB\fIdriver_enq\fR\&\fR\&: 3rd argument .TP 2 .B \fB\fIdriver_pushq\fR\&\fR\&: 3rd argument .TP 2 .B \fB\fIdriver_deq\fR\&\fR\&: 2nd argument and return value .TP 2 .B \fB\fIdriver_sizeq\fR\&\fR\&: Return value .TP 2 .B \fB\fIdriver_enq_bin\fR\&\fR\&: 3rd and 4th arguments .TP 2 .B \fB\fIdriver_pushq_bin\fR\&\fR\&: 3rd and 4th arguments .TP 2 .B \fB\fIdriver_enqv\fR\&\fR\&: 3rd argument .TP 2 .B \fB\fIdriver_pushqv\fR\&\fR\&: 3rd argument .TP 2 .B \fB\fIdriver_peekqv\fR\&\fR\&: Return value .RE .LP .RS -4 .B Note: .RE This is a change from signed to unsigned\&. This can cause problems for, for example, loop termination conditions and error conditions if you only change the types all over the place\&. .RE .SH "DATA TYPES" .RS 2 .TP 2 .B \fIErlDrvSizeT\fR\&: An unsigned integer type to be used as \fIsize_t\fR\&\&. .TP 2 .B \fIErlDrvSSizeT\fR\&: A signed integer type, the size of \fIErlDrvSizeT\fR\&\&. .TP 2 .B \fIErlDrvSysInfo\fR\&: .LP .nf typedef struct ErlDrvSysInfo { int driver_major_version; int driver_minor_version; char *erts_version; char *otp_release; int thread_support; int smp_support; int async_threads; int scheduler_threads; int nif_major_version; int nif_minor_version; int dirty_scheduler_support; } ErlDrvSysInfo; .fi .RS 2 .LP The \fIErlDrvSysInfo\fR\& structure is used for storage of information about the Erlang runtime system\&. \fB\fIdriver_system_info\fR\&\fR\& writes the system information when passed a reference to a \fIErlDrvSysInfo\fR\& structure\&. The fields in the structure are as follows: .RE .RS 2 .TP 2 .B \fIdriver_major_version\fR\&: The value of \fB\fIERL_DRV_EXTENDED_MAJOR_VERSION\fR\&\fR\& when the runtime system was compiled\&. This value is the same as the value of \fB\fIERL_DRV_EXTENDED_MAJOR_VERSION\fR\&\fR\& used when compiling the driver; otherwise the runtime system would have refused to load the driver\&. .TP 2 .B \fIdriver_minor_version\fR\&: The value of \fB\fIERL_DRV_EXTENDED_MINOR_VERSION\fR\&\fR\& when the runtime system was compiled\&. This value can differ from the value of \fB\fIERL_DRV_EXTENDED_MINOR_VERSION\fR\&\fR\& used when compiling the driver\&. .TP 2 .B \fIerts_version\fR\&: A string containing the version number of the runtime system (the same as returned by \fB\fIerlang:system_info(version)\fR\&\fR\&)\&. .TP 2 .B \fIotp_release\fR\&: A string containing the OTP release number (the same as returned by \fB\fIerlang:system_info(otp_release)\fR\&\fR\&)\&. .TP 2 .B \fIthread_support\fR\&: A value \fI!= 0\fR\& if the runtime system has thread support; otherwise \fI0\fR\&\&. .TP 2 .B \fIsmp_support\fR\&: A value \fI!= 0\fR\& if the runtime system has SMP support; otherwise \fI0\fR\&\&. .TP 2 .B \fIasync_threads\fR\&: The number of async threads in the async thread pool used by \fB\fIdriver_async\fR\&\fR\& (the same as returned by \fB\fIerlang:system_info(thread_pool_size)\fR\&\fR\&)\&. .TP 2 .B \fIscheduler_threads\fR\&: The number of scheduler threads used by the runtime system (the same as returned by \fB\fIerlang:system_info(schedulers)\fR\&\fR\&)\&. .TP 2 .B \fInif_major_version\fR\&: The value of \fIERL_NIF_MAJOR_VERSION\fR\& when the runtime system was compiled\&. .TP 2 .B \fInif_minor_version\fR\&: The value of \fIERL_NIF_MINOR_VERSION\fR\& when the runtime system was compiled\&. .TP 2 .B \fIdirty_scheduler_support\fR\&: A value \fI!= 0\fR\& if the runtime system has support for dirty scheduler threads; otherwise \fI0\fR\&\&. .RE .TP 2 .B \fIErlDrvBinary\fR\&: .LP .nf typedef struct ErlDrvBinary { ErlDrvSint orig_size; char orig_bytes[]; } ErlDrvBinary; .fi .RS 2 .LP The \fIErlDrvBinary\fR\& structure is a binary, as sent between the emulator and the driver\&. All binaries are reference counted; when \fIdriver_binary_free\fR\& is called, the reference count is decremented, when it reaches zero, the binary is deallocated\&. \fIorig_size\fR\& is the binary size and \fIorig_bytes\fR\& is the buffer\&. \fIErlDrvBinary\fR\& has not a fixed size, its size is \fIorig_size + 2 * sizeof(int)\fR\&\&. .RE .LP .RS -4 .B Note: .RE The \fIrefc\fR\& field has been removed\&. The reference count of an \fIErlDrvBinary\fR\& is now stored elsewhere\&. The reference count of an \fIErlDrvBinary\fR\& can be accessed through \fB\fIdriver_binary_get_refc\fR\&\fR\&, \fB\fIdriver_binary_inc_refc\fR\&\fR\&, and \fB\fIdriver_binary_dec_refc\fR\&\fR\&\&. .RS 2 .LP Some driver calls, such as \fIdriver_enq_binary\fR\&, increment the driver reference count, and others, such as \fIdriver_deq\fR\& decrement it\&. .RE .RS 2 .LP Using a driver binary instead of a normal buffer is often faster, as the emulator needs not to copy the data, only the pointer is used\&. .RE .RS 2 .LP A driver binary allocated in the driver, with \fIdriver_alloc_binary\fR\&, is to be freed in the driver (unless otherwise stated) with \fIdriver_free_binary\fR\&\&. (Notice that this does not necessarily deallocate it, if the driver is still referred in the emulator, the ref-count will not go to zero\&.) .RE .RS 2 .LP Driver binaries are used in the \fIdriver_output2\fR\& and \fIdriver_outputv\fR\& calls, and in the queue\&. Also the driver callback \fB\fIoutputv\fR\&\fR\& uses driver binaries\&. .RE .RS 2 .LP If the driver for some reason wants to keep a driver binary around, for example in a static variable, the reference count is to be incremented, and the binary can later be freed in the \fB\fIstop\fR\&\fR\& callback, with \fIdriver_free_binary\fR\&\&. .RE .RS 2 .LP Notice that as a driver binary is shared by the driver and the emulator\&. A binary received from the emulator or sent to the emulator must not be changed by the driver\&. .RE .RS 2 .LP Since ERTS 5\&.5 (Erlang/OTP R11B), \fIorig_bytes\fR\& is guaranteed to be properly aligned for storage of an array of doubles (usually 8-byte aligned)\&. .RE .TP 2 .B \fIErlDrvData\fR\&: A handle to driver-specific data, passed to the driver callbacks\&. It is a pointer, and is most often type cast to a specific pointer in the driver\&. .TP 2 .B \fISysIOVec\fR\&: A system I/O vector, as used by \fIwritev\fR\& on Unix and \fIWSASend\fR\& on Win32\&. It is used in \fIErlIOVec\fR\&\&. .TP 2 .B \fIErlIOVec\fR\&: .LP .nf typedef struct ErlIOVec { int vsize; ErlDrvSizeT size; SysIOVec* iov; ErlDrvBinary** binv; } ErlIOVec; .fi .RS 2 .LP The I/O vector used by the emulator and drivers is a list of binaries, with a \fISysIOVec\fR\& pointing to the buffers of the binaries\&. It is used in \fIdriver_outputv\fR\& and the \fB\fIoutputv\fR\&\fR\& driver callback\&. Also, the driver queue is an \fIErlIOVec\fR\&\&. .RE .TP 2 .B \fIErlDrvMonitor\fR\&: When a driver creates a monitor for a process, a \fIErlDrvMonitor\fR\& is filled in\&. This is an opaque data type that can be assigned to, but not compared without using the supplied compare function (that is, it behaves like a struct)\&. .RS 2 .LP The driver writer is to provide the memory for storing the monitor when calling \fB\fIdriver_monitor_process\fR\&\fR\&\&. The address of the data is not stored outside of the driver, so \fIErlDrvMonitor\fR\& can be used as any other data, it can be copied, moved in memory, forgotten, and so on\&. .RE .TP 2 .B \fIErlDrvNowData\fR\&: The \fIErlDrvNowData\fR\& structure holds a time stamp consisting of three values measured from some arbitrary point in the past\&. The three structure members are: .RS 2 .TP 2 .B \fImegasecs\fR\&: The number of whole megaseconds elapsed since the arbitrary point in time .TP 2 .B \fIsecs\fR\&: The number of whole seconds elapsed since the arbitrary point in time .TP 2 .B \fImicrosecs\fR\&: The number of whole microseconds elapsed since the arbitrary point in time .RE .TP 2 .B \fIErlDrvPDL\fR\&: If certain port-specific data must be accessed from other threads than those calling the driver callbacks, a port data lock can be used to synchronize the operations on the data\&. Currently, the only port-specific data that the emulator associates with the port data lock is the driver queue\&. .RS 2 .LP Normally a driver instance has no port data lock\&. If the driver instance wants to use a port data lock, it must create the port data lock by calling \fB\fIdriver_pdl_create\fR\&\fR\&\&. .RE .LP .RS -4 .B Note: .RE Once the port data lock has been created, every access to data associated with the port data lock must be done while the port data lock is locked\&. The port data lock is locked and unlocked by \fB\fIdriver_pdl_lock\fR\&\fR\&, and \fB\fIdriver_pdl_unlock\fR\&\fR\&, respectively\&. .RS 2 .LP A port data lock is reference counted, and when the reference count reaches zero, it is destroyed\&. The emulator at least increments the reference count once when the lock is created and decrements it once the port associated with the lock terminates\&. The emulator also increments the reference count when an async job is enqueued and decrements it when an async job has been invoked\&. Also, the driver is responsible for ensuring that the reference count does not reach zero before the last use of the lock by the driver has been made\&. The reference count can be read, incremented, and decremented by \fB\fIdriver_pdl_get_refc\fR\&\fR\&, \fB\fIdriver_pdl_inc_refc\fR\&\fR\&, and \fB\fIdriver_pdl_dec_refc\fR\&\fR\&, respectively\&. .RE .TP 2 .B \fIErlDrvTid\fR\&: Thread identifier\&. .RS 2 .LP See also \fB\fIerl_drv_thread_create\fR\&\fR\&, \fB\fIerl_drv_thread_exit\fR\&\fR\&, \fB\fIerl_drv_thread_join\fR\&\fR\&, \fB\fIerl_drv_thread_self\fR\&\fR\&, and \fB\fIerl_drv_equal_tids\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvThreadOpts\fR\&: .LP .nf int suggested_stack_size; .fi .RS 2 .LP Thread options structure passed to \fB\fIerl_drv_thread_create\fR\&\fR\&\&. The following fields exists: .RE .RS 2 .TP 2 .B \fIsuggested_stack_size\fR\&: A suggestion, in kilowords, on how large a stack to use\&. A value < 0 means default size\&. .RE .RS 2 .LP See also \fB\fIerl_drv_thread_opts_create\fR\&\fR\&, \fB\fIerl_drv_thread_opts_destroy\fR\&\fR\&, and \fB\fIerl_drv_thread_create\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvMutex\fR\&: Mutual exclusion lock\&. Used for synchronizing access to shared data\&. Only one thread at a time can lock a mutex\&. .RS 2 .LP See also \fB\fIerl_drv_mutex_create\fR\&\fR\&, \fB\fIerl_drv_mutex_destroy\fR\&\fR\&, \fB\fIerl_drv_mutex_lock\fR\&\fR\&, \fB\fIerl_drv_mutex_trylock\fR\&\fR\&, and \fB\fIerl_drv_mutex_unlock\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvCond\fR\&: Condition variable\&. Used when threads must wait for a specific condition to appear before continuing execution\&. Condition variables must be used with associated mutexes\&. .RS 2 .LP See also \fB\fIerl_drv_cond_create\fR\&\fR\&, \fB\fIerl_drv_cond_destroy\fR\&\fR\&, \fB\fIerl_drv_cond_signal\fR\&\fR\&, \fB\fIerl_drv_cond_broadcast\fR\&\fR\&, and \fB\fIerl_drv_cond_wait\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvRWLock\fR\&: Read/write lock\&. Used to allow multiple threads to read shared data while only allowing one thread to write the same data\&. Multiple threads can read lock an rwlock at the same time, while only one thread can read/write lock an rwlock at a time\&. .RS 2 .LP See also \fB\fIerl_drv_rwlock_create\fR\&\fR\&, \fB\fIerl_drv_rwlock_destroy\fR\&\fR\&, \fB\fIerl_drv_rwlock_rlock\fR\&\fR\&, \fB\fIerl_drv_rwlock_tryrlock\fR\&\fR\&, \fB\fIerl_drv_rwlock_runlock\fR\&\fR\&, \fB\fIerl_drv_rwlock_rwlock\fR\&\fR\&, \fB\fIerl_drv_rwlock_tryrwlock\fR\&\fR\&, and \fB\fIerl_drv_rwlock_rwunlock\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvTSDKey\fR\&: Key that thread-specific data can be associated with\&. .RS 2 .LP See also \fB\fIerl_drv_tsd_key_create\fR\&\fR\&, \fB\fIerl_drv_tsd_key_destroy\fR\&\fR\&, \fB\fIerl_drv_tsd_set\fR\&\fR\&, and \fB\fIerl_drv_tsd_get\fR\&\fR\&\&. .RE .TP 2 .B \fIErlDrvTime\fR\&: A signed 64-bit integer type for time representation\&. .TP 2 .B \fIErlDrvTimeUnit\fR\&: An enumeration of time units supported by the driver API: .RS 2 .TP 2 .B \fIERL_DRV_SEC\fR\&: Seconds .TP 2 .B \fIERL_DRV_MSEC\fR\&: Milliseconds .TP 2 .B \fIERL_DRV_USEC\fR\&: Microseconds .TP 2 .B \fIERL_DRV_NSEC\fR\&: Nanoseconds .RE .RE .SH EXPORTS .LP .B void add_driver_entry(ErlDrvEntry *de) .br .RS .LP Adds a driver entry to the list of drivers known by Erlang\&. The \fB\fIinit\fR\&\fR\& function of parameter \fIde\fR\& is called\&. .LP .RS -4 .B Note: .RE To use this function for adding drivers residing in dynamically loaded code is dangerous\&. If the driver code for the added driver resides in the same dynamically loaded module (that is, \fI\&.so\fR\& file) as a normal dynamically loaded driver (loaded with the \fIerl_ddll\fR\& interface), the caller is to call \fB\fIdriver_lock_driver\fR\&\fR\& before adding driver entries\&. .LP \fIUse of this function is generally deprecated\&.\fR\& .RE .LP .B void *driver_alloc(ErlDrvSizeT size) .br .RS .LP Allocates a memory block of the size specified in \fIsize\fR\&, and returns it\&. This fails only on out of memory, in which case \fINULL\fR\& is returned\&. (This is most often a wrapper for \fImalloc\fR\&)\&. .LP Memory allocated must be explicitly freed with a corresponding call to \fB\fIdriver_free\fR\&\fR\& (unless otherwise stated)\&. .LP This function is thread-safe\&. .RE .LP .B ErlDrvBinary *driver_alloc_binary(ErlDrvSizeT size) .br .RS .LP Allocates a driver binary with a memory block of at least \fIsize\fR\& bytes, and returns a pointer to it, or \fINULL\fR\& on failure (out of memory)\&. When a driver binary has been sent to the emulator, it must not be changed\&. Every allocated binary is to be freed by a corresponding call to \fB\fIdriver_free_binary\fR\&\fR\& (unless otherwise stated)\&. .LP Notice that a driver binary has an internal reference counter\&. This means that calling \fIdriver_free_binary\fR\&, it may not actually dispose of it\&. If it is sent to the emulator, it can be referenced there\&. .LP The driver binary has a field, \fIorig_bytes\fR\&, which marks the start of the data in the binary\&. .LP This function is thread-safe\&. .RE .LP .B long driver_async(ErlDrvPort port, unsigned int* key, void (*async_invoke)(void*), void* async_data, void (*async_free)(void*)) .br .RS .LP Performs an asynchronous call\&. The function \fIasync_invoke\fR\& is invoked in a thread separate from the emulator thread\&. This enables the driver to perform time-consuming, blocking operations without blocking the emulator\&. .LP The async thread pool size can be set with command-line argument \fB\fI+A\fR\&\fR\& in \fB\fIerl(1)\fR\&\fR\&\&. If an async thread pool is unavailable, the call is made synchronously in the thread calling \fIdriver_async\fR\&\&. The current number of async threads in the async thread pool can be retrieved through \fB\fIdriver_system_info\fR\&\fR\&\&. .LP If a thread pool is available, a thread is used\&. If argument \fIkey\fR\& is \fINULL\fR\&, the threads from the pool are used in a round-robin way, each call to \fIdriver_async\fR\& uses the next thread in the pool\&. With argument \fIkey\fR\& set, this behavior is changed\&. The two same values of \fI*key\fR\& always get the same thread\&. .LP To ensure that a driver instance always uses the same thread, the following call can be used: .LP .nf unsigned int myKey = driver_async_port_key(myPort); r = driver_async(myPort, &myKey, myData, myFunc); .fi .LP It is enough to initialize \fImyKey\fR\& once for each driver instance\&. .LP If a thread is already working, the calls are queued up and executed in order\&. Using the same thread for each driver instance ensures that the calls are made in sequence\&. .LP The \fIasync_data\fR\& is the argument to the functions \fIasync_invoke\fR\& and \fIasync_free\fR\&\&. It is typically a pointer to a structure containing a pipe or event that can be used to signal that the async operation completed\&. The data is to be freed in \fIasync_free\fR\&\&. .LP When the async operation is done, \fB\fIready_async\fR\&\fR\& driver entry function is called\&. If \fIready_async\fR\& is \fINULL\fR\& in the driver entry, the \fIasync_free\fR\& function is called instead\&. .LP The return value is \fI-1\fR\& if the \fIdriver_async\fR\& call fails\&. .LP .RS -4 .B Note: .RE As from ERTS 5\&.5\&.4\&.3 the default stack size for threads in the async-thread pool is 16 kilowords, that is, 64 kilobyte on 32-bit architectures\&. This small default size has been chosen because the amount of async-threads can be quite large\&. The default stack size is enough for drivers delivered with Erlang/OTP, but is possibly not sufficiently large for other dynamically linked-in drivers that use the \fIdriver_async\fR\& functionality\&. A suggested stack size for threads in the async-thread pool can be configured through command-line argument \fB\fI+a\fR\&\fR\& in \fB\fIerl(1)\fR\&\fR\&\&. .RE .LP .B unsigned int driver_async_port_key(ErlDrvPort port) .br .RS .LP Calculates a key for later use in \fB\fIdriver_async\fR\&\fR\&\&. The keys are evenly distributed so that a fair mapping between port IDs and async thread IDs is achieved\&. .LP .RS -4 .B Note: .RE Before Erlang/OTP R16, the port ID could be used as a key with proper casting, but after the rewrite of the port subsystem, this is no longer the case\&. With this function, you can achieve the same distribution based on port IDs as before Erlang/OTP R16\&. .RE .LP .B long driver_binary_dec_refc(ErlDrvBinary *bin) .br .RS .LP Decrements the reference count on \fIbin\fR\& and returns the reference count reached after the decrement\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .LP .RS -4 .B Note: .RE The reference count of driver binary is normally to be decremented by calling \fB\fIdriver_free_binary\fR\&\fR\&\&. .LP \fIdriver_binary_dec_refc\fR\& does \fInot\fR\& free the binary if the reference count reaches zero\&. \fIOnly\fR\& use \fIdriver_binary_dec_refc\fR\& when you are sure \fInot\fR\& to reach a reference count of zero\&. .RE .LP .B long driver_binary_get_refc(ErlDrvBinary *bin) .br .RS .LP Returns the current reference count on \fIbin\fR\&\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B long driver_binary_inc_refc(ErlDrvBinary *bin) .br .RS .LP Increments the reference count on \fIbin\fR\& and returns the reference count reached after the increment\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B ErlDrvTermData driver_caller(ErlDrvPort port) .br .RS .LP Returns the process ID of the process that made the current call to the driver\&. The process ID can be used with \fB\fIdriver_send_term\fR\&\fR\& to send back data to the caller\&. \fIdriver_caller\fR\& only returns valid data when currently executing in one of the following driver callbacks: .RS 2 .TP 2 .B \fB\fIstart\fR\&\fR\&: Called from \fB\fIerlang:open_port/2\fR\&\fR\&\&. .TP 2 .B \fB\fIoutput\fR\&\fR\&: Called from \fB\fIerlang:send/2\fR\&\fR\& and \fB\fIerlang:port_command/2\fR\&\fR\&\&. .TP 2 .B \fB\fIoutputv\fR\&\fR\&: Called from \fB\fIerlang:send/2\fR\&\fR\& and \fB\fIerlang:port_command/2\fR\&\fR\&\&. .TP 2 .B \fB\fIcontrol\fR\&\fR\&: Called from \fB\fIerlang:port_control/3\fR\&\fR\&\&. .TP 2 .B \fB\fIcall\fR\&\fR\&: Called from \fB\fIerlang:port_call/3\fR\&\fR\&\&. .RE .LP Notice that this function is \fInot\fR\& thread-safe, not even when the emulator with SMP support is used\&. .RE .LP .B int driver_cancel_timer(ErlDrvPort port) .br .RS .LP Cancels a timer set with \fB\fIdriver_set_timer\fR\&\fR\&\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B int driver_compare_monitors(const ErlDrvMonitor *monitor1, const ErlDrvMonitor *monitor2) .br .RS .LP Compares two \fIErlDrvMonitor\fR\&s\&. Can also be used to imply some artificial order on monitors, for whatever reason\&. .LP Returns \fI0\fR\& if \fImonitor1\fR\& and \fImonitor2\fR\& are equal, < \fI0\fR\& if \fImonitor1\fR\& < \fImonitor2\fR\&, and > \fI0\fR\& if \fImonitor1\fR\& > \fImonitor2\fR\&\&. .RE .LP .B ErlDrvTermData driver_connected(ErlDrvPort port) .br .RS .LP Returns the port owner process\&. .LP Notice that this function is \fInot\fR\& thread-safe, not even when the emulator with SMP support is used\&. .RE .LP .B ErlDrvPort driver_create_port(ErlDrvPort port, ErlDrvTermData owner_pid, char* name, ErlDrvData drv_data) .br .RS .LP Creates a new port executing the same driver code as the port creating the new port\&. .RS 2 .TP 2 .B \fIport\fR\&: The port handle of the port (driver instance) creating the new port\&. .TP 2 .B \fIowner_pid\fR\&: The process ID of the Erlang process to become owner of the new port\&. This process will be linked to the new port\&. You usually want to use \fIdriver_caller(port)\fR\& as \fIowner_pid\fR\&\&. .TP 2 .B \fIname\fR\&: The port name of the new port\&. You usually want to use the same port name as the driver name (\fB\fIdriver_name\fR\&\fR\& field of the \fB\fIdriver_entry\fR\&\fR\&)\&. .TP 2 .B \fIdrv_data\fR\&: The driver-defined handle that is passed in later calls to driver callbacks\&. Notice that the \fBdriver start callback\fR\& is not called for this new driver instance\&. The driver-defined handle is normally created in the \fBdriver start callback\fR\& when a port is created through \fB\fIerlang:open_port/2\fR\&\fR\&\&. .RE .LP The caller of \fIdriver_create_port\fR\& is allowed to manipulate the newly created port when \fIdriver_create_port\fR\& has returned\&. When \fBport level locking\fR\& is used, the creating port is only allowed to manipulate the newly created port until the current driver callback, which was called by the emulator, returns\&. .RE .LP .B int driver_demonitor_process(ErlDrvPort port, const ErlDrvMonitor *monitor) .br .RS .LP Cancels a monitor created earlier\&. .LP Returns \fI0\fR\& if a monitor was removed and > 0 if the monitor no longer exists\&. .RE .LP .B ErlDrvSizeT driver_deq(ErlDrvPort port, ErlDrvSizeT size) .br .RS .LP Dequeues data by moving the head pointer forward in the driver queue by \fIsize\fR\& bytes\&. The data in the queue is deallocated\&. .LP Returns the number of bytes remaining in the queue on success, otherwise \fI-1\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_enq(ErlDrvPort port, char* buf, ErlDrvSizeT len) .br .RS .LP Enqueues data in the driver queue\&. The data in \fIbuf\fR\& is copied (\fIlen\fR\& bytes) and placed at the end of the driver queue\&. The driver queue is normally used in a FIFO way\&. .LP The driver queue is available to queue output from the emulator to the driver (data from the driver to the emulator is queued by the emulator in normal Erlang message queues)\&. This can be useful if the driver must wait for slow devices, and so on, and wants to yield back to the emulator\&. The driver queue is implemented as an \fIErlIOVec\fR\&\&. .LP When the queue contains data, the driver does not close until the queue is empty\&. .LP The return value is \fI0\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_enq_bin(ErlDrvPort port, ErlDrvBinary *bin, ErlDrvSizeT offset, ErlDrvSizeT len) .br .RS .LP Enqueues a driver binary in the driver queue\&. The data in \fIbin\fR\& at \fIoffset\fR\& with length \fIlen\fR\& is placed at the end of the queue\&. This function is most often faster than \fB\fIdriver_enq\fR\&\fR\&, because no data must be copied\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B int driver_enqv(ErlDrvPort port, ErlIOVec *ev, ErlDrvSizeT skip) .br .RS .LP Enqueues the data in \fIev\fR\&, skipping the first \fIskip\fR\& bytes of it, at the end of the driver queue\&. It is faster than \fB\fIdriver_enq\fR\&\fR\&, because no data must be copied\&. .LP The return value is \fI0\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_failure(ErlDrvPort port, int error) .br .B int driver_failure_atom(ErlDrvPort port, char *string) .br .B int driver_failure_posix(ErlDrvPort port, int error) .br .RS .LP Signals to Erlang that the driver has encountered an error and is to be closed\&. The port is closed and the tuple \fI{\&'EXIT\&', error, Err}\fR\& is sent to the port owner process, where error is an error atom (\fIdriver_failure_atom\fR\& and \fIdriver_failure_posix\fR\&) or an integer (\fIdriver_failure\fR\&)\&. .LP The driver is to fail only when in severe error situations, when the driver cannot possibly keep open, for example, buffer allocation gets out of memory\&. For normal errors it is more appropriate to send error codes with \fB\fIdriver_output\fR\&\fR\&\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B int driver_failure_eof(ErlDrvPort port) .br .RS .LP Signals to Erlang that the driver has encountered an EOF and is to be closed, unless the port was opened with option \fIeof\fR\&, in which case \fIeof\fR\& is sent to the port\&. Otherwise the port is closed and an \fI\&'EXIT\&'\fR\& message is sent to the port owner process\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B void driver_free(void *ptr) .br .RS .LP Frees the memory pointed to by \fIptr\fR\&\&. The memory is to have been allocated with \fIdriver_alloc\fR\&\&. All allocated memory is to be deallocated, only once\&. There is no garbage collection in drivers\&. .LP This function is thread-safe\&. .RE .LP .B void driver_free_binary(ErlDrvBinary *bin) .br .RS .LP Frees a driver binary \fIbin\fR\&, allocated previously with \fB\fIdriver_alloc_binary\fR\&\fR\&\&. As binaries in Erlang are reference counted, the binary can still be around\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B ErlDrvTermData driver_get_monitored_process(ErlDrvPort port, const ErlDrvMonitor *monitor) .br .RS .LP Returns the process ID associated with a living monitor\&. It can be used in the \fB\fIprocess_exit\fR\&\fR\& callback to get the process identification for the exiting process\&. .LP Returns \fIdriver_term_nil\fR\& if the monitor no longer exists\&. .RE .LP .B int driver_get_now(ErlDrvNowData *now) .br .RS .LP .RS -4 .B Warning: .RE \fIThis function is deprecated\&. Do not use it\&.\fR\& Use \fB\fIerl_drv_monotonic_time\fR\&\fR\& (perhaps in combination with \fB\fIerl_drv_time_offset\fR\&\fR\&) instead\&. .LP Reads a time stamp into the memory pointed to by parameter \fInow\fR\&\&. For information about specific fields, see \fB\fIErlDrvNowData\fR\&\fR\&\&. .LP The return value is \fI0\fR\&, unless the \fInow\fR\& pointer is invalid, in which case it is < \fI0\fR\&\&. .RE .LP .B int driver_lock_driver(ErlDrvPort port) .br .RS .LP Locks the driver used by the port \fIport\fR\& in memory for the rest of the emulator process\&' lifetime\&. After this call, the driver behaves as one of Erlang\&'s statically linked-in drivers\&. .RE .LP .B ErlDrvTermData driver_mk_atom(char* string) .br .RS .LP Returns an atom given a name \fIstring\fR\&\&. The atom is created and does not change, so the return value can be saved and reused, which is faster than looking up the atom several times\&. .LP Notice that this function is \fInot\fR\& thread-safe, not even when the emulator with SMP support is used\&. .RE .LP .B ErlDrvTermData driver_mk_port(ErlDrvPort port) .br .RS .LP Converts a port handle to the Erlang term format, usable in \fB\fIerl_drv_output_term\fR\&\fR\& and \fB\fIerl_drv_send_term\fR\&\fR\&\&. .LP Notice that this function is \fInot\fR\& thread-safe, not even when the emulator with SMP support is used\&. .RE .LP .B int driver_monitor_process(ErlDrvPort port, ErlDrvTermData process, ErlDrvMonitor *monitor) .br .RS .LP Starts monitoring a process from a driver\&. When a process is monitored, a process exit results in a call to the provided \fB\fIprocess_exit\fR\&\fR\& callback in the \fB\fIErlDrvEntry\fR\&\fR\& structure\&. The \fIErlDrvMonitor\fR\& structure is filled in, for later removal or compare\&. .LP Parameter \fIprocess\fR\& is to be the return value of an earlier call to \fB\fIdriver_caller\fR\&\fR\& or \fB\fIdriver_connected\fR\&\fR\& call\&. .LP Returns \fI0\fR\& on success, < 0 if no callback is provided, and > 0 if the process is no longer alive\&. .RE .LP .B int driver_output(ErlDrvPort port, char *buf, ErlDrvSizeT len) .br .RS .LP Sends data from the driver up to the emulator\&. The data is received as terms or binary data, depending on how the driver port was opened\&. .LP The data is queued in the port owner process\&' message queue\&. Notice that this does not yield to the emulator (as the driver and the emulator run in the same thread)\&. .LP Parameter \fIbuf\fR\& points to the data to send, and \fIlen\fR\& is the number of bytes\&. .LP The return value for all output functions is \fI0\fR\& for normal use\&. If the driver is used for distribution, it can fail and return \fI-1\fR\&\&. .RE .LP .B int driver_output_binary(ErlDrvPort port, char *hbuf, ErlDrvSizeT hlen, ErlDrvBinary* bin, ErlDrvSizeT offset, ErlDrvSizeT len) .br .RS .LP Sends data to a port owner process from a driver binary\&. It has a header buffer (\fIhbuf\fR\& and \fIhlen\fR\&) just like \fB\fIdriver_output2\fR\&\fR\&\&. Parameter \fIhbuf\fR\& can be \fINULL\fR\&\&. .LP Parameter \fIoffset\fR\& is an offset into the binary and \fIlen\fR\& is the number of bytes to send\&. .LP Driver binaries are created with \fB\fIdriver_alloc_binary\fR\&\fR\&\&. .LP The data in the header is sent as a list and the binary as an Erlang binary in the tail of the list\&. .LP For example, if \fIhlen\fR\& is \fI2\fR\&, the port owner process receives \fI[H1, H2 | <>]\fR\&\&. .LP The return value is \fI0\fR\& for normal use\&. .LP Notice that, using the binary syntax in Erlang, the driver application can match the header directly from the binary, so the header can be put in the binary, and \fIhlen\fR\& can be set to \fI0\fR\&\&. .RE .LP .B int driver_output_term(ErlDrvPort port, ErlDrvTermData* term, int n) .br .RS .LP .RS -4 .B Warning: .RE \fIThis function is deprecated\&.\fR\& Use \fB\fIerl_drv_output_term\fR\&\fR\&instead\&. .LP Parameters \fIterm\fR\& and \fIn\fR\& work as in \fB\fIerl_drv_output_term\fR\&\fR\&\&. .LP Notice that this function is \fInot\fR\& thread-safe, not even when the emulator with SMP support is used\&. .RE .LP .B int driver_output2(ErlDrvPort port, char *hbuf, ErlDrvSizeT hlen, char *buf, ErlDrvSizeT len) .br .RS .LP First sends \fIhbuf\fR\& (length in \fIhlen\fR\&) data as a list, regardless of port settings\&. Then sends \fIbuf\fR\& as a binary or list\&. For example, if \fIhlen\fR\& is \fI3\fR\&, the port owner process receives \fI[H1, H2, H3 | T]\fR\&\&. .LP The point of sending data as a list header, is to facilitate matching on the data received\&. .LP The return value is \fI0\fR\& for normal use\&. .RE .LP .B int driver_outputv(ErlDrvPort port, char* hbuf, ErlDrvSizeT hlen, ErlIOVec *ev, ErlDrvSizeT skip) .br .RS .LP Sends data from an I/O vector, \fIev\fR\&, to the port owner process\&. It has a header buffer (\fIhbuf\fR\& and \fIhlen\fR\&), just like \fB\fIdriver_output2\fR\&\fR\&\&. .LP Parameter \fIskip\fR\& is a number of bytes to skip of the \fIev\fR\& vector from the head\&. .LP You get vectors of \fIErlIOVec\fR\& type from the driver queue (see below), and the \fB\fIoutputv\fR\&\fR\& driver entry function\&. You can also make them yourself, if you want to send several \fIErlDrvBinary\fR\& buffers at once\&. Often it is faster to use \fB\fIdriver_output\fR\&\fR\& or \fB\fR\&\&. .LP For example, if \fIhlen\fR\& is \fI2\fR\& and \fIev\fR\& points to an array of three binaries, the port owner process receives \fI[H1, H2, <>, <> | <>]\fR\&\&. .LP The return value is \fI0\fR\& for normal use\&. .LP The comment for \fIdriver_output_binary\fR\& also applies for \fIdriver_outputv\fR\&\&. .RE .LP .B ErlDrvPDL driver_pdl_create(ErlDrvPort port) .br .RS .LP Creates a port data lock associated with the \fIport\fR\&\&. .LP .RS -4 .B Note: .RE Once a port data lock has been created, it must be locked during all operations on the driver queue of the \fIport\fR\&\&. .LP Returns a newly created port data lock on success, otherwise \fINULL\fR\&\&. The function fails if \fIport\fR\& is invalid or if a port data lock already has been associated with the \fIport\fR\&\&. .RE .LP .B long driver_pdl_dec_refc(ErlDrvPDL pdl) .br .RS .LP Decrements the reference count of the port data lock passed as argument (\fIpdl\fR\&)\&. .LP The current reference count after the decrement has been performed is returned\&. .LP This function is thread-safe\&. .RE .LP .B long driver_pdl_get_refc(ErlDrvPDL pdl) .br .RS .LP Returns the current reference count of the port data lock passed as argument (\fIpdl\fR\&)\&. .LP This function is thread-safe\&. .RE .LP .B long driver_pdl_inc_refc(ErlDrvPDL pdl) .br .RS .LP Increments the reference count of the port data lock passed as argument (\fIpdl\fR\&)\&. .LP The current reference count after the increment has been performed is returned\&. .LP This function is thread-safe\&. .RE .LP .B void driver_pdl_lock(ErlDrvPDL pdl) .br .RS .LP Locks the port data lock passed as argument (\fIpdl\fR\&)\&. .LP This function is thread-safe\&. .RE .LP .B void driver_pdl_unlock(ErlDrvPDL pdl) .br .RS .LP Unlocks the port data lock passed as argument (\fIpdl\fR\&)\&. .LP This function is thread-safe\&. .RE .LP .B SysIOVec *driver_peekq(ErlDrvPort port, int *vlen) .br .RS .LP Retrieves the driver queue as a pointer to an array of \fISysIOVec\fR\&s\&. It also returns the number of elements in \fIvlen\fR\&\&. This is one of two ways to get data out of the queue\&. .LP Nothing is removed from the queue by this function, that must be done with \fB\fIdriver_deq\fR\&\fR\&\&. .LP The returned array is suitable to use with the Unix system call \fIwritev\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B ErlDrvSizeT driver_peekqv(ErlDrvPort port, ErlIOVec *ev) .br .RS .LP Retrieves the driver queue into a supplied \fIErlIOVec\fR\& \fIev\fR\&\&. It also returns the queue size\&. This is one of two ways to get data out of the queue\&. .LP If \fIev\fR\& is \fINULL\fR\&, all ones that is \fI-1\fR\& type cast to \fIErlDrvSizeT\fR\& are returned\&. .LP Nothing is removed from the queue by this function, that must be done with \fB\fIdriver_deq\fR\&\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_pushq(ErlDrvPort port, char* buf, ErlDrvSizeT len) .br .RS .LP Puts data at the head of the driver queue\&. The data in \fIbuf\fR\& is copied (\fIlen\fR\& bytes) and placed at the beginning of the queue\&. .LP The return value is \fI0\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_pushq_bin(ErlDrvPort port, ErlDrvBinary *bin, ErlDrvSizeT offset, ErlDrvSizeT len) .br .RS .LP Puts data in the binary \fIbin\fR\&, at \fIoffset\fR\& with length \fIlen\fR\& at the head of the driver queue\&. It is most often faster than \fB\fIdriver_pushq\fR\&\fR\&, because no data must be copied\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B int driver_pushqv(ErlDrvPort port, ErlIOVec *ev, ErlDrvSizeT skip) .br .RS .LP Puts the data in \fIev\fR\&, skipping the first \fIskip\fR\& bytes of it, at the head of the driver queue\&. It is faster than \fB\fIdriver_pushq\fR\&\fR\&, because no data must be copied\&. .LP The return value is \fI0\fR\&\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B int driver_read_timer(ErlDrvPort port, unsigned long *time_left) .br .RS .LP Reads the current time of a timer, and places the result in \fItime_left\fR\&\&. This is the time in milliseconds, before the time-out occurs\&. .LP The return value is \fI0\fR\&\&. .RE .LP .B void *driver_realloc(void *ptr, ErlDrvSizeT size) .br .RS .LP Resizes a memory block, either in place, or by allocating a new block, copying the data, and freeing the old block\&. A pointer is returned to the reallocated memory\&. On failure (out of memory), \fINULL\fR\& is returned\&. (This is most often a wrapper for \fIrealloc\fR\&\&.) .LP This function is thread-safe\&. .RE .LP .B ErlDrvBinary *driver_realloc_binary(ErlDrvBinary *bin, ErlDrvSizeT size) .br .RS .LP Resizes a driver binary, while keeping the data\&. .LP Returns the resized driver binary on success\&. Returns \fINULL\fR\& on failure (out of memory)\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B int driver_select(ErlDrvPort port, ErlDrvEvent event, int mode, int on) .br .RS .LP This function is used by drivers to provide the emulator with events to check for\&. This enables the emulator to call the driver when something has occurred asynchronously\&. .LP Parameter \fIevent\fR\& identifies an OS-specific event object\&. On Unix systems, the functions \fIselect\fR\&/\fIpoll\fR\& are used\&. The event object must be a socket or pipe (or other object that \fIselect\fR\&/\fIpoll\fR\& can use)\&. On Windows, the Win32 API function \fIWaitForMultipleObjects\fR\& is used\&. This places other restrictions on the event object; see the Win32 SDK documentation\&. .LP Parameter \fIon\fR\& is to be \fI1\fR\& for setting events and \fI0\fR\& for clearing them\&. .LP Parameter \fImode\fR\& is a bitwise OR combination of \fIERL_DRV_READ\fR\&, \fIERL_DRV_WRITE\fR\&, and \fIERL_DRV_USE\fR\&\&. The first two specify whether to wait for read events and/or write events\&. A fired read event calls \fB\fIready_input\fR\&\fR\& and a fired write event calls \fB\fIready_output\fR\&\fR\&\&. .LP .RS -4 .B Note: .RE Some OS (Windows) do not differentiate between read and write events\&. The callback for a fired event then only depends on the value of \fImode\fR\&\&. .LP \fIERL_DRV_USE\fR\& specifies if we are using the event object or if we want to close it\&. On an emulator with SMP support, it is not safe to clear all events and then close the event object after \fIdriver_select\fR\& has returned\&. Another thread can still be using the event object internally\&. To safely close an event object, call \fIdriver_select\fR\& with \fIERL_DRV_USE\fR\& and \fIon==0\fR\&, which clears all events and then either calls \fB\fIstop_select\fR\&\fR\& or schedules it to be called when it is safe to close the event object\&. \fIERL_DRV_USE\fR\& is to be set together with the first event for an event object\&. It is harmless to set \fIERL_DRV_USE\fR\& even if it already has been done\&. Clearing all events but keeping \fIERL_DRV_USE\fR\& set indicates that we are using the event object and probably will set events for it again\&. .LP .RS -4 .B Note: .RE \fIERL_DRV_USE\fR\& was added in Erlang/OTP R13\&. Old drivers still work as before, but it is recommended to update them to use \fIERL_DRV_USE\fR\& and \fIstop_select\fR\& to ensure that event objects are closed in a safe way\&. .LP The return value is \fI0\fR\&, unless \fIready_input\fR\&/\fIready_output\fR\& is \fINULL\fR\&, in which case it is \fI-1\fR\&\&. .RE .LP .B int driver_send_term(ErlDrvPort port, ErlDrvTermData receiver, ErlDrvTermData* term, int n) .br .RS .LP .RS -4 .B Warning: .RE \fIThis function is deprecated\&.\fR\& Use \fB\fIerl_drv_send_term\fR\&\fR\& instead\&. .LP .RS -4 .B Note: .RE The parameters of this function cannot be properly checked by the runtime system when executed by arbitrary threads\&. This can cause the function not to fail when it should\&. .LP Parameters \fIterm\fR\& and \fIn\fR\& work as in \fB\fIerl_drv_output_term\fR\&\fR\&\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B int driver_set_timer(ErlDrvPort port, unsigned long time) .br .RS .LP Sets a timer on the driver, which will count down and call the driver when it is timed out\&. Parameter \fItime\fR\& is the time in milliseconds before the timer expires\&. .LP When the timer reaches \fI0\fR\& and expires, the driver entry function \fB\fItimeout\fR\&\fR\& is called\&. .LP Notice that only one timer exists on each driver instance; setting a new timer replaces an older one\&. .LP Return value is \fI0\fR\&, unless the \fItimeout\fR\& driver function is \fINULL\fR\&, in which case it is \fI-1\fR\&\&. .RE .LP .B ErlDrvSizeT driver_sizeq(ErlDrvPort port) .br .RS .LP Returns the number of bytes currently in the driver queue\&. .LP This function can be called from any thread if a \fBport data lock\fR\& associated with the \fIport\fR\& is locked by the calling thread during the call\&. .RE .LP .B void driver_system_info(ErlDrvSysInfo *sys_info_ptr, size_t size) .br .RS .LP Writes information about the Erlang runtime system into the \fB\fIErlDrvSysInfo\fR\&\fR\& structure referred to by the first argument\&. The second argument is to be the size of the \fB\fIErlDrvSysInfo\fR\&\fR\& structure, that is, \fIsizeof(ErlDrvSysInfo)\fR\&\&. .LP For information about specific fields, see \fB\fIErlDrvSysInfo\fR\&\fR\&\&. .RE .LP .B ErlDrvSizeT driver_vec_to_buf(ErlIOVec *ev, char *buf, ErlDrvSizeT len) .br .RS .LP Collects several segments of data, referenced by \fIev\fR\&, by copying them in order to the buffer \fIbuf\fR\&, of the size \fIlen\fR\&\&. .LP If the data is to be sent from the driver to the port owner process, it is faster to use \fB\fIdriver_outputv\fR\&\fR\&\&. .LP The return value is the space left in the buffer, that is, if \fIev\fR\& contains less than \fIlen\fR\& bytes it is the difference, and if \fIev\fR\& contains \fIlen\fR\& bytes or more, it is \fI0\fR\&\&. This is faster if there is more than one header byte, as the binary syntax can construct integers directly from the binary\&. .RE .LP .B void erl_drv_busy_msgq_limits(ErlDrvPort port, ErlDrvSizeT *low, ErlDrvSizeT *high) .br .RS .LP Sets and gets limits that will be used for controlling the busy state of the port message queue\&. .LP The port message queue is set into a busy state when the amount of command data queued on the message queue reaches the \fIhigh\fR\& limit\&. The port message queue is set into a not busy state when the amount of command data queued on the message queue falls below the \fIlow\fR\& limit\&. Command data is in this context data passed to the port using either \fIPort ! {Owner, {command, Data}}\fR\& or \fIport_command/[2,3]\fR\&\&. Notice that these limits only concerns command data that have not yet reached the port\&. The \fBbusy port\fR\& feature can be used for data that has reached the port\&. .LP Valid limits are values in the range \fI[ERL_DRV_BUSY_MSGQ_LIM_MIN, ERL_DRV_BUSY_MSGQ_LIM_MAX]\fR\&\&. Limits are automatically adjusted to be sane\&. That is, the system adjusts values so that the low limit used is lower than or equal to the high limit used\&. By default the high limit is 8 kB and the low limit is 4 kB\&. .LP By passing a pointer to an integer variable containing the value \fIERL_DRV_BUSY_MSGQ_READ_ONLY\fR\&, the currently used limit is read and written back to the integer variable\&. A new limit can be set by passing a pointer to an integer variable containing a valid limit\&. The passed value is written to the internal limit\&. The internal limit is then adjusted\&. After this the adjusted limit is written back to the integer variable from which the new value was read\&. Values are in bytes\&. .LP The busy message queue feature can be disabled either by setting the \fIERL_DRV_FLAG_NO_BUSY_MSGQ\fR\& \fBdriver flag\fR\& in the \fB\fIdriver_entry\fR\&\fR\& used by the driver, or by calling this function with \fIERL_DRV_BUSY_MSGQ_DISABLED\fR\& as a limit (either low or high)\&. When this feature has been disabled, it cannot be enabled again\&. When reading the limits, both are \fIERL_DRV_BUSY_MSGQ_DISABLED\fR\& if this feature has been disabled\&. .LP Processes sending command data to the port are suspended if either the port is busy or if the port message queue is busy\&. Suspended processes are resumed when neither the port or the port message queue is busy\&. .LP For information about busy port functionality, see \fB\fIset_busy_port\fR\&\fR\&\&. .RE .LP .B void erl_drv_cond_broadcast(ErlDrvCond *cnd) .br .RS .LP Broadcasts on a condition variable\&. That is, if other threads are waiting on the condition variable being broadcast on, \fIall\fR\& of them are woken\&. .LP \fIcnd\fR\& is a pointer to a condition variable to broadcast on\&. .LP This function is thread-safe\&. .RE .LP .B ErlDrvCond *erl_drv_cond_create(char *name) .br .RS .LP Creates a condition variable and returns a pointer to it\&. .LP \fIname\fR\& is a string identifying the created condition variable\&. It is used to identify the condition variable in planned future debug functionality\&. .LP Returns \fINULL\fR\& on failure\&. The driver creating the condition variable is responsible for destroying it before the driver is unloaded\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_cond_destroy(ErlDrvCond *cnd) .br .RS .LP Destroys a condition variable previously created by \fB\fIerl_drv_cond_create\fR\&\fR\&\&. .LP \fIcnd\fR\& is a pointer to a condition variable to destroy\&. .LP This function is thread-safe\&. .RE .LP .B char *erl_drv_cond_name(ErlDrvCond *cnd) .br .RS .LP Returns a pointer to the name of the condition\&. .LP \fIcnd\fR\& is a pointer to an initialized condition\&. .LP .RS -4 .B Note: .RE This function is intended for debugging purposes only\&. .RE .LP .B void erl_drv_cond_signal(ErlDrvCond *cnd) .br .RS .LP Signals on a condition variable\&. That is, if other threads are waiting on the condition variable being signaled, \fIone\fR\& of them is woken\&. .LP \fIcnd\fR\& is a pointer to a condition variable to signal on\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_cond_wait(ErlDrvCond *cnd, ErlDrvMutex *mtx) .br .RS .LP Waits on a condition variable\&. The calling thread is blocked until another thread wakes it by signaling or broadcasting on the condition variable\&. Before the calling thread is blocked, it unlocks the mutex passed as argument\&. When the calling thread is woken, it locks the same mutex before returning\&. That is, the mutex currently must be locked by the calling thread when calling this function\&. .LP \fIcnd\fR\& is a pointer to a condition variable to wait on\&. \fImtx\fR\& is a pointer to a mutex to unlock while waiting\&. .LP .RS -4 .B Note: .RE \fIerl_drv_cond_wait\fR\& can return even if no one has signaled or broadcast on the condition variable\&. Code calling \fIerl_drv_cond_wait\fR\& is always to be prepared for \fIerl_drv_cond_wait\fR\& returning even if the condition that the thread was waiting for has not occurred\&. That is, when returning from \fIerl_drv_cond_wait\fR\&, always check if the condition has occurred, and if not call \fIerl_drv_cond_wait\fR\& again\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_consume_timeslice(ErlDrvPort port, int percent) .br .RS .LP Gives the runtime system a hint about how much CPU time the current driver callback call has consumed since the last hint, or since the the start of the callback if no previous hint has been given\&. .RS 2 .TP 2 .B \fIport\fR\&: Port handle of the executing port\&. .TP 2 .B \fIpercent\fR\&: Approximate consumed fraction of a full time-slice in percent\&. .RE .LP The time is specified as a fraction, in percent, of a full time-slice that a port is allowed to execute before it is to surrender the CPU to other runnable ports or processes\&. Valid range is \fI[1, 100]\fR\&\&. The scheduling time-slice is not an exact entity, but can usually be approximated to about 1 millisecond\&. .LP Notice that it is up to the runtime system to determine if and how to use this information\&. Implementations on some platforms can use other means to determine the consumed fraction of the time-slice\&. Lengthy driver callbacks should, regardless of this, frequently call this function to determine if it is allowed to continue execution or not\&. .LP This function returns a non-zero value if the time-slice has been exhausted, and zero if the callback is allowed to continue execution\&. If a non-zero value is returned, the driver callback is to return as soon as possible in order for the port to be able to yield\&. .LP This function is provided to better support co-operative scheduling, improve system responsiveness, and to make it easier to prevent misbehaviors of the VM because of a port monopolizing a scheduler thread\&. It can be used when dividing lengthy work into some repeated driver callback calls, without the need to use threads\&. .LP See also the important \fBwarning\fR\& text at the beginning of this manual page\&. .RE .LP .B ErlDrvTime erl_drv_convert_time_unit(ErlDrvTime val, ErlDrvTimeUnit from, ErlDrvTimeUnit to) .br .RS .LP Converts the \fIval\fR\& value of time unit \fIfrom\fR\& to the corresponding value of time unit \fIto\fR\&\&. The result is rounded using the floor function\&. .RS 2 .TP 2 .B \fIval\fR\&: Value to convert time unit for\&. .TP 2 .B \fIfrom\fR\&: Time unit of \fIval\fR\&\&. .TP 2 .B \fIto\fR\&: Time unit of returned value\&. .RE .LP Returns \fIERL_DRV_TIME_ERROR\fR\& if called with an invalid time unit argument\&. .LP See also \fB\fIErlDrvTime\fR\&\fR\& and \fB\fIErlDrvTimeUnit\fR\&\fR\&\&. .RE .LP .B int erl_drv_equal_tids(ErlDrvTid tid1, ErlDrvTid tid2) .br .RS .LP Compares two thread identifiers, \fItid1\fR\& and \fItid2\fR\&, for equality\&. .LP Returns \fI0\fR\& it they are not equal, and a value not equal to \fI0\fR\& if they are equal\&. .LP .RS -4 .B Note: .RE A thread identifier can be reused very quickly after a thread has terminated\&. Therefore, if a thread corresponding to one of the involved thread identifiers has terminated since the thread identifier was saved, the result of \fIerl_drv_equal_tids\fR\& does possibly not give the expected result\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_getenv(const char *key, char *value, size_t *value_size) .br .RS .LP Retrieves the value of an environment variable\&. .RS 2 .TP 2 .B \fIkey\fR\&: A \fINULL\fR\&-terminated string containing the name of the environment variable\&. .TP 2 .B \fIvalue\fR\&: A pointer to an output buffer\&. .TP 2 .B \fIvalue_size\fR\&: A pointer to an integer\&. The integer is used both for passing input and output sizes (see below)\&. .RE .LP When this function is called, \fI*value_size\fR\& is to contain the size of the \fIvalue\fR\& buffer\&. .LP On success, \fI0\fR\& is returned, the value of the environment variable has been written to the \fIvalue\fR\& buffer, and \fI*value_size\fR\& contains the string length (excluding the terminating \fINULL\fR\& character) of the value written to the \fIvalue\fR\& buffer\&. .LP On failure, that is, no such environment variable was found, a value < \fI0\fR\& is returned\&. When the size of the \fIvalue\fR\& buffer is too small, a value > \fI0\fR\& is returned and \fI*value_size\fR\& has been set to the buffer size needed\&. .LP .RS -4 .B Warning: .RE Do \fInot\fR\& use libc\&'s \fIgetenv\fR\& or similar C library interfaces from a driver\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_init_ack(ErlDrvPort port, ErlDrvData res) .br .RS .LP Acknowledges the start of the port\&. .RS 2 .TP 2 .B \fIport\fR\&: The port handle of the port (driver instance) doing the acknowledgment\&. .TP 2 .B \fIres\fR\&: The result of the port initialization\&. Can be the same values as the return value of \fB\fIstart\fR\&\fR\&, that is, any of the error codes or the \fIErlDrvData\fR\& that is to be used for this port\&. .RE .LP When this function is called the initiating \fIerlang:open_port\fR\& call is returned as if the \fB\fIstart\fR\&\fR\& function had just been called\&. It can only be used when flag \fB\fIERL_DRV_FLAG_USE_INIT_ACK\fR\&\fR\& has been set on the linked-in driver\&. .RE .LP .B ErlDrvTime erl_drv_monotonic_time(ErlDrvTimeUnit time_unit) .br .RS .LP Returns \fB Erlang monotonic time\fR\&\&. Notice that negative values are not uncommon\&. .LP \fItime_unit\fR\& is time unit of returned value\&. .LP Returns \fIERL_DRV_TIME_ERROR\fR\& if called with an invalid time unit argument, or if called from a thread that is not a scheduler thread\&. .LP See also \fB\fIErlDrvTime\fR\&\fR\& and \fB\fIErlDrvTimeUnit\fR\&\fR\&\&. .RE .LP .B ErlDrvMutex *erl_drv_mutex_create(char *name) .br .RS .LP Creates a mutex and returns a pointer to it\&. .LP \fIname\fR\& is a string identifying the created mutex\&. It is used to identify the mutex in planned future debug functionality\&. .LP Returns \fINULL\fR\& on failure\&. The driver creating the mutex is responsible for destroying it before the driver is unloaded\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_mutex_destroy(ErlDrvMutex *mtx) .br .RS .LP Destroys a mutex previously created by \fB\fIerl_drv_mutex_create\fR\&\fR\&\&. The mutex must be in an unlocked state before it is destroyed\&. .LP \fImtx\fR\& is a pointer to a mutex to destroy\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_mutex_lock(ErlDrvMutex *mtx) .br .RS .LP Locks a mutex\&. The calling thread is blocked until the mutex has been locked\&. A thread that has currently locked the mutex \fIcannot\fR\& lock the same mutex again\&. .LP \fImtx\fR\& is a pointer to a mutex to lock\&. .LP .RS -4 .B Warning: .RE If you leave a mutex locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B char *erl_drv_mutex_name(ErlDrvMutex *mtx) .br .RS .LP Returns a pointer to the mutex name\&. .LP \fImtx\fR\& is a pointer to an initialized mutex\&. .LP .RS -4 .B Note: .RE This function is intended for debugging purposes only\&. .RE .LP .B int erl_drv_mutex_trylock(ErlDrvMutex *mtx) .br .RS .LP Tries to lock a mutex\&. A thread that has currently locked the mutex \fIcannot\fR\& try to lock the same mutex again\&. .LP \fImtx\fR\& is a pointer to a mutex to try to lock\&. .LP Returns \fI0\fR\& on success, otherwise \fIEBUSY\fR\&\&. .LP .RS -4 .B Warning: .RE If you leave a mutex locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_mutex_unlock(ErlDrvMutex *mtx) .br .RS .LP Unlocks a mutex\&. The mutex currently must be locked by the calling thread\&. .LP \fImtx\fR\& is a pointer to a mutex to unlock\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_output_term(ErlDrvTermData port, ErlDrvTermData* term, int n) .br .RS .LP Sends data in the special driver term format to the port owner process\&. This is a fast way to deliver term data from a driver\&. It needs no binary conversion, so the port owner process receives data as normal Erlang terms\&. The \fB\fIerl_drv_send_term\fR\&\fR\& functions can be used for sending to any process on the local node\&. .LP .RS -4 .B Note: .RE Parameter \fIport\fR\& is \fInot\fR\& an ordinary port handle, but a port handle converted using \fB\fIdriver_mk_port\fR\&\fR\&\&. .LP Parameter \fIterm\fR\& points to an array of \fIErlDrvTermData\fR\& with \fIn\fR\& elements\&. This array contains terms described in the driver term format\&. Every term consists of 1-4 elements in the array\&. The first term has a term type and then arguments\&. Parameter \fIport\fR\& specifies the sending port\&. .LP Tuples, maps, and lists (except strings, see below) are built in reverse polish notation, so that to build a tuple, the elements are specified first, and then the tuple term, with a count\&. Likewise for lists and maps\&. .RS 2 .TP 2 * A tuple must be specified with the number of elements\&. (The elements precede the \fIERL_DRV_TUPLE\fR\& term\&.) .LP .TP 2 * A map must be specified with the number of key-value pairs \fIN\fR\&\&. The key-value pairs must precede the \fIERL_DRV_MAP\fR\& in this order: \fIkey1,value1,key2,value2,\&.\&.\&.,keyN,valueN\fR\&\&. Duplicate keys are not allowed\&. .LP .TP 2 * A list must be specified with the number of elements, including the tail, which is the last term preceding \fIERL_DRV_LIST\fR\&\&. .LP .RE .LP The special term \fIERL_DRV_STRING_CONS\fR\& is used to "splice" in a string in a list, a string specified this way is not a list in itself, but the elements are elements of the surrounding list\&. .LP .nf Term type Arguments --------- --------- ERL_DRV_NIL ERL_DRV_ATOM ErlDrvTermData atom (from driver_mk_atom(char *string)) ERL_DRV_INT ErlDrvSInt integer ERL_DRV_UINT ErlDrvUInt integer ERL_DRV_INT64 ErlDrvSInt64 *integer_ptr ERL_DRV_UINT64 ErlDrvUInt64 *integer_ptr ERL_DRV_PORT ErlDrvTermData port (from driver_mk_port(ErlDrvPort port)) ERL_DRV_BINARY ErlDrvBinary *bin, ErlDrvUInt len, ErlDrvUInt offset ERL_DRV_BUF2BINARY char *buf, ErlDrvUInt len ERL_DRV_STRING char *str, int len ERL_DRV_TUPLE int sz ERL_DRV_LIST int sz ERL_DRV_PID ErlDrvTermData pid (from driver_connected(ErlDrvPort port) or driver_caller(ErlDrvPort port)) ERL_DRV_STRING_CONS char *str, int len ERL_DRV_FLOAT double *dbl ERL_DRV_EXT2TERM char *buf, ErlDrvUInt len ERL_DRV_MAP int sz .fi .LP The unsigned integer data type \fIErlDrvUInt\fR\& and the signed integer data type \fIErlDrvSInt\fR\& are 64 bits wide on a 64-bit runtime system and 32 bits wide on a 32-bit runtime system\&. They were introduced in ERTS 5\&.6 and replaced some of the \fIint\fR\& arguments in the list above\&. .LP The unsigned integer data type \fIErlDrvUInt64\fR\& and the signed integer data type \fIErlDrvSInt64\fR\& are always 64 bits wide\&. They were introduced in ERTS 5\&.7\&.4\&. .LP To build the tuple \fI{tcp, Port, [100 | Binary]}\fR\&, the following call can be made\&. .LP .nf ErlDrvBinary* bin = ... ErlDrvPort port = ... ErlDrvTermData spec[] = { ERL_DRV_ATOM, driver_mk_atom("tcp"), ERL_DRV_PORT, driver_mk_port(drvport), ERL_DRV_INT, 100, ERL_DRV_BINARY, bin, 50, 0, ERL_DRV_LIST, 2, ERL_DRV_TUPLE, 3, }; erl_drv_output_term(driver_mk_port(drvport), spec, sizeof(spec) / sizeof(spec[0])); .fi .LP Here \fIbin\fR\& is a driver binary of length at least 50 and \fIdrvport\fR\& is a port handle\&. Notice that \fIERL_DRV_LIST\fR\& comes after the elements of the list, likewise \fIERL_DRV_TUPLE\fR\&\&. .LP The \fIERL_DRV_STRING_CONS\fR\& term is a way to construct strings\&. It works differently from how \fIERL_DRV_STRING\fR\& works\&. \fIERL_DRV_STRING_CONS\fR\& builds a string list in reverse order (as opposed to how \fIERL_DRV_LIST\fR\& works), concatenating the strings added to a list\&. The tail must be specified before \fIERL_DRV_STRING_CONS\fR\&\&. .LP \fIERL_DRV_STRING\fR\& constructs a string, and ends it\&. (So it is the same as \fIERL_DRV_NIL\fR\& followed by \fIERL_DRV_STRING_CONS\fR\&\&.) .LP .nf /* to send [x, "abc", y] to the port: */ ErlDrvTermData spec[] = { ERL_DRV_ATOM, driver_mk_atom("x"), ERL_DRV_STRING, (ErlDrvTermData)"abc", 3, ERL_DRV_ATOM, driver_mk_atom("y"), ERL_DRV_NIL, ERL_DRV_LIST, 4 }; erl_drv_output_term(driver_mk_port(drvport), spec, sizeof(spec) / sizeof(spec[0])); .fi .LP .nf /* to send "abc123" to the port: */ ErlDrvTermData spec[] = { ERL_DRV_NIL, /* with STRING_CONS, the tail comes first */ ERL_DRV_STRING_CONS, (ErlDrvTermData)"123", 3, ERL_DRV_STRING_CONS, (ErlDrvTermData)"abc", 3, }; erl_drv_output_term(driver_mk_port(drvport), spec, sizeof(spec) / sizeof(spec[0])); .fi .LP The \fIERL_DRV_EXT2TERM\fR\& term type is used for passing a term encoded with the \fBexternal format\fR\&, that is, a term that has been encoded by \fB\fIerlang:term_to_binary\fR\&\fR\&, \fB\fIerl_interface:ei(3erl)\fR\&\fR\&, and so on\&. For example, if \fIbinp\fR\& is a pointer to an \fIErlDrvBinary\fR\& that contains term \fI{17, 4711}\fR\& encoded with the \fBexternal format\fR\&, and you want to wrap it in a two-tuple with the tag \fImy_tag\fR\&, that is, \fI{my_tag, {17, 4711}}\fR\&, you can do as follows: .LP .nf ErlDrvTermData spec[] = { ERL_DRV_ATOM, driver_mk_atom("my_tag"), ERL_DRV_EXT2TERM, (ErlDrvTermData) binp->orig_bytes, binp->orig_size ERL_DRV_TUPLE, 2, }; erl_drv_output_term(driver_mk_port(drvport), spec, sizeof(spec) / sizeof(spec[0])); .fi .LP To build the map \fI#{key1 => 100, key2 => {200, 300}}\fR\&, the following call can be made\&. .LP .nf ErlDrvPort port = ... ErlDrvTermData spec[] = { ERL_DRV_ATOM, driver_mk_atom("key1"), ERL_DRV_INT, 100, ERL_DRV_ATOM, driver_mk_atom("key2"), ERL_DRV_INT, 200, ERL_DRV_INT, 300, ERL_DRV_TUPLE, 2, ERL_DRV_MAP, 2 }; erl_drv_output_term(driver_mk_port(drvport), spec, sizeof(spec) / sizeof(spec[0])); .fi .LP If you want to pass a binary and do not already have the content of the binary in an \fIErlDrvBinary\fR\&, you can benefit from using \fIERL_DRV_BUF2BINARY\fR\& instead of creating an \fIErlDrvBinary\fR\& through \fB\fIdriver_alloc_binary\fR\&\fR\& and then pass the binary through \fIERL_DRV_BINARY\fR\&\&. The runtime system often allocates binaries smarter if \fIERL_DRV_BUF2BINARY\fR\& is used\&. However, if the content of the binary to pass already resides in an \fIErlDrvBinary\fR\&, it is normally better to pass the binary using \fIERL_DRV_BINARY\fR\& and the \fIErlDrvBinary\fR\& in question\&. .LP The \fIERL_DRV_UINT\fR\&, \fIERL_DRV_BUF2BINARY\fR\&, and \fIERL_DRV_EXT2TERM\fR\& term types were introduced in ERTS 5\&.6\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B int erl_drv_putenv(const char *key, char *value) .br .RS .LP Sets the value of an environment variable\&. .LP \fIkey\fR\& is a \fINULL\fR\&-terminated string containing the name of the environment variable\&. .LP \fIvalue\fR\& is a \fINULL\fR\&-terminated string containing the new value of the environment variable\&. .LP Returns \fI0\fR\& on success, otherwise a value \fI!= 0\fR\&\&. .LP .RS -4 .B Note: .RE The result of passing the empty string (\fI""\fR\&) as a value is platform-dependent\&. On some platforms the variable value is set to the empty string, on others the environment variable is removed\&. .LP .RS -4 .B Warning: .RE Do \fInot\fR\& use libc\&'s \fIputenv\fR\& or similar C library interfaces from a driver\&. .LP This function is thread-safe\&. .RE .LP .B ErlDrvRWLock *erl_drv_rwlock_create(char *name) .br .RS .LP Creates an rwlock and returns a pointer to it\&. .LP \fIname\fR\& is a string identifying the created rwlock\&. It is used to identify the rwlock in planned future debug functionality\&. .LP Returns \fINULL\fR\& on failure\&. The driver creating the rwlock is responsible for destroying it before the driver is unloaded\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_rwlock_destroy(ErlDrvRWLock *rwlck) .br .RS .LP Destroys an rwlock previously created by \fB\fIerl_drv_rwlock_create\fR\&\fR\&\&. The rwlock must be in an unlocked state before it is destroyed\&. .LP \fIrwlck\fR\& is a pointer to an rwlock to destroy\&. .LP This function is thread-safe\&. .RE .LP .B char *erl_drv_rwlock_name(ErlDrvRWLock *rwlck) .br .RS .LP Returns a pointer to the name of the rwlock\&. .LP \fIrwlck\fR\& is a pointer to an initialized rwlock\&. .LP .RS -4 .B Note: .RE This function is intended for debugging purposes only\&. .RE .LP .B void erl_drv_rwlock_rlock(ErlDrvRWLock *rwlck) .br .RS .LP Read locks an rwlock\&. The calling thread is blocked until the rwlock has been read locked\&. A thread that currently has read or read/write locked the rwlock \fIcannot\fR\& lock the same rwlock again\&. .LP \fIrwlck\fR\& is a pointer to the rwlock to read lock\&. .LP .RS -4 .B Warning: .RE If you leave an rwlock locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_rwlock_runlock(ErlDrvRWLock *rwlck) .br .RS .LP Read unlocks an rwlock\&. The rwlock currently must be read locked by the calling thread\&. .LP \fIrwlck\fR\& is a pointer to an rwlock to read unlock\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_rwlock_rwlock(ErlDrvRWLock *rwlck) .br .RS .LP Read/write locks an rwlock\&. The calling thread is blocked until the rwlock has been read/write locked\&. A thread that currently has read or read/write locked the rwlock \fIcannot\fR\& lock the same rwlock again\&. .LP \fIrwlck\fR\& is a pointer to an rwlock to read/write lock\&. .LP .RS -4 .B Warning: .RE If you leave an rwlock locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_rwlock_rwunlock(ErlDrvRWLock *rwlck) .br .RS .LP Read/write unlocks an rwlock\&. The rwlock currently must be read/write locked by the calling thread\&. .LP \fIrwlck\fR\& is a pointer to an rwlock to read/write unlock\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_rwlock_tryrlock(ErlDrvRWLock *rwlck) .br .RS .LP Tries to read lock an rwlock\&. .LP \fIrwlck\fR\& is a pointer to an rwlock to try to read lock\&. .LP Returns \fI0\fR\& on success, otherwise \fIEBUSY\fR\&\&. A thread that currently has read or read/write locked the rwlock \fIcannot\fR\& try to lock the same rwlock again\&. .LP .RS -4 .B Warning: .RE If you leave an rwlock locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_rwlock_tryrwlock(ErlDrvRWLock *rwlck) .br .RS .LP Tries to read/write lock an rwlock\&. A thread that currently has read or read/write locked the rwlock \fIcannot\fR\& try to lock the same rwlock again\&. .LP \fIrwlck\fR\&is pointer to an rwlock to try to read/write lock\&. .LP Returns \fI0\fR\& on success, otherwise \fIEBUSY\fR\&\&. .LP .RS -4 .B Warning: .RE If you leave an rwlock locked in an emulator thread when you let the thread out of your control, you will \fIvery likely\fR\& deadlock the whole emulator\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_send_term(ErlDrvTermData port, ErlDrvTermData receiver, ErlDrvTermData* term, int n) .br .RS .LP This function is the only way for a driver to send data to \fIother\fR\& processes than the port owner process\&. Parameter \fIreceiver\fR\& specifies the process to receive the data\&. .LP .RS -4 .B Note: .RE Parameter \fIport\fR\& is \fInot\fR\& an ordinary port handle, but a port handle converted using \fB\fIdriver_mk_port\fR\&\fR\&\&. .LP Parameters \fIport\fR\&, \fIterm\fR\&, and \fIn\fR\& work as in \fB\fIerl_drv_output_term\fR\&\fR\&\&. .LP This function is only thread-safe when the emulator with SMP support is used\&. .RE .LP .B void erl_drv_set_os_pid(ErlDrvPort port, ErlDrvSInt pid) .br .RS .LP Sets the \fIos_pid\fR\& seen when doing \fB\fIerlang:port_info/2\fR\&\fR\& on this port\&. .LP \fIport\fR\& is the port handle of the port (driver instance) to set the pid on\&. \fIpid\fR\&is the pid to set\&. .RE .LP .B int erl_drv_thread_create(char *name, ErlDrvTid *tid, void * (*func)(void *), void *arg, ErlDrvThreadOpts *opts) .br .RS .LP Creates a new thread\&. .RS 2 .TP 2 .B \fIname\fR\&: A string identifying the created thread\&. It is used to identify the thread in planned future debug functionality\&. .TP 2 .B \fItid\fR\&: A pointer to a thread identifier variable\&. .TP 2 .B \fIfunc\fR\&: A pointer to a function to execute in the created thread\&. .TP 2 .B \fIarg\fR\&: A pointer to argument to the \fIfunc\fR\& function\&. .TP 2 .B \fIopts\fR\&: A pointer to thread options to use or \fINULL\fR\&\&. .RE .LP Returns \fI0\fR\& on success, otherwise an \fIerrno\fR\& value is returned to indicate the error\&. The newly created thread begins executing in the function pointed to by \fIfunc\fR\&, and \fIfunc\fR\& is passed \fIarg\fR\& as argument\&. When \fIerl_drv_thread_create\fR\& returns, the thread identifier of the newly created thread is available in \fI*tid\fR\&\&. \fIopts\fR\& can be either a \fINULL\fR\& pointer, or a pointer to an \fB\fIErlDrvThreadOpts\fR\&\fR\& structure\&. If \fIopts\fR\& is a \fINULL\fR\& pointer, default options are used, otherwise the passed options are used\&. .LP .RS -4 .B Warning: .RE You are not allowed to allocate the \fB\fIErlDrvThreadOpts\fR\&\fR\& structure by yourself\&. It must be allocated and initialized by \fB\fIerl_drv_thread_opts_create\fR\&\fR\&\&. .LP The created thread terminates either when \fIfunc\fR\& returns or if \fB\fIerl_drv_thread_exit\fR\&\fR\& is called by the thread\&. The exit value of the thread is either returned from \fIfunc\fR\& or passed as argument to \fB\fIerl_drv_thread_exit\fR\&\fR\&\&. The driver creating the thread is responsible for joining the thread, through \fB\fIerl_drv_thread_join\fR\&\fR\&, before the driver is unloaded\&. "Detached" threads cannot be created, that is, threads that do not need to be joined\&. .LP .RS -4 .B Warning: .RE All created threads must be joined by the driver before it is unloaded\&. If the driver fails to join all threads created before it is unloaded, the runtime system most likely crashes when the driver code is unloaded\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_thread_exit(void *exit_value) .br .RS .LP Terminates the calling thread with the exit value passed as argument\&. \fIexit_value\fR\& is a pointer to an exit value or \fINULL\fR\&\&. .LP You are only allowed to terminate threads created with \fB\fIerl_drv_thread_create\fR\&\fR\&\&. .LP The exit value can later be retrieved by another thread through \fB\fIerl_drv_thread_join\fR\&\fR\&\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_thread_join(ErlDrvTid tid, void **exit_value) .br .RS .LP Joins the calling thread with another thread, that is, the calling thread is blocked until the thread identified by \fItid\fR\& has terminated\&. .LP \fItid\fR\& is the thread identifier of the thread to join\&. \fIexit_value\fR\& is a pointer to a pointer to an exit value, or \fINULL\fR\&\&. .LP Returns \fI0\fR\& on success, otherwise an \fIerrno\fR\& value is returned to indicate the error\&. .LP A thread can only be joined once\&. The behavior of joining more than once is undefined, an emulator crash is likely\&. If \fIexit_value == NULL\fR\&, the exit value of the terminated thread is ignored, otherwise the exit value of the terminated thread is stored at \fI*exit_value\fR\&\&. .LP This function is thread-safe\&. .RE .LP .B char *erl_drv_thread_name(ErlDrvTid tid) .br .RS .LP Returns a pointer to the name of the thread\&. .LP \fItid\fR\& is a thread identifier\&. .LP .RS -4 .B Note: .RE This function is intended for debugging purposes only\&. .RE .LP .B ErlDrvThreadOpts *erl_drv_thread_opts_create(char *name) .br .RS .LP Allocates and initializes a thread option structure\&. .LP \fIname\fR\& is a string identifying the created thread options\&. It is used to identify the thread options in planned future debug functionality\&. .LP Returns \fINULL\fR\& on failure\&. A thread option structure is used for passing options to \fB\fIerl_drv_thread_create\fR\&\fR\&\&. If the structure is not modified before it is passed to \fB\fIerl_drv_thread_create\fR\&\fR\&, the default values are used\&. .LP .RS -4 .B Warning: .RE You are not allowed to allocate the \fB\fIErlDrvThreadOpts\fR\&\fR\& structure by yourself\&. It must be allocated and initialized by \fIerl_drv_thread_opts_create\fR\&\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_thread_opts_destroy(ErlDrvThreadOpts *opts) .br .RS .LP Destroys thread options previously created by \fB\fIerl_drv_thread_opts_create\fR\&\fR\&\&. .LP \fIopts\fR\& is a pointer to thread options to destroy\&. .LP This function is thread-safe\&. .RE .LP .B ErlDrvTid erl_drv_thread_self(void) .br .RS .LP Returns the thread identifier of the calling thread\&. .LP This function is thread-safe\&. .RE .LP .B ErlDrvTime erl_drv_time_offset(ErlDrvTimeUnit time_unit) .br .RS .LP Returns the current time offset between \fB Erlang monotonic time\fR\& and \fB Erlang system time\fR\& converted into the \fItime_unit\fR\& passed as argument\&. .LP \fItime_unit\fR\& is time unit of returned value\&. .LP Returns \fIERL_DRV_TIME_ERROR\fR\& if called with an invalid time unit argument, or if called from a thread that is not a scheduler thread\&. .LP See also \fB\fIErlDrvTime\fR\&\fR\& and \fB\fIErlDrvTimeUnit\fR\&\fR\&\&. .RE .LP .B void *erl_drv_tsd_get(ErlDrvTSDKey key) .br .RS .LP Returns the thread-specific data associated with \fIkey\fR\& for the calling thread\&. .LP \fIkey\fR\& is a thread-specific data key\&. .LP Returns \fINULL\fR\& if no data has been associated with \fIkey\fR\& for the calling thread\&. .LP This function is thread-safe\&. .RE .LP .B int erl_drv_tsd_key_create(char *name, ErlDrvTSDKey *key) .br .RS .LP Creates a thread-specific data key\&. .LP \fIname\fR\& is a string identifying the created key\&. It is used to identify the key in planned future debug functionality\&. .LP \fIkey\fR\& is a pointer to a thread-specific data key variable\&. .LP Returns \fI0\fR\& on success, otherwise an \fIerrno\fR\& value is returned to indicate the error\&. The driver creating the key is responsible for destroying it before the driver is unloaded\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_tsd_key_destroy(ErlDrvTSDKey key) .br .RS .LP Destroys a thread-specific data key previously created by \fB\fIerl_drv_tsd_key_create\fR\&\fR\&\&. All thread-specific data using this key in all threads must be cleared (see \fB\fIerl_drv_tsd_set\fR\&\fR\&) before the call to \fIerl_drv_tsd_key_destroy\fR\&\&. .LP \fIkey\fR\& is a thread-specific data key to destroy\&. .LP .RS -4 .B Warning: .RE A destroyed key is very likely to be reused soon\&. Therefore, if you fail to clear the thread-specific data using this key in a thread before destroying the key, you will \fIvery likely\fR\& get unexpected errors in other parts of the system\&. .LP This function is thread-safe\&. .RE .LP .B void erl_drv_tsd_set(ErlDrvTSDKey key, void *data) .br .RS .LP Sets thread-specific data associated with \fIkey\fR\& for the calling thread\&. You are only allowed to set thread-specific data for threads while they are fully under your control\&. For example, if you set thread-specific data in a thread calling a driver callback function, it must be cleared, that is, set to \fINULL\fR\&, before returning from the driver callback function\&. .LP \fIkey\fR\& is a thread-specific data key\&. .LP \fIdata\fR\& is a pointer to data to associate with \fIkey\fR\& in the calling thread\&. .LP .RS -4 .B Warning: .RE If you fail to clear thread-specific data in an emulator thread before letting it out of your control, you might never be able to clear this data with later unexpected errors in other parts of the system as a result\&. .LP This function is thread-safe\&. .RE .LP .B char *erl_errno_id(int error) .br .RS .LP Returns the atom name of the Erlang error, given the error number in \fIerror\fR\&\&. The error atoms are \fIeinval\fR\&, \fIenoent\fR\&, and so on\&. It can be used to make error terms from the driver\&. .RE .LP .B int remove_driver_entry(ErlDrvEntry *de) .br .RS .LP Removes a driver entry \fIde\fR\& previously added with \fB\fIadd_driver_entry\fR\&\fR\&\&. .LP Driver entries added by the \fIerl_ddll\fR\& Erlang interface cannot be removed by using this interface\&. .RE .LP .B void set_busy_port(ErlDrvPort port, int on) .br .RS .LP Sets and unsets the busy state of the port\&. If \fIon\fR\& is non-zero, the port is set to busy\&. If it is zero, the port is set to not busy\&. You typically want to combine this feature with the \fB busy port message queue\fR\& functionality\&. .LP Processes sending command data to the port are suspended if either the port or the port message queue is busy\&. Suspended processes are resumed when neither the port or the port message queue is busy\&. Command data is in this context data passed to the port using either \fIPort ! {Owner, {command, Data}}\fR\& or \fIport_command/[2,3]\fR\&\&. .LP If the \fB ERL_DRV_FLAG_SOFT_BUSY\fR\& has been set in the \fB\fIdriver_entry\fR\&\fR\&, data can be forced into the driver through \fB\fIerlang:port_command(Port, Data, [force])\fR\&\fR\& even if the driver has signaled that it is busy\&. .LP For information about busy port message queue functionality, see \fB\fIerl_drv_busy_msgq_limits\fR\&\fR\&\&. .RE .LP .B void set_port_control_flags(ErlDrvPort port, int flags) .br .RS .LP Sets flags for how the \fB\fIcontrol\fR\&\fR\& driver entry function will return data to the port owner process\&. (The \fIcontrol\fR\& function is called from \fB\fIerlang:port_control/3\fR\&\fR\&\&.) .LP Currently there are only two meaningful values for \fIflags\fR\&: \fI0\fR\& means that data is returned in a list, and \fIPORT_CONTROL_FLAG_BINARY\fR\& means data is returned as a binary from \fIcontrol\fR\&\&. .RE .SH "SEE ALSO" .LP \fB\fIdriver_entry(3erl)\fR\&\fR\&, \fB\fIerlang(3erl)\fR\&\fR\&, \fB\fIerl_ddll(3erl)\fR\&\fR\&, section \fBHow to Implement an Alternative Carrier for the Erlang Distribution>\fR\& in the User\&'s Guide