os/ossrv/genericopenlibs/openenvcore/include/sys/aeselect.dosc
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /** @file   ../include/sys/aeselect.h
     2 @internalComponent
     3 */
     4 
     5 /** @fn int aselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout, TRequestStatus* aRequestStatus)
     6 @param nfds - The nfds argument specifies the range of file descriptors to be tested. The select() function tests file descriptors in the range of 0 to nfds-1.
     7 @param readfds - If the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read.
     8 @param writefds - If the writefds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write.
     9 @param errorfds - If the errorfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending.
    10 @param timeout - If the timeout argument is not a null pointer, it points to an object of type struct timeval that specifies a maximum interval to wait for the selection to complete. If the timeout argument points to an object of type struct timeval whose members are 0, select() does not block. If the timeout argument is a null pointer, select() blocks until an event causes one of the masks to be returned with a valid (non-zero) value. If the time limit expires before any event occurs that would cause one of the masks to be set to a non-zero value, select() completes successfully and returns 0. 
    11 @param aRequestStatus A TRequestStatus object that is used for the asynchronous request
    12 @return 0 on Success and -1 on Failure. After completion, TRequestStatus is set to the of total number of bits set in the bit masks on success and -errno on failure
    13 		
    14 The aselect() function is an asynchronous version of the select API, which allows select-based code to be used with other event schedulers
    15 e.g. an ActiveScheduler). It invokes select in a new thread and when that returns, it completes the TRequestStatus argument with
    16 the result of select or –errno.
    17 
    18 If aselect is unable to set up an asynchronous request, it fails with -1 and sets errno appropriately.
    19 Otherwise, it returns 0. Any error that occurs later is reflected in the completion value of the TRequestStatus variable.
    20 
    21 On success, the TRequestStatus variable is set to the return value of select (number of descriptors with ready events on them).
    22 On error, this is set to the negative value of errno (-errno)
    23 
    24 
    25 Examples:
    26 @code
    27 	#include <sys/aeselect.h>
    28 	#include <e32cmn.h>
    29 	int main()
    30          	{
    31          	timeval time;
    32          	time.tv_sec = 5;
    33          	time.tv_usec = 0;
    34          	TRequestStatus status;
    35          	// Issue aselect request which complete after 5 seconds
    36          	aselect(0, NULL, NULL, NULL,&time ,&status);
    37          	// Catch the request completion event
    38          	User::WaitForRequest(status);
    39          	// The status is completed with negative value on failure
    40          	if( status.Int() < 0 )
    41          		{
    42          		// Failure
    43          		return KErrGeneral;
    44          		}
    45          	else
    46          		{
    47          		// Success
    48          		return KErrNone;
    49          		}
    50          	}
    51 @endcode
    52 
    53 @see select()
    54 
    55 @publishedAll
    56 @externallyDefinedApi
    57 */
    58 
    59 /** @fn int cancelaselect(TRequestStatus*)
    60 @param TRequestStatus indicating the aselect request to be cancelled
    61 @return 0 on Success and -1 on Failure. errno is set to indicate the error.
    62 
    63 cancelaselect() function is used to cancel a pending aselect request. The aselect request to be cancelled is indicated by passing
    64 the TRequestStatus on which aselect was issued. 	
    65 	
    66 Upon successful cancellation, the request is completed with KErrCancel, and cancelaselect function returns 0.
    67 Upon error it returns -1 and errno is set to the error code.
    68 
    69 
    70 Examples:
    71 @code
    72 	#include <sys/aeselect.h>
    73 	#include <e32cmn.h>
    74 	int main()
    75 	{
    76 	timeval time;
    77 	time.tv_sec = 5;
    78 	time.tv_usec = 0;
    79 	TRequestStatus status[10];
    80 	for( int i=0; i<10;i++ )
    81 		{
    82 		aselect(0, NULL, NULL, NULL,&time ,&status[i]);
    83 		}
    84 	for( int j=0; j<10;j++ )
    85 		{
    86 		int p = cancelaselect(&status[j]);
    87 		if( p != 0)
    88 			{
    89 			return KErrGeneral; // Error in cancelaselect
    90 			}
    91 		// Consume the cancellation event
    92 		User::WaitForRequest(status[j]);
    93 		// Ensure that the cancel has set the status to KErrCancel
    94 		if( status[j] != KErrCancel ) 
    95 			{
    96 			return KErrGeneral;
    97 			}
    98 		}
    99 	return KErrNone;
   100 	}
   101 @endcode
   102 
   103 @see select(),aselect()
   104 
   105 @publishedAll
   106 @externallyDefinedApi
   107 */
   108 
   109 /** @fn int eselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout, int numreqs, TRequestStatus* waitarray)
   110 @param nfds - The nfds argument specifies the range of file descriptors to be tested. The select() function tests file descriptors in the range of 0 to nfds-1.
   111 @param readfds - If the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read.
   112 @param writefds - If the writefds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write.
   113 @param errorfds - If the errorfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending.
   114 @param timeout - If the timeout argument is not a null pointer, it points to an object of type struct timeval that specifies a maximum interval to wait for the selection to complete. If the timeout argument points to an object of type struct timeval whose members are 0, select() does not block. If the timeout argument is a null pointer, select() blocks until an event causes one of the masks to be returned with a valid (non-zero) value. If the time limit expires before any event occurs that would cause one of the masks to be set to a non-zero value, select() completes successfully and returns 0. 
   115 @param numreqs The number of TRequestStatus elements in the waitarray
   116 @param waitarray An array of TRequestStatus elements to wait on
   117 @return Total number of bits set in the bit masks on success
   118 
   119 The eselect function is an extended variant of select. Alongside the fd_sets to watch for, eselect also accepts an array of 
   120 TRequestStatus variables – representing other pending events.
   121 It returns whenever there is an event on one of the fds or any of the TRequestStatus variables are complete.
   122 
   123 eselect thus takes 2 extra parameters:
   124 
   125 1.	An array of TRequestStatus objects.
   126 2.	The number of TRequestStatus objects in the array.
   127 
   128 The function is synchronous and returns, when an event is available on any of the fds or
   129 when any of the TRequestStatus objects in TRequestStatus array is ‘Complete’-d.
   130 
   131 Note that the return value of eselect is the same as select (the number of descriptors with ready events or 0, on success and -1 on failure).
   132 The number of TRequestStatus variables that are Complete do not show up in the result.
   133 
   134 It is the caller’s responsibility to check each of your input variables in a loop (after checking the fd_sets), to ascertain which ones are now Complete.
   135 It is the caller’s responsibility to cancel any outstanding requests and/or gather their completion status.
   136 
   137 IMPORTANT INFORMATION:
   138 Note on return value and callers responsibility:
   139 
   140 The return value of eselect is as follows
   141 
   142 	1. The total number of bits set in the bit masks on success.
   143 	2. 0 when there is a timeout due to timeval or when any of the TRequestStatus elements in the waitarray are signalled.
   144 	   In case of a timeout errno is set to ETIMEDOUT.
   145 	3. -1 in case of error and errno is set to reflect the error.
   146 	
   147 The caller will have to either wait for or cancel the following number of outstanding requests
   148 	
   149       1. numreqs number of requests if return value is 0, and errno is ETIMEDOUT
   150       2. (numreqs-1) number of requests if return value is 0 and errno is not ETIMEDOUT.
   151 
   152 
   153 Examples:
   154 @code
   155 	// Example to denote the usage of eselect
   156 	TRequestStatus status = KRequestPending;
   157 	fd_set readfds;
   158 	FD_ZERO(&readfds);
   159 	FD_SET(0, &readfds);
   160 	RTimer timer;
   161 	if (timer.CreateLocal() != KErrNone)
   162 		{
   163 		// die
   164 		return -1;
   165 		}
   166 	timer.After(status, 10000000);
   167 	int ret = eselect(1, &readfds, NULL, NULL, NULL, 1, status);
   168 	//Blocks
   169 	// Check if eselect is successful
   170 	if (ret >= 0)
   171 		{
   172 		if (FD_ISSET(0, &readfds))
   173 			{
   174 			// do something
   175 			}
   176 		if (status.Int() == KRequestPending)
   177 			{
   178 			// Cancel this request
   179 			timer.Cancel()
   180 			User::WaitForRequest(status);
   181 			}
   182 		}
   183 	else
   184 		{
   185 		// Failure in eselect
   186 		}
   187 @endcode
   188 
   189 @code
   190 	// Example to denote the return value and errno value upon a timeval timeout
   191     TInt timesecs;
   192     TInt timemicrosecs;  	
   193     timemicrosecs = 0;
   194     timesecs = 5;  	
   195     struct timeval tv;
   196     tv.tv_sec = timesecs;
   197     tv.tv_usec = timemicrosecs;    
   198     time_t time1,time2;
   199 	TRequestStatus waitarray[3];
   200 	for (TInt i = 0; i < 3; i++)
   201 		{
   202 		waitarray[i] = KRequestPending;
   203 		}
   204 	RTimer timer;
   205 	timer.CreateLocal();
   206 	timer.After(waitarray[2],TTimeIntervalMicroSeconds32(10000000));
   207     int err = time(&time1);
   208     // Upon time out from timeval eselect should return 0
   209 	if ( eselect(1, NULL, NULL, NULL, &tv,3,waitarray) == KErrNone )
   210 		{
   211 	    err = time(&time2);
   212 	    // errno should be set to ETIMEDOUT if eselect returns due to a timeval timeout
   213 	    if( errno == ETIMEDOUT )
   214 	    	{
   215 	    	if ( ((time2 - time1) >= timesecs) && (waitarray[2] == KRequestPending) )
   216 	    		{
   217 	    		timer.Cancel();
   218 	    		timer.Close();
   219 	    		return KErrNone;
   220 	    		}
   221 	    	else
   222 	    		{
   223 	    		timer.Cancel();
   224 	    		timer.Close();
   225 	    		return KErrGeneral;
   226 	    		}
   227 	    	}
   228 	    else
   229 	    	{
   230 	    	// errno not set to ETIMEDOUT, hence it is an error
   231 	    	return KErrGeneral;
   232 	    	}
   233 		}
   234 	else
   235 		{
   236 		timer.Cancel();
   237 		timer.Close();
   238 		return KErrGeneral;
   239 		}
   240 @endcode
   241 	
   242 @see select()
   243 
   244 @publishedAll
   245 @externallyDefinedApi
   246 */