async_select  2.0.2
async_select
Enumerations | Functions
async_select.h File Reference

Asynchronous network select API. More...

#include <stdint.h>
#include <sni.h>

Go to the source code of this file.

Enumerations

enum  SELECT_Operation { SELECT_READ, SELECT_WRITE }
 Select operations list.
 

Functions

int32_t non_blocking_select (int32_t fd, SELECT_Operation operation)
 Execute a select() for the given file descriptor and operation without blocking. More...
 
int32_t async_select (int32_t fd, SELECT_Operation operation, int64_t timeout_ms, SNI_callback callback)
 Executes asynchronously a select() operation for the given file descriptor. This function will suspend the execution of the current Java thread using SNI_suspendCurrentJavaThreadWithCallback(). Once the select() succeeds the Java thread is resumed and the given SNI callback is called. More...
 
int32_t async_select_init (void)
 Initialize the async_select component. This function must be called prior to any call of async_select(). More...
 
void async_select_notify_closed_fd (int32_t fd)
 Notifies the async_select task that a file descriptor has been closed. On some systems the close of a file descriptor does not unblock the select that's why we need to notify the async_select task.
 

Detailed Description

Asynchronous network select API.

Author
MicroEJ Developer Team
Version
2.0.2
Date
13 November 2020

Definition in file async_select.h.

Function Documentation

§ async_select()

int32_t async_select ( int32_t  fd,
SELECT_Operation  operation,
int64_t  timeout_ms,
SNI_callback  callback 
)

Executes asynchronously a select() operation for the given file descriptor. This function will suspend the execution of the current Java thread using SNI_suspendCurrentJavaThreadWithCallback(). Once the select() succeeds the Java thread is resumed and the given SNI callback is called.

Parameters
[in]fdthe file descriptor.
[in]operationthe operation (read or write) we want to monitor with the select().
[in]timeout_mstimeout in millisecond
[in]theSNI callback to call when the Java thread is resumed or timeout occurs.
Returns
0 on success, -1 on failure.

Executes asynchronously a select() operation for the given file descriptor. This function will suspend the execution of the current Java thread using SNI_suspendCurrentJavaThreadWithCallback(). Once the select() succeeds the Java thread is resumed and the given SNI callback is called.

This function will suspend the execution of the current Java thread using SNI_suspendCurrentJavaThreadWithCallback(). Once the select() succeeds the Java thread is resumed and the given SNI callback is called. *

Parameters
fdthe file descriptor.
operationthe operation (read or write) we want to monitor with the select().
timeout_mstimeout in millisecond
theSNI callback to call when the Java thread is resumed or timeout occurs.
Returns
0 on success, -1 on failure.

Definition at line 174 of file async_select.c.

174  {
175 
176  int32_t res;
177 
178  async_select_Request* request = async_select_allocate_request();
179  if(request == NULL){
180  // No request available :-(
181  return -1;
182  }
183 
184  int32_t java_thread_id = SNI_getCurrentJavaThreadID();
185  if(java_thread_id == SNI_ERROR){
186  // Not called from the VM task
187  async_select_free_unused_request(request);
188  return -1;
189  }
190 
191  LLNET_DEBUG_TRACE("async_select: async_select on fd=0x%X operation=%s thread 0x%X\n", fd, operation==SELECT_READ ? "read":"write", java_thread_id);
192  request->java_thread_id = java_thread_id;
193  request->fd = fd;
194  request->operation = operation;
195  if(timeout_ms != 0){
196  request->absolute_timeout_ms = async_select_get_current_time_ms() + timeout_ms;
197  }
198  else { // infinite timeout
199  request->absolute_timeout_ms = 0;
200  }
201 
202  SNI_suspendCurrentJavaThreadWithCallback(0, callback, NULL);
203  res = async_select_send_new_request(request);
204 
205  return res;
206 }
#define async_select_get_current_time_ms()
Definition: async_select.c:72
An asynchronous select request.
Definition: async_select.c:45

§ async_select_init()

int32_t async_select_init ( void  )

Initialize the async_select component. This function must be called prior to any call of async_select().

Returns
0 on success, -1 on failure.

Definition at line 64 of file async_select_osal.c.

64  {
65  int32_t res;
66  res = async_select_start_task();
67  if(res == 0){
69  }
70  return res;
71 }
void async_select_request_fifo_init(void)
Initializes the requests FIFOs. This function must be called prior to any call of async_select()...
Definition: async_select.c:213

§ non_blocking_select()

int32_t non_blocking_select ( int32_t  fd,
SELECT_Operation  operation 
)

Execute a select() for the given file descriptor and operation without blocking.

Parameters
[in]fdthe file descriptor.
[in]operationthe operation (read or write) we want to monitor with the select().
Returns
select() result.
Parameters
fdthe file descriptor.
operationthe operation (read or write) we want to monitor with the select().
Returns
select() result.

Definition at line 136 of file async_select.c.

136  {
137  struct timeval zero_timeout;
138  zero_timeout.tv_sec = 0;
139  zero_timeout.tv_usec = 0;
140  fd_set io_fds;
141  fd_set* read_fds_ptr;
142  fd_set* write_fds_ptr;
143  FD_ZERO(&io_fds);
144  FD_SET(fd, &io_fds);
145 
146  if(operation == SELECT_READ){
147  read_fds_ptr = &io_fds;
148  write_fds_ptr = NULL;
149  }
150  else { // SELECT_WRITE
151  read_fds_ptr = NULL;
152  write_fds_ptr = &io_fds;
153  }
154 
155  int32_t selectRes = select(fd+1, read_fds_ptr, write_fds_ptr, NULL, &zero_timeout);
156 
157  return selectRes;
158 }