class::sys::Aio

class sys::Aio
  : public api::SysWorkObject

The Asynchronous IO class is used for performing asynchronous operations on hardware devices. When calling synchronous IO, the read/write function is called and then returns when the operation is complete. With Asynchronous IO, the read/write function returns immediately and the Aio class can be used to see when the operation completes.

On Stratify OS, synchronous operations will always cause the running thread to yield the processor. This can be avoided by using the Aio class. Consider the following example:

#include <sapi/hal.hpp>
#include <sapi/sys.hpp>
#include <sapi/var.hpp>

int main(int argc, char * argv[]){
   String buf = "Hello World!\n";
   Uart uart(0); //we will read/write data to UART port 0
   Aio aio(buf.data(), buf.size()); //aio uses buf as it's data

   uart.init(); //init the UART with default settings

 //First a synchronous write
   uart.write(buf.c_str(), buf.size());  //this will return when the bytes are written
 //While bytes are writing, the processor will be used on another thread

 //Now the AIO version
   uart.write(aio);
 //This thread keeps the processor -- so we can do something else in parallel

   while( aio.done() == false ){
   Timer::wait_milliseconds(5); //wait for the operation to complete
   }

   return 0;
}

Methods

  • public inlineAio()
  • public inlineAio(volatile void * buf,int nbytes,int offset)
  • public inline void *buf() const
  • public inline interror()
  • public inline boolis_busy() const
  • public inline boolis_done() const
  • public inline intnbytes() const
  • public inline intoffset() const
  • public inline intret()
  • public inline voidset_buf(volatile void * buf,int nbytes)
  • public inline voidset_buf(var::Data& data)
  • public inline voidset_offset(int offset)
  • public inline voidset_signal(int number,int value)
  • public inline intsuspend(int timeout_usec)

Details

public inlineAio()

Constructs an empy AIO object.


public inlineAio(volatile void * buf,int nbytes,int offset)

Constructs a new Aio object.

Parameters

  • buf The buffer for data transactions

  • nbytes The number of bytes to transfer on transactions

  • offset The file/device offset location


public inline void *buf() const

Returns the buffer pointer.


public inline interror()

Returns the error number of the operation.


public inline boolis_busy() const

Returns true if operation is still in progress.


public inline boolis_done() const

Returns true if operation is complete.


public inline intnbytes() const

Returns the number of bytes to transfer.


public inline intoffset() const

Returns the offset (or channel for Dac, Adc, Pwm, etc).


public inline intret()

Returns the return value of the operation.


public inline voidset_buf(volatile void * buf,int nbytes)

Sets the buffer pointer.

Parameters

  • buf A pointer to the data buffer

  • nbytes The number of bytes to transfer


public inline voidset_buf(var::Data& data)

Sets the buffer using a var::Data object.

Parameters

  • data The data object

The data object must not change the buffer allocation after calling this method. If it does, it will need to set the buffer again.


public inline voidset_offset(int offset)

Sets the offset (or channcel for Dac, Adc, Pwm, etc).


public inline voidset_signal(int number,int value)

Causes the calling thread to receive a signal when the operation completes.

Parameters

  • number The signal number (ie SIGTERM) use -1 to prevent a signal from being sent

  • value The signal value (passed as an argument to the handler if using siginfo)

Returns

Zero on success


public inline intsuspend(int timeout_usec)

Blocks until the operation completes or timeout is reached.

Parameters

  • timeout_usec The timeout in microseconds (0 to block indefinitely)

X

Thanks for Coming!

Subscribe to news and updates