OLD | NEW |
(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 UI_GFX_SHARED_EVENT_H_ |
| 6 #define UI_GFX_SHARED_EVENT_H_ |
| 7 |
| 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/process/process_handle.h" |
| 10 #include "base/time/time.h" |
| 11 #include "ui/gfx/gfx_export.h" |
| 12 |
| 13 namespace gfx { |
| 14 |
| 15 typedef base::SharedMemoryHandle SharedEventHandle; |
| 16 |
| 17 // SharedEvent is a useful synchronization tool when you want to allow one |
| 18 // thread in one process to wait for a thread in another process to finish |
| 19 // some work. All functions except Wait are guaranteed to never block. |
| 20 class GFX_EXPORT SharedEvent { |
| 21 public: |
| 22 SharedEvent(); |
| 23 |
| 24 // Create a new SharedEvent object from an existing, platform-specific |
| 25 // OS primitive. |
| 26 explicit SharedEvent(const SharedEventHandle& handle); |
| 27 |
| 28 // Creates a new event which can be used in a remote process. Returns a |
| 29 // platform-specific OS primitive for the new event. The caller is |
| 30 // responsible for destroying the OS primitive. |
| 31 static SharedEventHandle CreateForProcess(base::ProcessHandle process); |
| 32 |
| 33 // Duplicates the underlying OS primitive. The caller is responsible for |
| 34 // destroying the duplicated OS primitive. |
| 35 static SharedEventHandle DuplicateHandle(const SharedEventHandle& handle); |
| 36 |
| 37 // Put the event in the un-signaled state. |
| 38 void Reset(); |
| 39 |
| 40 // Put the event in the signaled state. Causing any thread blocked on Wait |
| 41 // to be woken up. |
| 42 void Signal(); |
| 43 |
| 44 // Returns true if the event is in the signaled state, else false. |
| 45 bool IsSignaled(); |
| 46 |
| 47 // Wait up until |max_time| has passed for the event to be signaled. Returns |
| 48 // true if the event was signaled. If this method returns false, then it |
| 49 // does not necessarily mean that |max_time| was exceeded. |
| 50 // |
| 51 // WARNING: This may block and should NOT be used to wait on an event shared |
| 52 // with an untrusted process to signal. |
| 53 bool Wait(const base::TimeDelta& max_time); |
| 54 |
| 55 // Returns the underlying OS primitive for this event. |
| 56 SharedEventHandle GetHandle() const; |
| 57 |
| 58 // Closes the open shared memory backing. The memory will remain mapped. |
| 59 void Close(); |
| 60 |
| 61 private: |
| 62 inline int32_t* uaddr() const { |
| 63 return reinterpret_cast<int32_t*>(shared_memory_.memory()); |
| 64 } |
| 65 base::SharedMemory shared_memory_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(SharedEvent); |
| 68 }; |
| 69 |
| 70 } // namespace gfx |
| 71 |
| 72 #endif // UI_GFX_SHARED_EVENT_H_ |
OLD | NEW |