diff options
Diffstat (limited to 'src/system_wrappers/interface')
-rw-r--r-- | src/system_wrappers/interface/aligned_malloc.h | 25 | ||||
-rw-r--r-- | src/system_wrappers/interface/atomic32_wrapper.h | 55 | ||||
-rw-r--r-- | src/system_wrappers/interface/condition_variable_wrapper.h | 41 | ||||
-rw-r--r-- | src/system_wrappers/interface/constructor_magic.h | 50 | ||||
-rw-r--r-- | src/system_wrappers/interface/cpu_features_wrapper.h | 34 | ||||
-rw-r--r-- | src/system_wrappers/interface/cpu_wrapper.h | 51 | ||||
-rw-r--r-- | src/system_wrappers/interface/critical_section_wrapper.h | 66 | ||||
-rw-r--r-- | src/system_wrappers/interface/event_wrapper.h | 68 | ||||
-rw-r--r-- | src/system_wrappers/interface/file_wrapper.h | 72 | ||||
-rw-r--r-- | src/system_wrappers/interface/list_wrapper.h | 109 | ||||
-rw-r--r-- | src/system_wrappers/interface/map_wrapper.h | 77 | ||||
-rw-r--r-- | src/system_wrappers/interface/rw_lock_wrapper.h | 76 | ||||
-rw-r--r-- | src/system_wrappers/interface/sort.h | 64 | ||||
-rw-r--r-- | src/system_wrappers/interface/thread_wrapper.h | 86 | ||||
-rw-r--r-- | src/system_wrappers/interface/tick_util.h | 304 | ||||
-rw-r--r-- | src/system_wrappers/interface/trace.h | 83 |
16 files changed, 1261 insertions, 0 deletions
diff --git a/src/system_wrappers/interface/aligned_malloc.h b/src/system_wrappers/interface/aligned_malloc.h new file mode 100644 index 0000000000..c229435e64 --- /dev/null +++ b/src/system_wrappers/interface/aligned_malloc.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ + +#include <stddef.h> + +namespace webrtc +{ + void* AlignedMalloc( + size_t size, + size_t alignment); + void AlignedFree( + void* memBlock); +} + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ALIGNED_MALLOC_H_ diff --git a/src/system_wrappers/interface/atomic32_wrapper.h b/src/system_wrappers/interface/atomic32_wrapper.h new file mode 100644 index 0000000000..40862fb492 --- /dev/null +++ b/src/system_wrappers/interface/atomic32_wrapper.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// Atomic system independant 32-bit integer. +// Note: uses full memory barriers. +// Note: assumes 32-bit (or higher) system +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ + +#include "common_types.h" + +namespace webrtc { +class Atomic32Impl; +class Atomic32Wrapper +{ +public: + Atomic32Wrapper(WebRtc_Word32 initialValue = 0); + ~Atomic32Wrapper(); + + // Prefix operator! + WebRtc_Word32 operator++(); + WebRtc_Word32 operator--(); + + Atomic32Wrapper& operator=(const Atomic32Wrapper& rhs); + Atomic32Wrapper& operator=(WebRtc_Word32 rhs); + + WebRtc_Word32 operator+=(WebRtc_Word32 rhs); + WebRtc_Word32 operator-=(WebRtc_Word32 rhs); + + // Sets the value atomically to newValue if the value equals compare value. + // The function returns true if the exchange happened. + bool CompareExchange(WebRtc_Word32 newValue, WebRtc_Word32 compareValue); + WebRtc_Word32 Value() const; +private: + // Disable the + and - operator since it's unclear what these operations + // should do. + Atomic32Wrapper operator+(const Atomic32Wrapper& rhs); + Atomic32Wrapper operator-(const Atomic32Wrapper& rhs); + + WebRtc_Word32& operator++(int); + WebRtc_Word32& operator--(int); + + // Cheshire cat to hide the implementation (faster than + // using virtual functions) + Atomic32Impl& _impl; +}; +} // namespace webrtc +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_ATOMIC32_WRAPPER_H_ diff --git a/src/system_wrappers/interface/condition_variable_wrapper.h b/src/system_wrappers/interface/condition_variable_wrapper.h new file mode 100644 index 0000000000..c040fbf29f --- /dev/null +++ b/src/system_wrappers/interface/condition_variable_wrapper.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONDITION_VARIABLE_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONDITION_VARIABLE_WRAPPER_H_ + +namespace webrtc { +class CriticalSectionWrapper; + +class ConditionVariableWrapper +{ +public: + // Factory method, constructor disabled. + static ConditionVariableWrapper* CreateConditionVariable(); + + virtual ~ConditionVariableWrapper() {} + + // Calling thread will atomically release critSect and wait until next + // some other thread calls Wake() or WakeAll(). + virtual void SleepCS(CriticalSectionWrapper& critSect) = 0; + + // Same as above but with a timeout. + virtual bool SleepCS(CriticalSectionWrapper& critSect, + unsigned long maxTimeInMS) = 0; + + // Wakes one thread calling SleepCS(). + virtual void Wake() = 0; + + // Wakes all threads calling SleepCS(). + virtual void WakeAll() = 0; +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONDITION_VARIABLE_WRAPPER_H_ diff --git a/src/system_wrappers/interface/constructor_magic.h b/src/system_wrappers/interface/constructor_magic.h new file mode 100644 index 0000000000..b2aabc574f --- /dev/null +++ b/src/system_wrappers/interface/constructor_magic.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +/* + * WebRtc + * Copy from third_party/libjingle/source/talk/base/constructormagic.h + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONSTRUCTOR_MAGIC_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONSTRUCTOR_MAGIC_H_ + +#ifndef DISALLOW_ASSIGN +#define DISALLOW_ASSIGN(TypeName) \ + void operator=(const TypeName&) +#endif + +#ifndef DISALLOW_COPY_AND_ASSIGN +// A macro to disallow the evil copy constructor and operator= functions +// This should be used in the private: declarations for a class +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + DISALLOW_ASSIGN(TypeName) +#endif + +#ifndef DISALLOW_EVIL_CONSTRUCTORS +// Alternative, less-accurate legacy name. +#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ + DISALLOW_COPY_AND_ASSIGN(TypeName) +#endif + +#ifndef DISALLOW_IMPLICIT_CONSTRUCTORS +// A macro to disallow all the implicit constructors, namely the +// default constructor, copy constructor and operator= functions. +// +// This should be used in the private: declarations for a class +// that wants to prevent anyone from instantiating it. This is +// especially useful for classes containing only static methods. +#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName(); \ + DISALLOW_EVIL_CONSTRUCTORS(TypeName) +#endif + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CONSTRUCTOR_MAGIC_H_ diff --git a/src/system_wrappers/interface/cpu_features_wrapper.h b/src/system_wrappers/interface/cpu_features_wrapper.h new file mode 100644 index 0000000000..5d8a828a7a --- /dev/null +++ b/src/system_wrappers/interface/cpu_features_wrapper.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_FEATURES_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_FEATURES_WRAPPER_H_ + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +// list of features. +typedef enum { + kSSE2, + kSSE3 +} CPUFeature; + +typedef int (*WebRtc_CPUInfo)(CPUFeature feature); +// returns true if the CPU supports the feature. +extern WebRtc_CPUInfo WebRtc_GetCPUInfo; +// No CPU feature is available => straight C path. +extern WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM; + +#if defined(__cplusplus) || defined(c_plusplus) +} // extern "C" +#endif + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_FEATURES_WRAPPER_H_ diff --git a/src/system_wrappers/interface/cpu_wrapper.h b/src/system_wrappers/interface/cpu_wrapper.h new file mode 100644 index 0000000000..b72c20cd57 --- /dev/null +++ b/src/system_wrappers/interface/cpu_wrapper.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_WRAPPER_H_ + +#include "typedefs.h" + +namespace webrtc { +class CpuWrapper +{ +public: + static WebRtc_UWord32 DetectNumberOfCores(); + + static CpuWrapper* CreateCpu(); + virtual ~CpuWrapper() {} + + // Returns the average CPU usage for all processors. The CPU usage can be + // between and including 0 to 100 (%) + virtual WebRtc_Word32 CpuUsage() = 0; + virtual WebRtc_Word32 CpuUsage(WebRtc_Word8* processName, + WebRtc_UWord32 length) = 0; + virtual WebRtc_Word32 CpuUsage(WebRtc_UWord32 dwProcessID) = 0; + + // The CPU usage per core is returned in cpu_usage. The CPU can be between + // and including 0 to 100 (%) + // Note that the pointer passed as cpu_usage is redirected to a local member + // of the CPU wrapper. + // numCores is the number of cores in the cpu_usage array. + virtual WebRtc_Word32 CpuUsageMultiCore(WebRtc_UWord32& numCores, + WebRtc_UWord32*& cpu_usage) = 0; + + virtual void Reset() = 0; + virtual void Stop() = 0; + +protected: + CpuWrapper() {} + +private: + static WebRtc_UWord32 _numberOfCores; + +}; +} // namespace webrtc +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CPU_WRAPPER_H_ diff --git a/src/system_wrappers/interface/critical_section_wrapper.h b/src/system_wrappers/interface/critical_section_wrapper.h new file mode 100644 index 0000000000..ad31497e76 --- /dev/null +++ b/src/system_wrappers/interface/critical_section_wrapper.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_ + +// If the critical section is heavily contended it may be beneficial to use +// read/write locks instead. + +#include "common_types.h" + +namespace webrtc { +class CriticalSectionWrapper +{ +public: + // Factory method, constructor disabled + static CriticalSectionWrapper* CreateCriticalSection(); + + virtual ~CriticalSectionWrapper() {} + + // Tries to grab lock, beginning of a critical section. Will wait for the + // lock to become available if the grab failed. + virtual void Enter() = 0; + + // Returns a grabbed lock, end of critical section. + virtual void Leave() = 0; +}; + +// RAII extension of the critical section. Prevents Enter/Leave missmatches and +// provides more compact critical section syntax. +class CriticalSectionScoped +{ +public: + CriticalSectionScoped(CriticalSectionWrapper& critsec) + : + _ptrCritSec(&critsec) + { + _ptrCritSec->Enter(); + } + + ~CriticalSectionScoped() + { + if (_ptrCritSec) + { + Leave(); + } + } + +private: + void Leave() + { + _ptrCritSec->Leave(); + _ptrCritSec = 0; + } + + CriticalSectionWrapper* _ptrCritSec; +}; +} // namespace webrtc +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_ diff --git a/src/system_wrappers/interface/event_wrapper.h b/src/system_wrappers/interface/event_wrapper.h new file mode 100644 index 0000000000..0c9a908109 --- /dev/null +++ b/src/system_wrappers/interface/event_wrapper.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ + +namespace webrtc { +enum EventTypeWrapper +{ + kEventSignaled = 1, + kEventError = 2, + kEventTimeout = 3 +}; + +#define WEBRTC_EVENT_10_SEC 10000 +#define WEBRTC_EVENT_INFINITE 0xffffffff + +class EventWrapper +{ +public: + // Factory method. Constructor disabled. + static EventWrapper* Create(); + virtual ~EventWrapper() {} + + // Releases threads who are calling Wait() and has started waiting. Please + // note that a thread calling Wait() will not start waiting immediately. + // assumptions to the contrary is a very common source of issues in + // multithreaded programming. + // Set is sticky in the sense that it will release at least one thread + // either immediately or some time in the future. + virtual bool Set() = 0; + + // Prevents future Wait() calls from finishing without a new Set() call. + virtual bool Reset() = 0; + + // Puts the calling thread into a wait state. The thread may be released + // by a Set() call depending on if other threads are waiting and if so on + // timing. The thread that was released will call Reset() before leaving + // preventing more threads from being released. If multiple threads + // are waiting for the same Set(), only one (random) thread is guaranteed to + // be released. It is possible that multiple (random) threads are released + // Depending on timing. + virtual EventTypeWrapper Wait(unsigned long maxTime) = 0; + + // Starts a timer that will call a non-sticky version of Set() either once + // or periodically. If the timer is periodic it ensures that there is no + // drift over time relative to the system clock. + virtual bool StartTimer(bool periodic, unsigned long time) = 0; + + virtual bool StopTimer() = 0; + + // Only implemented on Windows + // Returns 1 if a key has been pressed since last call to this function. + // -1 indicates failure + // 0 indicates no key has been pressed since last call + // TODO(hellner) this function does not seem to belong here + static int KeyPressed(); +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_EVENT_WRAPPER_H_ diff --git a/src/system_wrappers/interface/file_wrapper.h b/src/system_wrappers/interface/file_wrapper.h new file mode 100644 index 0000000000..8f0cd8c246 --- /dev/null +++ b/src/system_wrappers/interface/file_wrapper.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ + +#include "common_types.h" +#include "typedefs.h" + +// Implementation of an InStream and OutStream that can read (exclusive) or +// write from/to a file. + +namespace webrtc { +class FileWrapper : public InStream, public OutStream +{ +public: + enum { kMaxFileNameSize = 1024}; + enum { kFileMaxTextMessageSize = 1024}; + + // Factory method. Constructor disabled. + static FileWrapper* Create(); + + // Returns true if a file has been opened. + virtual bool Open() const = 0; + + // Opens a file in read or write mode, decided by the readOnly parameter. + virtual WebRtc_Word32 OpenFile(const WebRtc_Word8* fileNameUTF8, + const bool readOnly, + const bool loop = false, + const bool text = false) = 0; + + virtual WebRtc_Word32 CloseFile() = 0; + + // Limits the file size. + virtual WebRtc_Word32 SetMaxFileSize(WebRtc_Word32 bytes) = 0; + + // Flush any pending writes. + virtual WebRtc_Word32 Flush() = 0; + + // Returns the opened file's name in fileNameUTF8. size is the allocated + // size of fileNameUTF8. The name will be truncated if the size of + // fileNameUTF8 is to small. + virtual WebRtc_Word32 FileName(WebRtc_Word8* fileNameUTF8, + WebRtc_UWord32 size) const = 0; + + // Write text to the opened file. The written text can contain plain text + // and text with type specifiers in the same way as sprintf works. + virtual WebRtc_Word32 WriteText(const WebRtc_Word8* text, ...) = 0; + + // Reads len number of bytes from buf to file. + virtual int Read(void* buf, int len) = 0; + + // Writes len number of bytes to buf from file. Please note that the actual + // writing to file may happen some time later. Call flush to force a write + // to take affect + virtual bool Write(const void *buf,int len) = 0; + + // Rewinds the file to the start. Only available when OpenFile() has been + // called with loop argument set to true. Or readOnly argument has been set + // to false. + virtual int Rewind() = 0; +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ diff --git a/src/system_wrappers/interface/list_wrapper.h b/src/system_wrappers/interface/list_wrapper.h new file mode 100644 index 0000000000..bc10ad4a7d --- /dev/null +++ b/src/system_wrappers/interface/list_wrapper.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_ + +#include "constructor_magic.h" + +namespace webrtc { +class CriticalSectionWrapper; + +class ListItem +{ +friend class ListWrapper; + +public: + ListItem(const void* ptr); + ListItem(const unsigned int item); + virtual ~ListItem(); + void* GetItem() const; + unsigned int GetUnsignedItem() const; + +protected: + ListItem* next_; + ListItem* prev_; + +private: + const void* item_ptr_; + const unsigned int item_; + DISALLOW_COPY_AND_ASSIGN(ListItem); +}; + +class ListWrapper +{ +public: + ListWrapper(); + virtual ~ListWrapper(); + + // Returns the number of elements stored in the list. + unsigned int GetSize() const; + + // Puts a pointer to anything last in the list. + int PushBack(const void* ptr); + // Puts a pointer to anything first in the list. + int PushFront(const void* ptr); + + // Puts a copy of the specified integer last in the list. + int PushBack(const unsigned int item_id); + // Puts a copy of the specified integer first in the list. + int PushFront(const unsigned int item_id); + + // Pops the first ListItem from the list + int PopFront(); + + // Pops the last ListItem from the list + int PopBack(); + + // Returns true if the list is empty + bool Empty() const; + + // Returns a pointer to the first ListItem in the list. + ListItem* First() const; + + // Returns a pointer to the last ListItem in the list. + ListItem* Last() const; + + // Returns a pointer to the ListItem stored after item in the list. + ListItem* Next(ListItem* item) const; + + // Returns a pointer to the ListItem stored before item in the list. + ListItem* Previous(ListItem* item) const; + + // Removes item from the list. + int Erase(ListItem* item); + + // Insert list item after existing_previous_item. Please note that new_item + // must be created using new ListItem(). The map will take ownership of + // new_item following a successfull insert. If insert fails new_item will + // not be released by the List + int Insert(ListItem* existing_previous_item, + ListItem* new_item); + + // Insert list item before existing_next_item. Please note that new_item + // must be created using new ListItem(). The map will take ownership of + // new_item following a successfull insert. If insert fails new_item will + // not be released by the List + int InsertBefore(ListItem* existing_next_item, + ListItem* new_item); + +private: + void PushBackImpl(ListItem* item); + void PushFrontImpl(ListItem* item); + + CriticalSectionWrapper* critical_section_; + ListItem* first_; + ListItem* last_; + unsigned int size_; + DISALLOW_COPY_AND_ASSIGN(ListWrapper); +}; +} //namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_ diff --git a/src/system_wrappers/interface/map_wrapper.h b/src/system_wrappers/interface/map_wrapper.h new file mode 100644 index 0000000000..9297382cd0 --- /dev/null +++ b/src/system_wrappers/interface/map_wrapper.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ + +#include <map> + +#include "constructor_magic.h" + +namespace webrtc { +class MapItem +{ +friend class MapWrapper; + +public: + MapItem(int id, void* ptr); + virtual ~MapItem(); + void* GetItem(); + int GetId(); + unsigned int GetUnsignedId(); + void SetItem(void* ptr); + +private: + int item_id_; + void* item_pointer_; + DISALLOW_COPY_AND_ASSIGN(MapItem); +}; + +class MapWrapper +{ +public: + MapWrapper(); + ~MapWrapper(); + + // Puts a pointer to anything in the map and associates it with id. Note, id + // needs to be unique for all items in the map. + int Insert(int id, void* ptr); + + // Removes item from map. + int Erase(MapItem* item); + + // Finds item with associated with id and removes it from the map. + int Erase(int id); + + // Returns the number of elements stored in the map. + int Size() const; + + // Returns a pointer to the first MapItem in the map. + MapItem* First() const; + + // Returns a pointer to the last MapItem in the map. + MapItem* Last() const; + + // Returns a pointer to the MapItem stored after item in the map. + MapItem* Next(MapItem* item) const; + + // Returns a pointer to the MapItem stored before item in the map. + MapItem* Previous(MapItem* item) const; + + // Returns a pointer to the MapItem associated with id from the map. + MapItem* Find(int id) const; + +private: + std::map<int, MapItem*> map_; + DISALLOW_COPY_AND_ASSIGN(MapWrapper); +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_MAP_WRAPPER_H_ diff --git a/src/system_wrappers/interface/rw_lock_wrapper.h b/src/system_wrappers/interface/rw_lock_wrapper.h new file mode 100644 index 0000000000..f0842ac852 --- /dev/null +++ b/src/system_wrappers/interface/rw_lock_wrapper.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ + +// Note, Windows pre-Vista version of RW locks are not supported nativly. For +// these OSs regular critical sections have been used to approximate RW lock +// functionality and will therefore have worse performance. + +namespace webrtc { +class RWLockWrapper +{ +public: + static RWLockWrapper* CreateRWLock(); + virtual ~RWLockWrapper(); + + virtual void AcquireLockExclusive() = 0; + virtual void ReleaseLockExclusive() = 0; + + virtual void AcquireLockShared() = 0; + virtual void ReleaseLockShared() = 0; + +protected: + virtual int Init() = 0; +}; + +// RAII extensions of the RW lock. Prevents Acquire/Release missmatches and +// provides more compact locking syntax. +class ReadLockScoped +{ +public: + ReadLockScoped(RWLockWrapper& rwLock) + : + _rwLock(rwLock) + { + _rwLock.AcquireLockShared(); + } + + ~ReadLockScoped() + { + _rwLock.ReleaseLockShared(); + } + +private: + RWLockWrapper& _rwLock; +}; + +class WriteLockScoped +{ +public: + WriteLockScoped(RWLockWrapper& rwLock) + : + _rwLock(rwLock) + { + _rwLock.AcquireLockExclusive(); + } + + ~WriteLockScoped() + { + _rwLock.ReleaseLockExclusive(); + } + +private: + RWLockWrapper& _rwLock; +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_ diff --git a/src/system_wrappers/interface/sort.h b/src/system_wrappers/interface/sort.h new file mode 100644 index 0000000000..fb25ecfc6f --- /dev/null +++ b/src/system_wrappers/interface/sort.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// Generic unstable sorting routines. + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_ + +#include "typedefs.h" +#include "common_types.h" + +namespace webrtc +{ + enum Type + { + TYPE_Word8, + TYPE_UWord8, + TYPE_Word16, + TYPE_UWord16, + TYPE_Word32, + TYPE_UWord32, + TYPE_Word64, + TYPE_UWord64, + TYPE_Float32, + TYPE_Float64 + }; + // Sorts intrinsic data types. + // + // data [in/out] A pointer to an array of intrinsic type. + // Upon return it will be sorted in ascending order. + // numOfElements The number of elements in the array. + // dataType Enum corresponding to the type of the array. + // + // returns 0 on success, -1 on failure. + WebRtc_Word32 Sort(void* data, WebRtc_UWord32 numOfElements, Type dataType); + + // Sorts arbitrary data types. This requires an array of intrinsically typed + // key values which will be used to sort the data array. There must be a + // one-to-one correspondence between data elements and key elements, with + // corresponding elements sharing the same position in their respective + // arrays. + // + // data [in/out] A pointer to an array of arbitrary type. + // Upon return it will be sorted in ascending order. + // key [in] A pointer to an array of keys used to sort the + // data array. + // numOfElements The number of elements in the arrays. + // sizeOfElement The size, in bytes, of the data array. + // keyType Enum corresponding to the type of the key array. + // + // returns 0 on success, -1 on failure. + // + WebRtc_Word32 KeySort(void* data, void* key, WebRtc_UWord32 numOfElements, + WebRtc_UWord32 sizeOfElement, Type keyType); +} + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_ diff --git a/src/system_wrappers/interface/thread_wrapper.h b/src/system_wrappers/interface/thread_wrapper.h new file mode 100644 index 0000000000..eccf3c2399 --- /dev/null +++ b/src/system_wrappers/interface/thread_wrapper.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// System independant wrapper for spawning threads +// Note: the spawned thread will loop over the callback function until stopped. +// Note: The callback function is expected to return every 2 seconds or more +// often. + +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ + +namespace webrtc { +// Object that will be passed by the spawned thread when it enters the callback +// function. +#define ThreadObj void* + +// Callback function that the spawned thread will enter once spawned +typedef bool(*ThreadRunFunction)(ThreadObj); + +enum ThreadPriority +{ + kLowPriority = 1, + kNormalPriority = 2, + kHighPriority = 3, + kHighestPriority = 4, + kRealtimePriority = 5 +}; + +class ThreadWrapper +{ +public: + enum {kThreadMaxNameLength = 64}; + + virtual ~ThreadWrapper() {}; + + // Factory method. Constructor disabled. + // + // func Pointer to a, by user, specified callback function. + // obj Object associated with the thread. Passed in the callback + // function. + // prio Thread priority. May require root/admin rights. + // threadName NULL terminated thread name, will be visable in the Windows + // debugger. + static ThreadWrapper* CreateThread(ThreadRunFunction func = 0, + ThreadObj obj= 0, + ThreadPriority prio = kNormalPriority, + const char* threadName = 0); + + // Non blocking termination of the spawned thread. Note that it is not safe + // to delete this class until the spawned thread has been reclaimed. + virtual void SetNotAlive() = 0; + + // Spawns the thread. This will start the triggering of the callback + // function. + virtual bool Start(unsigned int& id) = 0; + + // Sets the threads CPU affinity. CPUs are listed 0 - (number of CPUs - 1). + // The numbers in processorNumbers specify which CPUs are allowed to run the + // thread. processorNumbers should not contain any duplicates and elements + // should be lower than (number of CPUs - 1). amountOfProcessors should be + // equal to the number of processors listed in processorNumbers + virtual bool SetAffinity(const int* /*processorNumbers*/, + const unsigned int /*amountOfProcessors*/) + {return false;} + + // Stops the spawned thread and waits for it to be reclaimed with a timeout + // of two seconds. Will return false if the thread was not reclaimed. + // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds). + // It's ok to call Stop() even if the spawned thread has been reclaimed. + virtual bool Stop() = 0; + + // Stops the spawned thread dead in its tracks. Will likely result in a + // corrupt state. There should be an extremely good reason for even looking + // at this function. Can cause many problems deadlock being one of them. + virtual bool Shutdown() {return false;} +}; +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ diff --git a/src/system_wrappers/interface/tick_util.h b/src/system_wrappers/interface/tick_util.h new file mode 100644 index 0000000000..4c280677b5 --- /dev/null +++ b/src/system_wrappers/interface/tick_util.h @@ -0,0 +1,304 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// System independant wrapper for polling elapsed time in ms and us. +// The implementation works in the tick domain which can be mapped over to the +// time domain. +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ + +#if _WIN32 +#include <windows.h> +#include <mmsystem.h> +#elif WEBRTC_LINUX +#include <ctime> +#else +#include <sys/time.h> +#include <time.h> +#endif + +#include "typedefs.h" + +namespace webrtc { +class TickInterval; + +class TickTime +{ +public: + // Current time in the tick domain. + static TickTime Now(); + + // Now in the time domain in ms. + static WebRtc_Word64 MillisecondTimestamp(); + + // Now in the time domain in us. + static WebRtc_Word64 MicrosecondTimestamp(); + + WebRtc_Word64 Ticks() const; + + static WebRtc_Word64 MillisecondsToTicks(const WebRtc_Word64 ms); + + static WebRtc_Word64 TicksToMilliseconds(const WebRtc_Word64 ticks); + + // Returns a TickTime that is ticks later than the passed TickTime + friend TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks); + TickTime& operator+=(const WebRtc_Word64& rhs); + + + // Returns a TickInterval that is the difference in ticks beween rhs and lhs + friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); +private: + WebRtc_Word64 _ticks; +}; + +class TickInterval +{ +public: + TickInterval(); + + WebRtc_Word64 Milliseconds() const; + WebRtc_Word64 Microseconds() const; + + // Returns the sum of two TickIntervals as a TickInterval + friend TickInterval operator+(const TickInterval& lhs, + const TickInterval& rhs); + TickInterval& operator-=(const TickInterval& rhs); + + // Returns a TickInterval corresponding to rhs - lhs + friend TickInterval operator-(const TickInterval& lhs, + const TickInterval& rhs); + TickInterval& operator+=(const TickInterval& rhs); + +private: + TickInterval(WebRtc_Word64 interval); + + friend class TickTime; + friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs); + +private: + WebRtc_Word64 _interval; +}; + +inline TickInterval operator+(const TickInterval& lhs, const TickInterval& rhs) +{ + return TickInterval(lhs._interval + rhs._interval); +} + +inline TickInterval operator-(const TickInterval& lhs, const TickInterval& rhs) +{ + return TickInterval(lhs._interval - rhs._interval); +} + +inline TickInterval operator-(const TickTime& lhs,const TickTime& rhs) +{ + return TickInterval(lhs._ticks - rhs._ticks); +} + +inline TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks) +{ + TickTime time = lhs; + time._ticks += ticks; + return time; +} + +inline TickTime TickTime::Now() +{ + TickTime result; +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + // QueryPerformanceCounter returns the value from the TSC which is + // incremented at the CPU frequency. The algorithm used requires + // the CPU frequency to be constant. Technology like speed stepping + // which has variable CPU frequency will therefore yield unpredictable, + // incorrect time estimations. + LARGE_INTEGER qpcnt; + QueryPerformanceCounter(&qpcnt); + result._ticks = qpcnt.QuadPart; + #else + static volatile LONG lastTimeGetTime = 0; + static volatile WebRtc_Word64 numWrapTimeGetTime = 0; + volatile LONG* lastTimeGetTimePtr = &lastTimeGetTime; + DWORD now = timeGetTime(); + // Atomically update the last gotten time + DWORD old = InterlockedExchange(lastTimeGetTimePtr, now); + if(now < old) + { + // If now is earlier than old, there may have been a race between + // threads. + // 0x0fffffff ~3.1 days, the code will not take that long to execute + // so it must have been a wrap around. + if(old > 0xf0000000 && now < 0x0fffffff) + { + numWrapTimeGetTime++; + } + } + result._ticks = now + (numWrapTimeGetTime<<32); + #endif +#elif defined(WEBRTC_LINUX) + struct timespec ts; + #ifdef WEBRTC_CLOCK_TYPE_REALTIME + clock_gettime(CLOCK_REALTIME, &ts); + #else + clock_gettime(CLOCK_MONOTONIC, &ts); + #endif + result._ticks = 1000000000LL * static_cast<WebRtc_Word64>(ts.tv_sec) + static_cast<WebRtc_Word64>(ts.tv_nsec); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + result._ticks = 1000000LL * static_cast<WebRtc_Word64>(tv.tv_sec) + static_cast<WebRtc_Word64>(tv.tv_usec); +#endif + return result; +} + +inline WebRtc_Word64 TickTime::MillisecondTimestamp() +{ + TickTime now = TickTime::Now(); +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (now._ticks * 1000) / qpfreq.QuadPart; + #else + return now._ticks; + #endif +#elif WEBRTC_LINUX + return now._ticks / 1000000LL; +#else + return now._ticks / 1000LL; +#endif +} + +inline WebRtc_Word64 TickTime::MicrosecondTimestamp() +{ + TickTime now = TickTime::Now(); + +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (now._ticks * 1000) / (qpfreq.QuadPart/1000); + #else + return now._ticks *1000LL; + #endif +#elif WEBRTC_LINUX + return now._ticks / 1000LL; +#else + return now._ticks; +#endif +} + +inline WebRtc_Word64 TickTime::Ticks() const +{ + return _ticks; +} + +inline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms) +{ +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (qpfreq.QuadPart * ms) / 1000; + #else + return ms; + #endif +#elif WEBRTC_LINUX + return ms * 1000000LL; +#else + return ms * 1000LL; +#endif +} + +inline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks) +{ +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (ticks * 1000) / qpfreq.QuadPart; + #else + return ticks; + #endif +#elif WEBRTC_LINUX + return ticks / 1000000LL; +#else + return ticks / 1000LL; +#endif +} + +inline TickTime& TickTime::operator+=(const WebRtc_Word64& ticks) +{ + _ticks += ticks; + return *this; +} + +inline TickInterval::TickInterval() : _interval(0) +{ +} + +inline TickInterval::TickInterval(const WebRtc_Word64 interval) + : _interval(interval) +{ +} + +inline WebRtc_Word64 TickInterval::Milliseconds() const +{ +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (_interval * 1000) / qpfreq.QuadPart; + #else + // _interval is in ms + return _interval; + #endif +#elif WEBRTC_LINUX + // _interval is in ns + return _interval / 1000000; +#else + // _interval is usecs + return _interval / 1000; +#endif +} + +inline WebRtc_Word64 TickInterval::Microseconds() const +{ +#if _WIN32 + #ifdef USE_QUERY_PERFORMANCE_COUNTER + LARGE_INTEGER qpfreq; + QueryPerformanceFrequency(&qpfreq); + return (_interval * 1000000) / qpfreq.QuadPart; + #else + // _interval is in ms + return _interval *1000LL; + #endif +#elif WEBRTC_LINUX + // _interval is in ns + return _interval / 1000; +#else + // _interval is usecs + return _interval; +#endif +} + +inline TickInterval& TickInterval::operator+=(const TickInterval& rhs) +{ + _interval += rhs._interval; + return *this; +} + +inline TickInterval& TickInterval::operator-=(const TickInterval& rhs) +{ + _interval -= rhs._interval; + return *this; +} +} // namespace webrtc + +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_ diff --git a/src/system_wrappers/interface/trace.h b/src/system_wrappers/interface/trace.h new file mode 100644 index 0000000000..0f7df4d46e --- /dev/null +++ b/src/system_wrappers/interface/trace.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +// System independant wrapper for logging runtime information to file. +// Note: All log messages will be written to the same trace file. +// Note: If to many messages are written to file there will be a build up of +// messages. Apply filtering to avoid that. +#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_H_ +#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_H_ + +#include "common_types.h" +#include "typedefs.h" + +#ifdef WEBRTC_NO_TRACE + #define WEBRTC_TRACE +#else + // Ideally we would use __VA_ARGS__ but it's not supported by all compilers + // such as VS2003 (it's supported in VS2005). TODO (hellner) why + // would this be better than current implementation (not convinced)? + #define WEBRTC_TRACE Trace::Add +#endif + +namespace webrtc { +class Trace +{ +public: + + // Increments the reference count to the trace. + static void CreateTrace(); + // Decrements the reference count to the trace. + static void ReturnTrace(); + // Note: any instance that writes to the trace file should increment and + // decrement the reference count on construction and destruction + // respectively + + // Specifies what type of messages should be written to the trace file. The + // filter parameter is a bitmask where each message type is enumerated by + // the TraceLevel enumerator. TODO (hellner) why is the + // TraceLevel enumerator not defined in this file? + static WebRtc_Word32 SetLevelFilter(const WebRtc_UWord32 filter); + + // Returns what type of messages are written to the trace file. + static WebRtc_Word32 LevelFilter(WebRtc_UWord32& filter); + + // Sets the file name. If addFileCounter is false the same file will be + // reused when it fills up. If it's true a new file with incremented name + // will be used. + static WebRtc_Word32 SetTraceFile(const WebRtc_Word8* fileName, + const bool addFileCounter = false); + + // Returns the name of the file that the trace is currently writing to. + static WebRtc_Word32 TraceFile(WebRtc_Word8 fileName[1024]); + + // Registers callback to receive trace messages. TODO (hellner) + // why not use OutStream instead? Why is TraceCallback not defined in this + // file + static WebRtc_Word32 SetTraceCallback(TraceCallback* callback); + + // Adds a trace message for writing to file. The message is put in a queue + // for writing to file whenever possible for performance reasons. I.e. there + // is a crash it is possible that the last, vital logs are not logged yet. + // level is the the type of message to log. If that type of messages is + // filtered it will not be written to file. module is an identifier for what + // part of the code the message is comming. + // id is an identifier that should be unique for that set of classes that + // are associated (e.g. all instances owned by an engine). + // msg and the elipsis are the same as e.g. sprintf. + // TODO (hellner) Why is TraceModule not defined in this file? + static void Add(const TraceLevel level, + const TraceModule module, + const WebRtc_Word32 id, + const char* msg, ...); + +}; +} // namespace webrtc +#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_H_ |