The Pedigree Project  0.1
Buffer.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_BUFFER_H
21 #define KERNEL_UTILITIES_BUFFER_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/process/ConditionVariable.h"
25 #include "pedigree/kernel/process/Mutex.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include "pedigree/kernel/utilities/List.h"
28 #include "pedigree/kernel/utilities/new"
29 
30 class Event;
31 class Semaphore;
32 class Thread;
33 
48 template <class T, bool allowShortOperation = false>
50 {
51  friend class UnixSocket; // to allow for calling notifyMonitors()
52 
53  public:
54  Buffer(size_t bufferSize);
55  ~Buffer();
56 
61  size_t write(const T *buffer, size_t count, bool block = true);
62 
67  size_t read(T *buffer, size_t count, bool block = true);
68 
73  void disableWrites();
74 
79  void disableReads();
80 
86  bool enableWrites();
87 
93  bool enableReads();
94 
98  size_t getDataSize();
99 
103  size_t getSize();
104 
109  bool canWrite(bool block);
110 
114  bool canRead(bool block);
115 
119  void wipe();
120 
126  void monitor(Thread *pThread, Event *pEvent);
127 
131  void monitor(Semaphore *pSemaphore);
132 
136  void cullMonitorTargets(Thread *pThread);
137 
141  void cullMonitorTargets(Semaphore *pSemaphore);
142 
146  void cullMonitorTargets(Event *pEvent);
147 
148  private:
149  WITHOUT_IMPLICIT_CONSTRUCTORS(Buffer);
150 
156  void notifyMonitors();
157 
161  void addSegment(const T *buffer, size_t count);
162 
166  static const size_t m_SegmentSize = 32768;
167 
172  struct Segment
173  {
174  Segment() : data(), reader(0), size(0)
175  {
176  }
177 
179  T data[m_SegmentSize];
180 
182  size_t reader;
183 
185  size_t size;
186  };
187 
192  {
193  MonitorTarget() : pThread(0), pEvent(0), pSemaphore(0)
194  {
195  }
196 
197  MonitorTarget(Thread *thread, Event *event)
198  : pThread(thread), pEvent(event), pSemaphore(0)
199  {
200  }
201 
202  MonitorTarget(Semaphore *sem) : pThread(0), pEvent(0), pSemaphore(sem)
203  {
204  }
205 
206  Thread *pThread;
207  Event *pEvent;
208  Semaphore *pSemaphore;
209  };
210 
211  size_t m_BufferSize;
212  size_t m_DataSize;
213 
214  Mutex m_Lock;
215 
216  ConditionVariable m_WriteCondition;
217  ConditionVariable m_ReadCondition;
218 
219  List<Segment *> m_Segments;
220  List<MonitorTarget *> m_MonitorTargets;
221 
222  bool m_bCanRead;
223  bool m_bCanWrite;
224 };
225 
226 // Specializations are in a .cc file.
227 extern template class Buffer<uint8_t, false>; // IWYU pragma: keep
228 extern template class Buffer<uint8_t, true>; // IWYU pragma: keep
229 extern template class Buffer<char, false>; // IWYU pragma: keep
230 extern template class Buffer<char, true>; // IWYU pragma: keep
231 extern template class Buffer<size_t, false>; // IWYU pragma: keep
232 extern template class Buffer<size_t, true>; // IWYU pragma: keep
233 
234 #endif // KERNEL_UTILITIES_BUFFER_H
void monitor(Thread *pThread, Event *pEvent)
Definition: File.cc:683
Definition: Mutex.h:58
void cullMonitorTargets(Thread *pThread)
Definition: File.cc:691
Definition: Buffer.h:49
Definition: List.h:64
virtual uint64_t read(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true) final
Definition: File.cc:116
Definition: Thread.h:54
Definition: Event.h:48
size_t size
Definition: Buffer.h:185
size_t reader
Definition: Buffer.h:182
virtual uint64_t write(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true) final
Definition: File.cc:183