Home | History | Annotate | Download | only in event_injector
      1 /*
      2 * Copyright (C) 2011 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 * http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 #ifndef IOLOOPER_H
     17 #define IOLOOPER_H
     18 
     19 #include <stdint.h>
     20 
     21 #ifdef __cplusplus
     22 extern "C" {
     23 #endif
     24 
     25 /* An IOLooper is an abstraction for select() */
     26 
     27 typedef struct IoLooper  IoLooper;
     28 
     29 IoLooper*  iolooper_new(void);
     30 void       iolooper_free( IoLooper*  iol );
     31 void       iolooper_reset( IoLooper*  iol );
     32 
     33 void       iolooper_add_read( IoLooper*  iol, int  fd );
     34 void       iolooper_add_write( IoLooper*  iol, int  fd );
     35 void       iolooper_del_read( IoLooper*  iol, int  fd );
     36 void       iolooper_del_write( IoLooper*  iol, int  fd );
     37 
     38 enum {
     39     IOLOOPER_READ = (1<<0),
     40     IOLOOPER_WRITE = (1<<1),
     41 };
     42 void       iolooper_modify( IoLooper*  iol, int fd, int oldflags, int newflags);
     43 
     44 int        iolooper_poll( IoLooper*  iol );
     45 /* Wrapper around select()
     46  * Return:
     47  *  > 0 in case an I/O has occurred, or < 0 on error, or 0 on timeout with
     48  *  errno set to ETIMEDOUT.
     49  */
     50 int        iolooper_wait( IoLooper*  iol, int64_t  duration );
     51 
     52 int        iolooper_is_read( IoLooper*  iol, int  fd );
     53 int        iolooper_is_write( IoLooper*  iol, int  fd );
     54 /* Returns 1 if this IoLooper has one or more file descriptor to interact with */
     55 int        iolooper_has_operations( IoLooper*  iol );
     56 /* Gets current time in milliseconds.
     57  * Return:
     58  *  Number of milliseconds corresponded to the current time on success, or -1
     59  *  on failure.
     60  */
     61 int64_t    iolooper_now(void);
     62 /* Waits for an I/O to occur before specific absolute time.
     63  * This routine should be used (instead of iolooper_wait) in cases when multiple
     64  * sequential I/O should be completed within given time interval. For instance,
     65  * consider the scenario, when "server" does two sequential writes, and "client"
     66  * now has to read data transferred with these two distinct writes. It might be
     67  * wasteful to do two reads, each with the same (large) timeout. Instead, it
     68  * would be better to assign a deadline for both reads before the first read,
     69  * and call iolooper_wait_absoulte with the same deadline value:
     70  *  int64_t deadline = iolooper_now() + TIMEOUT;
     71  *  if (iolooper_wait_absoulte(iol, deadline)) {
     72  *      // Process first buffer.
     73  *      (iolooper_wait_absoulte(iol, deadline)) {
     74  *          // Process second read
     75  *      }
     76  *  }
     77  * Param:
     78  *  iol IoLooper instance for an I/O.
     79  *  deadline Deadline (absoulte time in milliseconds) before which an I/O should
     80  *      occur.
     81  * Return:
     82  *  Number of I/O descriptors set in iol, if an I/O has occurred, 0 if no I/O
     83  *  occurred before the deadline, or -1 on error.
     84  */
     85 int iolooper_wait_absolute(IoLooper* iol, int64_t deadline);
     86 
     87 #ifdef __cplusplus
     88 }
     89 #endif
     90 
     91 #endif /* IOLOOPER_H */
     92