First public contribution.
1 /** @file ../include/sys/aeselect.h
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
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.
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.
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)
27 #include <sys/aeselect.h>
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 )
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.
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.
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.
72 #include <sys/aeselect.h>
79 TRequestStatus status[10];
80 for( int i=0; i<10;i++ )
82 aselect(0, NULL, NULL, NULL,&time ,&status[i]);
84 for( int j=0; j<10;j++ )
86 int p = cancelaselect(&status[j]);
89 return KErrGeneral; // Error in cancelaselect
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 )
103 @see select(),aselect()
106 @externallyDefinedApi
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
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.
123 eselect thus takes 2 extra parameters:
125 1. An array of TRequestStatus objects.
126 2. The number of TRequestStatus objects in the array.
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.
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.
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.
137 IMPORTANT INFORMATION:
138 Note on return value and callers responsibility:
140 The return value of eselect is as follows
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.
147 The caller will have to either wait for or cancel the following number of outstanding requests
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.
155 // Example to denote the usage of eselect
156 TRequestStatus status = KRequestPending;
161 if (timer.CreateLocal() != KErrNone)
166 timer.After(status, 10000000);
167 int ret = eselect(1, &readfds, NULL, NULL, NULL, 1, status);
169 // Check if eselect is successful
172 if (FD_ISSET(0, &readfds))
176 if (status.Int() == KRequestPending)
178 // Cancel this request
180 User::WaitForRequest(status);
185 // Failure in eselect
190 // Example to denote the return value and errno value upon a timeval timeout
196 tv.tv_sec = timesecs;
197 tv.tv_usec = timemicrosecs;
199 TRequestStatus waitarray[3];
200 for (TInt i = 0; i < 3; i++)
202 waitarray[i] = KRequestPending;
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 )
212 // errno should be set to ETIMEDOUT if eselect returns due to a timeval timeout
213 if( errno == ETIMEDOUT )
215 if ( ((time2 - time1) >= timesecs) && (waitarray[2] == KRequestPending) )
230 // errno not set to ETIMEDOUT, hence it is an error
245 @externallyDefinedApi