| 6 | #define BASE_THREADING_SCOPED_BLOCKING_CALL_H |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/location.h" |
| 10 | #include "base/threading/scoped_blocking_call_internal.h" |
| 11 |
| 12 | namespace base { |
| 13 |
| 14 | // A "blocking call" refers to any call that causes the calling thread to wait |
| 15 | // off-CPU. It includes but is not limited to calls that wait on synchronous |
| 16 | // file I/O operations: read or write a file from disk, interact with a pipe or |
| 17 | // a socket, rename or delete a file, enumerate files in a directory, etc. |
| 18 | // Acquiring a low contention lock is not considered a blocking call. |
| 19 |
| 20 | // BlockingType indicates the likelihood that a blocking call will actually |
| 21 | // block. |
| 22 | enum class BlockingType { |
| 23 | // The call might block (e.g. file I/O that might hit in memory cache). |
| 24 | MAY_BLOCK, |
| 25 | // The call will definitely block (e.g. cache already checked and now pinging |
| 26 | // server synchronously). |
| 27 | WILL_BLOCK |
| 28 | }; |
| 29 |
| 30 | // This class must be instantiated in every scope where a blocking call is made |
| 31 | // and serves as a precise annotation of the scope that may/will block for the |
| 32 | // scheduler. When a ScopedBlockingCall is instantiated, it asserts that |
| 33 | // blocking calls are allowed in its scope with a call to |
| 34 | // base::AssertBlockingAllowed(). CPU usage should be minimal within that scope. |
| 35 | // //base APIs that block instantiate their own ScopedBlockingCall; it is not |
| 36 | // necessary to instantiate another ScopedBlockingCall in the scope where these |
| 37 | // APIs are used. Nested ScopedBlockingCalls are supported (mostly a no-op |
| 38 | // except for WILL_BLOCK nested within MAY_BLOCK which will result in immediate |
| 39 | // WILL_BLOCK semantics). |
| 40 | // |
| 41 | // Good: |
| 42 | // Data data; |
| 43 | // { |
| 44 | // ScopedBlockingCall scoped_blocking_call( |
| 45 | // FROM_HERE, BlockingType::WILL_BLOCK); |
| 46 | // data = GetDataFromNetwork(); |
| 47 | // } |
| 48 | // CPUIntensiveProcessing(data); |
| 49 | // |
| 50 | // Bad: |
| 51 | // ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 52 | // BlockingType::WILL_BLOCK); |
| 53 | // Data data = GetDataFromNetwork(); |
| 54 | // CPUIntensiveProcessing(data); // CPU usage within a ScopedBlockingCall. |
| 55 | // |
| 56 | // Good: |
| 57 | // Data a; |
| 58 | // Data b; |
| 59 | // { |
| 60 | // ScopedBlockingCall scoped_blocking_call( |
| 61 | // FROM_HERE, BlockingType::MAY_BLOCK); |
| 62 | // a = GetDataFromMemoryCacheOrNetwork(); |
| 63 | // b = GetDataFromMemoryCacheOrNetwork(); |
| 64 | // } |
| 65 | // CPUIntensiveProcessing(a); |
| 66 | // CPUIntensiveProcessing(b); |
| 67 | // |
| 68 | // Bad: |
| 69 | // ScopedBlockingCall scoped_blocking_call( |
| 70 | // FROM_HERE, BlockingType::MAY_BLOCK); |
| 71 | // Data a = GetDataFromMemoryCacheOrNetwork(); |
| 72 | // Data b = GetDataFromMemoryCacheOrNetwork(); |
| 73 | // CPUIntensiveProcessing(a); // CPU usage within a ScopedBlockingCall. |
| 74 | // CPUIntensiveProcessing(b); // CPU usage within a ScopedBlockingCall. |
| 75 | // |
| 76 | // Good: |
| 77 | // base::WaitableEvent waitable_event(...); |
| 78 | // waitable_event.Wait(); |
| 79 | // |
| 80 | // Bad: |
| 81 | // base::WaitableEvent waitable_event(...); |
| 82 | // ScopedBlockingCall scoped_blocking_call( |
| 83 | // FROM_HERE, BlockingType::WILL_BLOCK); |
| 84 | // waitable_event.Wait(); // Wait() instantiates its own ScopedBlockingCall. |
| 85 | // |
| 86 | // When a ScopedBlockingCall is instantiated from a ThreadPool parallel or |
| 87 | // sequenced task, the thread pool size is incremented to compensate for the |
| 88 | // blocked thread (more or less aggressively depending on BlockingType). |
| 89 | class BASE_EXPORT ScopedBlockingCall |
| 90 | : public internal::UncheckedScopedBlockingCall { |
| 91 | public: |
| 92 | ScopedBlockingCall(const Location& from_here, BlockingType blocking_type); |
| 93 | ~ScopedBlockingCall(); |
| 94 | }; |
| 95 |
| 96 | // Usage reserved for //base callers. |
| 97 | namespace internal { |
| 98 |
| 99 | // This class must be instantiated in every scope where a sync primitive is |
| 100 | // used. When a ScopedBlockingCallWithBaseSyncPrimitives is instantiated, it |
| 101 | // asserts that sync primitives are allowed in its scope with a call to |
| 102 | // internal::AssertBaseSyncPrimitivesAllowed(). The same guidelines as for |
| 103 | // ScopedBlockingCall should be followed. |
| 104 | class BASE_EXPORT ScopedBlockingCallWithBaseSyncPrimitives |
| 105 | : public UncheckedScopedBlockingCall { |
| 106 | public: |
| 107 | ScopedBlockingCallWithBaseSyncPrimitives(const Location& from_here, |
| 108 | BlockingType blocking_type); |
| 109 | ~ScopedBlockingCallWithBaseSyncPrimitives(); |
| 110 | }; |
| 111 |
| 112 | } // namespace internal |
| 113 |
| 114 | } // namespace base |
| 115 |
| 116 | #endif // BASE_THREADING_SCOPED_BLOCKING_CALL_H |
| 117 |