Home | History | Annotate | Download | only in message_loop
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
      6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/macros.h"
     10 #include "base/message_loop/message_pump.h"
     11 #include "base/threading/thread_checker.h"
     12 #include "base/time/time.h"
     13 
     14 // Declare structs we need from libevent.h rather than including it
     15 struct event_base;
     16 struct event;
     17 
     18 namespace base {
     19 
     20 // Class to monitor sockets and issue callbacks when sockets are ready for I/O
     21 // TODO(dkegel): add support for background file IO somehow
     22 class BASE_EXPORT MessagePumpLibevent : public MessagePump {
     23  public:
     24   // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
     25   // of a file descriptor.
     26   class Watcher {
     27    public:
     28     // Called from MessageLoop::Run when an FD can be read from/written to
     29     // without blocking
     30     virtual void OnFileCanReadWithoutBlocking(int fd) = 0;
     31     virtual void OnFileCanWriteWithoutBlocking(int fd) = 0;
     32 
     33    protected:
     34     virtual ~Watcher() {}
     35   };
     36 
     37   // Object returned by WatchFileDescriptor to manage further watching.
     38   class FileDescriptorWatcher {
     39    public:
     40     FileDescriptorWatcher();
     41     ~FileDescriptorWatcher();  // Implicitly calls StopWatchingFileDescriptor.
     42 
     43     // NOTE: These methods aren't called StartWatching()/StopWatching() to
     44     // avoid confusion with the win32 ObjectWatcher class.
     45 
     46     // Stop watching the FD, always safe to call.  No-op if there's nothing
     47     // to do.
     48     bool StopWatchingFileDescriptor();
     49 
     50    private:
     51     friend class MessagePumpLibevent;
     52     friend class MessagePumpLibeventTest;
     53 
     54     // Called by MessagePumpLibevent, ownership of |e| is transferred to this
     55     // object.
     56     void Init(event* e);
     57 
     58     // Used by MessagePumpLibevent to take ownership of event_.
     59     event* ReleaseEvent();
     60 
     61     void set_pump(MessagePumpLibevent* pump) { pump_ = pump; }
     62     MessagePumpLibevent* pump() const { return pump_; }
     63 
     64     void set_watcher(Watcher* watcher) { watcher_ = watcher; }
     65 
     66     void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump);
     67     void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump);
     68 
     69     event* event_;
     70     MessagePumpLibevent* pump_;
     71     Watcher* watcher_;
     72     // If this pointer is non-NULL, the pointee is set to true in the
     73     // destructor.
     74     bool* was_destroyed_;
     75 
     76     DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher);
     77   };
     78 
     79   enum Mode {
     80     WATCH_READ = 1 << 0,
     81     WATCH_WRITE = 1 << 1,
     82     WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE
     83   };
     84 
     85   MessagePumpLibevent();
     86   ~MessagePumpLibevent() override;
     87 
     88   // Have the current thread's message loop watch for a a situation in which
     89   // reading/writing to the FD can be performed without blocking.
     90   // Callers must provide a preallocated FileDescriptorWatcher object which
     91   // can later be used to manage the lifetime of this event.
     92   // If a FileDescriptorWatcher is passed in which is already attached to
     93   // an event, then the effect is cumulative i.e. after the call |controller|
     94   // will watch both the previous event and the new one.
     95   // If an error occurs while calling this method in a cumulative fashion, the
     96   // event previously attached to |controller| is aborted.
     97   // Returns true on success.
     98   // Must be called on the same thread the message_pump is running on.
     99   // TODO(dkegel): switch to edge-triggered readiness notification
    100   bool WatchFileDescriptor(int fd,
    101                            bool persistent,
    102                            int mode,
    103                            FileDescriptorWatcher *controller,
    104                            Watcher *delegate);
    105 
    106   // MessagePump methods:
    107   void Run(Delegate* delegate) override;
    108   void Quit() override;
    109   void ScheduleWork() override;
    110   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
    111 
    112  private:
    113   friend class MessagePumpLibeventTest;
    114 
    115   void WillProcessIOEvent();
    116   void DidProcessIOEvent();
    117 
    118   // Risky part of constructor.  Returns true on success.
    119   bool Init();
    120 
    121   // Called by libevent to tell us a registered FD can be read/written to.
    122   static void OnLibeventNotification(int fd, short flags,
    123                                      void* context);
    124 
    125   // Unix pipe used to implement ScheduleWork()
    126   // ... callback; called by libevent inside Run() when pipe is ready to read
    127   static void OnWakeup(int socket, short flags, void* context);
    128 
    129   // This flag is set to false when Run should return.
    130   bool keep_running_;
    131 
    132   // This flag is set when inside Run.
    133   bool in_run_;
    134 
    135   // This flag is set if libevent has processed I/O events.
    136   bool processed_io_events_;
    137 
    138   // The time at which we should call DoDelayedWork.
    139   TimeTicks delayed_work_time_;
    140 
    141   // Libevent dispatcher.  Watches all sockets registered with it, and sends
    142   // readiness callbacks when a socket is ready for I/O.
    143   event_base* event_base_;
    144 
    145   // ... write end; ScheduleWork() writes a single byte to it
    146   int wakeup_pipe_in_;
    147   // ... read end; OnWakeup reads it and then breaks Run() out of its sleep
    148   int wakeup_pipe_out_;
    149   // ... libevent wrapper for read end
    150   event* wakeup_event_;
    151 
    152   ThreadChecker watch_file_descriptor_caller_checker_;
    153   DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent);
    154 };
    155 
    156 }  // namespace base
    157 
    158 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
    159