Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: base/files/file_descriptor_watcher_posix.h

Issue 2332923004: Add a new file descriptor watch API. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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_FILES_FILE_DESCRIPTOR_WATCHER_POSIX_H_
6 #define BASE_FILES_FILE_DESCRIPTOR_WATCHER_POSIX_H_
7
8 #include <memory>
9
10 #include "base/base_export.h"
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/sequence_checker.h"
17
18 namespace base {
19
20 class SingleThreadTaskRunner;
21
22 // The FileDescriptorWatcher API allows callbacks to be invoked when file
23 // descriptors are readable or writable without blocking.
24 class BASE_EXPORT FileDescriptorWatcher {
25 public:
26 // Instantiated and returned by WatchReadable() or WatchWritable(). The
27 // constructor registers a callback to be invoked when a file descriptor is
28 // readable or writable without blocking and the destructor unregisters it.
29 class Controller {
30 public:
31 // Unregisters the callback registered by the constructor.
32 ~Controller();
33
34 private:
35 friend class FileDescriptorWatcher;
36 class Watcher;
37
38 // Registers |callback| to be invoked when |fd| is readable or writable
39 // without blocking (depending on |mode|).
40 Controller(MessageLoopForIO::Mode mode, int fd, const Closure& callback);
41
42 // Starts watching the file descriptor. |called_from_constructor| indicates
43 // is true if this is called from the constructor.
44 void StartWatching(bool called_from_constructor);
45
46 // Runs |callback_|.
47 void RunCallback();
48
49 // The callback to run when the watched file descriptor is readable or
50 // writable without blocking.
51 std::unique_ptr<Closure> callback_;
52
53 // TaskRunner associated with the MessageLoopForIO that watches the file
54 // descriptor.
55 const scoped_refptr<SingleThreadTaskRunner>
56 message_loop_for_io_task_runner_;
57
58 // Notified by the MessageLoopForIO associated with
59 // |message_loop_for_io_task_runner_| when the watched file descriptor is
60 // readable or writable without blocking. Posts a task to run RunCallback()
61 // on the sequence on which the Controller was instantiated. When the
62 // Controller is deleted, ownership of |watcher_| is transfered to a delete
63 // task posted to the MessageLoopForIO. This ensures that |watcher_| isn't
64 // deleted while it is being used by the MessageLoopForIO.
65 std::unique_ptr<Watcher> watcher_;
66
67 // Validates that the Controller is used on the sequence on which it was
68 // instantiated.
69 SequenceChecker sequence_checker_;
70
71 // The value in this pointer is set to true by the destructor.
72 bool* was_deleted_ = nullptr;
73
74 WeakPtrFactory<Controller> weak_factory_;
75
76 DISALLOW_COPY_AND_ASSIGN(Controller);
77 };
78
79 // Registers |message_loop_for_io| to watch file descriptors for which
80 // callbacks are registered from the current thread via WatchReadable() or
81 // WatchWritable(). |message_loop_for_io| may run on another thread. The
82 // constructed FileDescriptorWatcher must not outlive |message_loop_for_io|.
83 FileDescriptorWatcher(MessageLoopForIO* message_loop_for_io);
84 ~FileDescriptorWatcher();
85
86 // Registers |callback| to be invoked on the current sequence when |fd| is
87 // readable or writable without blocking. |callback| is unregistered when the
88 // returned Controller is deleted (deletion must happen on the current
89 // sequence). To call these methods, a FileDescriptorWatcher must have been
90 // instantiated on the current thread and SequencedTaskRunnerHandle::IsSet()
91 // must return true.
92 static std::unique_ptr<Controller> WatchReadable(int fd,
93 const Closure& callback);
94 static std::unique_ptr<Controller> WatchWritable(int fd,
95 const Closure& callback);
96
97 private:
98 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher);
99 };
100
101 } // namespace base
102
103 #endif // BASE_FILES_FILE_DESCRIPTOR_WATCHER_POSIX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698