VSM C++ SDK
Vehicle Specific Modules SDK
poll_io_controller.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
8 #ifndef _POLL_IO_CONTROLLER_H_
9 #define _POLL_IO_CONTROLLER_H_
10 
12 #include <poll.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 
16 namespace ugcs {
17 namespace vsm {
18 namespace internal {
19 
22 public:
23 
25  struct Io_cb {
27  // @{
28  enum class Operation {
29  READ,
30  WRITE
31  };
32  // @}
33 
35  typedef void (*Callback)(Io_cb &);
36 
38  int fd;
42  void *buf;
44  size_t size;
49 
51  int error;
53  ssize_t return_value;
54  };
55 
57 
58  virtual
60 
62  virtual void
63  Enable() override;
64 
66  virtual void
67  Disable() override;
68 
70  virtual void
72  {}
73 
75  virtual void
77  {}
78 
104  void
105  Delete_handle(int fd);
106 
111  bool
112  Queue_operation(Io_cb &io_cb);
113 
118  bool
119  Cancel_operation(Io_cb &io_cb);
120 
121 private:
126  class File_desc {
127  public:
128  Io_cb *read_cb = nullptr,
129  *write_cb = nullptr;
131  size_t poll_fd_idx = 0;
132  bool close_on_remove = false;
133  };
135  std::thread dispatcher_thread;
137  std::atomic_bool quit_req = { false };
139  std::map<int, File_desc> fd_map;
141  std::mutex map_mutex;
143  std::vector<pollfd> poll_fd_array;
145  int signal_fd;
146 
148  void
149  Dispatcher_thread();
150 
152  void
153  Read(Io_cb &io_cb);
154 
156  void
157  Write(Io_cb &io_cb);
158 
160  void
161  Seek(Io_cb &io_cb);
162 
163  size_t
164  Allocate_poll_fd_index();
165 
166  void
167  Release_poll_fd_index(size_t idx);
168 
169  void
170  Send_signal();
171 
172  void
173  Accept_signal();
174 
176  void
177  Update_poll_fd_array();
178 };
179 
180 } /* namespace internal */
181 } /* namespace vsm */
182 } /* namespace ugcs */
183 
184 #endif /* _POLL_IO_CONTROLLER_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Linux-specific implementation for I/O controller.
Definition: poll_io_controller.h:21
int64_t Offset
Offset for read/write operations.
Definition: io_stream.h:62
virtual void Enable() override
Enable the controller.
bool Queue_operation(Io_cb &io_cb)
Queue IO operation.
virtual void Unregister_handle(File_processor::Stream::Native_handle &) override
Unregister previously registered file handle.
Definition: poll_io_controller.h:76
virtual void Register_handle(File_processor::Stream::Native_handle &) override
Register new opened file handle.
Definition: poll_io_controller.h:71
Io_stream::Offset offset
File offset.
Definition: poll_io_controller.h:46
int fd
File descriptor.
Definition: poll_io_controller.h:38
ssize_t return_value
Operation return value, typically transfer size.
Definition: poll_io_controller.h:53
size_t size
Data size.
Definition: poll_io_controller.h:44
Callback cbk
Callback to call when operation is completed.
Definition: poll_io_controller.h:48
virtual void Disable() override
Disable the controller.
Interface for platform native file handle.
Definition: file_processor.h:108
Operation op
Requested operation.
Definition: poll_io_controller.h:40
Operation
Operation type.
Definition: poll_io_controller.h:28
Generic callback.
Definition: callback.h:213
bool Cancel_operation(Io_cb &io_cb)
Cancel pending operation.
void * buf
Data buffer.
Definition: poll_io_controller.h:42
void Delete_handle(int fd)
Delete handle.
int error
Operation error code.
Definition: poll_io_controller.h:51
Control block for I/O operation.
Definition: poll_io_controller.h:25
Interface for native I/O controller which manages I/O operations for all native handles.
Definition: file_processor.h:516
Processor for handling file I/O.