OLD | NEW |
---|---|
(Empty) | |
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 MEDIA_AUDIO_SHARED_MEM_SYNCHRONIZER_H_ | |
6 #define MEDIA_AUDIO_SHARED_MEM_SYNCHRONIZER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/process.h" | |
10 #include "media/base/media_export.h" | |
11 | |
12 #if defined(OS_WIN) | |
13 #include "base/win/scoped_handle.h" | |
14 #else | |
15 #include "base/file_descriptor_posix.h" | |
16 #include "base/sync_socket.h" | |
17 #endif | |
18 | |
19 // A mechanism to synchronize access to a shared memory segment between | |
20 // two parties when the usage pattern resembles that of two players playing | |
21 // a game of chess. Each end has an instance of SharedMemSynchronizer and | |
22 // calls Signal() when it has finished reading from/writing to the shared | |
23 // memory section. Before accessing the memory, it must call Wait() in order | |
24 // to know when the other end has called Signal(). | |
scherkus (not reviewing)
2012/03/08 02:37:08
I know I can look at unit tests, but I think we co
tommi (sloooow) - chröme
2012/03/08 16:10:57
Done.
| |
25 class MEDIA_EXPORT SharedMemSynchronizer { | |
26 public: | |
27 #if defined(OS_WIN) | |
28 typedef HANDLE IPCHandle; | |
29 #else | |
30 typedef base::FileDescriptor IPCHandle; | |
31 #endif | |
32 | |
33 SharedMemSynchronizer(); | |
scherkus (not reviewing)
2012/03/08 02:37:08
docs on ctors considering they can be different
tommi (sloooow) - chröme
2012/03/08 16:10:57
Done.
| |
34 SharedMemSynchronizer(IPCHandle handle_1, IPCHandle handle_2); | |
35 ~SharedMemSynchronizer(); | |
36 | |
37 void Signal(); | |
scherkus (not reviewing)
2012/03/08 02:37:08
docs
tommi (sloooow) - chröme
2012/03/08 16:10:57
Done.
| |
38 void Wait(); | |
39 | |
40 bool IsValid() const; | |
41 | |
42 // Copies the internal handles to the output parameters, |handle_1| and | |
43 // |handle_2|. The operation can fail, so the caller must be prepared to | |
44 // handle that case. | |
45 bool ShareToProcess(base::ProcessHandle process, IPCHandle* handle_1, | |
46 IPCHandle* handle_2); | |
47 | |
48 // Initialized a pair of SharedMemSynchronizer instances. Note that this can | |
scherkus (not reviewing)
2012/03/08 02:37:08
typo?
Initialized -> Initialize ?
tommi (sloooow) - chröme
2012/03/08 16:10:57
Done.
| |
49 // fail (e.g. due to EMFILE on Linux), so the caller must handle that case. | |
50 static bool Create(SharedMemSynchronizer* a, SharedMemSynchronizer* b); | |
scherkus (not reviewing)
2012/03/08 02:37:08
would it be better to rename this Initialize()?
tommi (sloooow) - chröme
2012/03/08 16:10:57
I kinda borrowed this name from SyncSocket except
| |
51 | |
52 // Use an instance of this class when you have to repeatedly wait | |
53 // for multiple instances of SharedMemSynchronizer on the same thread. | |
54 // The class will store information about which synchronizer was last signaled | |
55 // and try to distribute the signals so that all synchronizers get a chance to | |
56 // be processed in times of high load and a busy one won't starve the | |
57 // others. | |
58 // TODO(tommi): Support a way to abort the wait. | |
scherkus (not reviewing)
2012/03/08 02:37:08
I guess starvation would still occur if a client i
tommi (sloooow) - chröme
2012/03/08 16:10:57
Yes. However, adding/removing to/from the array s
| |
59 class MEDIA_EXPORT WaitForMultiple { | |
60 public: | |
61 WaitForMultiple(SharedMemSynchronizer* synchronizers, size_t count); | |
62 | |
63 // Waits for any of the synchronizers to be signaled. Returns the 0 based | |
64 // index of a signaled synchronizer. | |
65 int Wait(); | |
66 | |
67 private: | |
68 SharedMemSynchronizer* synchronizers_; | |
69 size_t count_; | |
70 size_t last_; | |
71 }; | |
72 | |
73 protected: | |
scherkus (not reviewing)
2012/03/08 02:37:08
this class doesn't appear to have subclasses -- pr
tommi (sloooow) - chröme
2012/03/08 16:10:57
Done.
| |
74 // Only called by the WaitForMultiple class. See documentation | |
75 // for WaitForMultiple and comments inside WaitMultiple for details. | |
76 static int WaitMultiple(SharedMemSynchronizer* synchronizers, size_t count, | |
77 size_t last_signaled); | |
78 | |
79 #if defined(OS_WIN) | |
80 base::win::ScopedHandle mine_; | |
81 base::win::ScopedHandle other_; | |
82 #else | |
83 typedef base::CancelableSyncSocket SocketClass; | |
84 SocketClass socket_; | |
85 #endif | |
86 | |
87 private: | |
88 DISALLOW_COPY_AND_ASSIGN(SharedMemSynchronizer); | |
89 }; | |
90 | |
91 #endif // MEDIA_AUDIO_SHARED_MEM_SYNCHRONIZER_H_ | |
OLD | NEW |