Update code to current Chromium master
This corresponds to: Chromium: 6555f9456074c0c0e5f7713564b978588ac04a5d webrtc: c8b569e0a7ad0b369e15f0197b3a558699ec8efa
This commit is contained in:
88
webrtc/system_wrappers/include/aligned_array.h
Normal file
88
webrtc/system_wrappers/include/aligned_array.h
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2014 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_INCLUDE_ALIGNED_ARRAY_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/system_wrappers/include/aligned_malloc.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Wrapper class for aligned arrays. Every row (and the first dimension) are
|
||||
// aligned to the given byte alignment.
|
||||
template<typename T> class AlignedArray {
|
||||
public:
|
||||
AlignedArray(int rows, size_t cols, int alignment)
|
||||
: rows_(rows),
|
||||
cols_(cols),
|
||||
alignment_(alignment) {
|
||||
RTC_CHECK_GT(alignment_, 0);
|
||||
head_row_ = static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_),
|
||||
alignment_));
|
||||
for (int i = 0; i < rows_; ++i) {
|
||||
head_row_[i] = static_cast<T*>(AlignedMalloc(cols_ * sizeof(**head_row_),
|
||||
alignment_));
|
||||
}
|
||||
}
|
||||
|
||||
~AlignedArray() {
|
||||
for (int i = 0; i < rows_; ++i) {
|
||||
AlignedFree(head_row_[i]);
|
||||
}
|
||||
AlignedFree(head_row_);
|
||||
}
|
||||
|
||||
T* const* Array() {
|
||||
return head_row_;
|
||||
}
|
||||
|
||||
const T* const* Array() const {
|
||||
return head_row_;
|
||||
}
|
||||
|
||||
T* Row(int row) {
|
||||
RTC_CHECK_LE(row, rows_);
|
||||
return head_row_[row];
|
||||
}
|
||||
|
||||
const T* Row(int row) const {
|
||||
RTC_CHECK_LE(row, rows_);
|
||||
return head_row_[row];
|
||||
}
|
||||
|
||||
T& At(int row, size_t col) {
|
||||
RTC_CHECK_LE(col, cols_);
|
||||
return Row(row)[col];
|
||||
}
|
||||
|
||||
const T& At(int row, size_t col) const {
|
||||
RTC_CHECK_LE(col, cols_);
|
||||
return Row(row)[col];
|
||||
}
|
||||
|
||||
int rows() const {
|
||||
return rows_;
|
||||
}
|
||||
|
||||
size_t cols() const {
|
||||
return cols_;
|
||||
}
|
||||
|
||||
private:
|
||||
int rows_;
|
||||
size_t cols_;
|
||||
int alignment_;
|
||||
T** head_row_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_ARRAY_
|
59
webrtc/system_wrappers/include/aligned_malloc.h
Normal file
59
webrtc/system_wrappers/include/aligned_malloc.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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_INCLUDE_ALIGNED_MALLOC_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_MALLOC_H_
|
||||
|
||||
// The functions declared here
|
||||
// 1) Allocates block of aligned memory.
|
||||
// 2) Re-calculates a pointer such that it is aligned to a higher or equal
|
||||
// address.
|
||||
// Note: alignment must be a power of two. The alignment is in bytes.
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Returns a pointer to the first boundry of |alignment| bytes following the
|
||||
// address of |ptr|.
|
||||
// Note that there is no guarantee that the memory in question is available.
|
||||
// |ptr| has no requirements other than it can't be NULL.
|
||||
void* GetRightAlign(const void* ptr, size_t alignment);
|
||||
|
||||
// Allocates memory of |size| bytes aligned on an |alignment| boundry.
|
||||
// The return value is a pointer to the memory. Note that the memory must
|
||||
// be de-allocated using AlignedFree.
|
||||
void* AlignedMalloc(size_t size, size_t alignment);
|
||||
// De-allocates memory created using the AlignedMalloc() API.
|
||||
void AlignedFree(void* mem_block);
|
||||
|
||||
// Templated versions to facilitate usage of aligned malloc without casting
|
||||
// to and from void*.
|
||||
template<typename T>
|
||||
T* GetRightAlign(const T* ptr, size_t alignment) {
|
||||
return reinterpret_cast<T*>(GetRightAlign(reinterpret_cast<const void*>(ptr),
|
||||
alignment));
|
||||
}
|
||||
template<typename T>
|
||||
T* AlignedMalloc(size_t size, size_t alignment) {
|
||||
return reinterpret_cast<T*>(AlignedMalloc(size, alignment));
|
||||
}
|
||||
|
||||
// Deleter for use with scoped_ptr. E.g., use as
|
||||
// scoped_ptr<Foo, AlignedFreeDeleter> foo;
|
||||
struct AlignedFreeDeleter {
|
||||
inline void operator()(void* ptr) const {
|
||||
AlignedFree(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ALIGNED_MALLOC_H_
|
66
webrtc/system_wrappers/include/asm_defines.h
Normal file
66
webrtc/system_wrappers/include/asm_defines.h
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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_INCLUDE_ASM_DEFINES_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ASM_DEFINES_H_
|
||||
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
// Define the macros used in ARM assembly code, so that for Mac or iOS builds
|
||||
// we add leading underscores for the function names.
|
||||
#ifdef __APPLE__
|
||||
.macro GLOBAL_FUNCTION name
|
||||
.global _\name
|
||||
.private_extern _\name
|
||||
.endm
|
||||
.macro DEFINE_FUNCTION name
|
||||
_\name:
|
||||
.endm
|
||||
.macro CALL_FUNCTION name
|
||||
bl _\name
|
||||
.endm
|
||||
.macro GLOBAL_LABEL name
|
||||
.global _\name
|
||||
.private_extern _\name
|
||||
.endm
|
||||
#else
|
||||
.macro GLOBAL_FUNCTION name
|
||||
.global \name
|
||||
.hidden \name
|
||||
.endm
|
||||
.macro DEFINE_FUNCTION name
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.type \name,%function
|
||||
#endif
|
||||
\name:
|
||||
.endm
|
||||
.macro CALL_FUNCTION name
|
||||
bl \name
|
||||
.endm
|
||||
.macro GLOBAL_LABEL name
|
||||
.global \name
|
||||
.hidden \name
|
||||
.endm
|
||||
#endif
|
||||
|
||||
// With Apple's clang compiler, for instructions ldrb, strh, etc.,
|
||||
// the condition code is after the width specifier. Here we define
|
||||
// only the ones that are actually used in the assembly files.
|
||||
#if (defined __llvm__) && (defined __APPLE__)
|
||||
.macro streqh reg1, reg2, num
|
||||
strheq \reg1, \reg2, \num
|
||||
.endm
|
||||
#endif
|
||||
|
||||
.text
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_ASM_DEFINES_H_
|
24
webrtc/system_wrappers/include/compile_assert_c.h
Normal file
24
webrtc/system_wrappers/include/compile_assert_c.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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_INCLUDE_COMPILE_ASSERT_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_COMPILE_ASSERT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
#error "Only use this for C files. For C++, use static_assert."
|
||||
#endif
|
||||
|
||||
// Use this macro to verify at compile time that certain restrictions are met.
|
||||
// The argument is the boolean expression to evaluate.
|
||||
// Example:
|
||||
// COMPILE_ASSERT(sizeof(foo) < 128);
|
||||
#define COMPILE_ASSERT(expression) switch (0) {case 0: case expression:;}
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_COMPILE_ASSERT_H_
|
51
webrtc/system_wrappers/include/cpu_features_wrapper.h
Normal file
51
webrtc/system_wrappers/include/cpu_features_wrapper.h
Normal file
@ -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_INCLUDE_CPU_FEATURES_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CPU_FEATURES_WRAPPER_H_
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
// List of features in x86.
|
||||
typedef enum {
|
||||
kSSE2,
|
||||
kSSE3
|
||||
} CPUFeature;
|
||||
|
||||
// List of features in ARM.
|
||||
enum {
|
||||
kCPUFeatureARMv7 = (1 << 0),
|
||||
kCPUFeatureVFPv3 = (1 << 1),
|
||||
kCPUFeatureNEON = (1 << 2),
|
||||
kCPUFeatureLDREXSTREX = (1 << 3)
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
// Return the features in an ARM device.
|
||||
// It detects the features in the hardware platform, and returns supported
|
||||
// values in the above enum definition as a bitmask.
|
||||
extern uint64_t WebRtc_GetCPUFeaturesARM(void);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CPU_FEATURES_WRAPPER_H_
|
54
webrtc/system_wrappers/include/critical_section_wrapper.h
Normal file
54
webrtc/system_wrappers/include/critical_section_wrapper.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
|
||||
|
||||
// If the critical section is heavily contended it may be beneficial to use
|
||||
// read/write locks instead.
|
||||
|
||||
#include "webrtc/base/thread_annotations.h"
|
||||
#include "webrtc/common_types.h"
|
||||
|
||||
namespace webrtc {
|
||||
class LOCKABLE 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() EXCLUSIVE_LOCK_FUNCTION() = 0;
|
||||
|
||||
// Returns a grabbed lock, end of critical section.
|
||||
virtual void Leave() UNLOCK_FUNCTION() = 0;
|
||||
};
|
||||
|
||||
// RAII extension of the critical section. Prevents Enter/Leave mismatches and
|
||||
// provides more compact critical section syntax.
|
||||
class SCOPED_LOCKABLE CriticalSectionScoped {
|
||||
public:
|
||||
explicit CriticalSectionScoped(CriticalSectionWrapper* critsec)
|
||||
EXCLUSIVE_LOCK_FUNCTION(critsec)
|
||||
: ptr_crit_sec_(critsec) {
|
||||
ptr_crit_sec_->Enter();
|
||||
}
|
||||
|
||||
~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); }
|
||||
|
||||
private:
|
||||
CriticalSectionWrapper* ptr_crit_sec_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
|
70
webrtc/system_wrappers/include/event_wrapper.h
Normal file
70
webrtc/system_wrappers/include/event_wrapper.h
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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_INCLUDE_EVENT_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_
|
||||
|
||||
namespace webrtc {
|
||||
enum EventTypeWrapper {
|
||||
kEventSignaled = 1,
|
||||
kEventError = 2,
|
||||
kEventTimeout = 3
|
||||
};
|
||||
|
||||
#define WEBRTC_EVENT_INFINITE 0xffffffff
|
||||
|
||||
class EventTimerWrapper;
|
||||
|
||||
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;
|
||||
|
||||
// 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 reset the event 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.
|
||||
//
|
||||
// |max_time| is the maximum time to wait in milliseconds or
|
||||
// WEBRTC_EVENT_INFINITE to wait infinitely.
|
||||
virtual EventTypeWrapper Wait(unsigned long max_time) = 0;
|
||||
};
|
||||
|
||||
class EventTimerWrapper : public EventWrapper {
|
||||
public:
|
||||
static EventTimerWrapper* Create();
|
||||
|
||||
// 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.
|
||||
//
|
||||
// |time| is in milliseconds.
|
||||
virtual bool StartTimer(bool periodic, unsigned long time) = 0;
|
||||
|
||||
virtual bool StopTimer() = 0;
|
||||
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_
|
78
webrtc/system_wrappers/include/file_wrapper.h
Normal file
78
webrtc/system_wrappers/include/file_wrapper.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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_INCLUDE_FILE_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "webrtc/common_types.h"
|
||||
#include "webrtc/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:
|
||||
static const size_t kMaxFileNameSize = 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 read_only parameter.
|
||||
virtual int OpenFile(const char* file_name_utf8,
|
||||
bool read_only,
|
||||
bool loop = false,
|
||||
bool text = false) = 0;
|
||||
|
||||
// Initializes the wrapper from an existing handle. |read_only| must match in
|
||||
// the mode the file was opened in. If |manage_file| is true, the wrapper
|
||||
// takes ownership of |handle| and closes it in CloseFile().
|
||||
virtual int OpenFromFileHandle(FILE* handle,
|
||||
bool manage_file,
|
||||
bool read_only,
|
||||
bool loop = false) = 0;
|
||||
|
||||
virtual int CloseFile() = 0;
|
||||
|
||||
// Limits the file size to |bytes|. Writing will fail after the cap
|
||||
// is hit. Pass zero to use an unlimited size.
|
||||
virtual int SetMaxFileSize(size_t bytes) = 0;
|
||||
|
||||
// Flush any pending writes.
|
||||
virtual int Flush() = 0;
|
||||
|
||||
// Returns the opened file's name in |file_name_utf8|. Provide the size of
|
||||
// the buffer in bytes in |size|. The name will be truncated if |size| is
|
||||
// too small.
|
||||
virtual int FileName(char* file_name_utf8,
|
||||
size_t size) const = 0;
|
||||
|
||||
// Write |format| to the opened file. Arguments are taken in the same manner
|
||||
// as printf. That is, supply a format string containing text and
|
||||
// specifiers. Returns the number of characters written or -1 on error.
|
||||
virtual int WriteText(const char* format, ...) = 0;
|
||||
|
||||
// Inherited from both Instream and OutStream.
|
||||
// Rewinds the file to the start. Only available when OpenFile() has been
|
||||
// called with |loop| == true or |readOnly| == true.
|
||||
// virtual int Rewind() = 0;
|
||||
int Rewind() override;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_FILE_WRAPPER_H_
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2013 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.
|
||||
*/
|
||||
|
||||
// Various inline functions and macros to fix compilation of 32 bit target
|
||||
// on MSVC with /Wp64 flag enabled.
|
||||
|
||||
// The original code can be found here:
|
||||
// http://src.chromium.org/svn/trunk/src/base/fix_wp64.h
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_FIX_INTERLOCKED_EXCHANGE_POINTER_WINDOWS_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_FIX_INTERLOCKED_EXCHANGE_POINTER_WINDOWS_H_
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
// Platform SDK fixes when building with /Wp64 for a 32 bits target.
|
||||
#if !defined(_WIN64) && defined(_Wp64)
|
||||
|
||||
#ifdef InterlockedExchangePointer
|
||||
#undef InterlockedExchangePointer
|
||||
// The problem is that the macro provided for InterlockedExchangePointer() is
|
||||
// doing a (LONG) C-style cast that triggers invariably the warning C4312 when
|
||||
// building on 32 bits.
|
||||
inline void* InterlockedExchangePointer(void* volatile* target, void* value) {
|
||||
return reinterpret_cast<void*>(static_cast<LONG_PTR>(InterlockedExchange(
|
||||
reinterpret_cast<volatile LONG*>(target),
|
||||
static_cast<LONG>(reinterpret_cast<LONG_PTR>(value)))));
|
||||
}
|
||||
#endif // #ifdef InterlockedExchangePointer
|
||||
|
||||
#endif // #if !defined(_WIN64) && defined(_Wp64)
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FIX_INTERLOCKED_EXCHANGE_POINTER_WINDOWS_H_
|
161
webrtc/system_wrappers/include/logging.h
Normal file
161
webrtc/system_wrappers/include/logging.h
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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.
|
||||
*/
|
||||
|
||||
// This is a highly stripped-down version of libjingle's talk/base/logging.h.
|
||||
// It is a thin wrapper around WEBRTC_TRACE, maintaining the libjingle log
|
||||
// semantics to ease a transition to that format.
|
||||
|
||||
// NOTE: LS_INFO maps to a new trace level which should be reserved for
|
||||
// infrequent, non-verbose logs. The other levels below kTraceWarning have been
|
||||
// rendered essentially useless due to their verbosity. Carefully consider the
|
||||
// impact of adding a new LS_INFO log. If it will be logged at anything
|
||||
// approaching a frame or packet frequency, use LS_VERBOSE if necessary, or
|
||||
// preferably, do not log at all.
|
||||
|
||||
// LOG(...) an ostream target that can be used to send formatted
|
||||
// output to a variety of logging targets, such as debugger console, stderr,
|
||||
// file, or any StreamInterface.
|
||||
// The severity level passed as the first argument to the LOGging
|
||||
// functions is used as a filter, to limit the verbosity of the logging.
|
||||
// Static members of LogMessage documented below are used to control the
|
||||
// verbosity and target of the output.
|
||||
// There are several variations on the LOG macro which facilitate logging
|
||||
// of common error conditions, detailed below.
|
||||
|
||||
// LOG(sev) logs the given stream at severity "sev", which must be a
|
||||
// compile-time constant of the LoggingSeverity type, without the namespace
|
||||
// prefix.
|
||||
// LOG_V(sev) Like LOG(), but sev is a run-time variable of the LoggingSeverity
|
||||
// type (basically, it just doesn't prepend the namespace).
|
||||
// LOG_F(sev) Like LOG(), but includes the name of the current function.
|
||||
|
||||
// Additional helper macros added by WebRTC:
|
||||
// LOG_API is a shortcut for API call logging. Pass in the input parameters of
|
||||
// the method. For example:
|
||||
// Foo(int bar, int baz) {
|
||||
// LOG_API2(bar, baz);
|
||||
// }
|
||||
//
|
||||
// LOG_FERR is a shortcut for logging a failed function call. For example:
|
||||
// if (!Foo(bar)) {
|
||||
// LOG_FERR1(LS_WARNING, Foo, bar);
|
||||
// }
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Note that the non-standard LoggingSeverity aliases exist because they are
|
||||
// still in broad use. The meanings of the levels are:
|
||||
// LS_SENSITIVE: Information which should only be logged with the consent
|
||||
// of the user, due to privacy concerns.
|
||||
// LS_VERBOSE: This level is for data which we do not want to appear in the
|
||||
// normal debug log, but should appear in diagnostic logs.
|
||||
// LS_INFO: Chatty level used in debugging for all sorts of things, the default
|
||||
// in debug builds.
|
||||
// LS_WARNING: Something that may warrant investigation.
|
||||
// LS_ERROR: Something that should not have occurred.
|
||||
enum LoggingSeverity {
|
||||
LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR
|
||||
};
|
||||
|
||||
class LogMessage {
|
||||
public:
|
||||
LogMessage(const char* file, int line, LoggingSeverity sev);
|
||||
~LogMessage();
|
||||
|
||||
static bool Loggable(LoggingSeverity sev);
|
||||
std::ostream& stream() { return print_stream_; }
|
||||
|
||||
private:
|
||||
// The ostream that buffers the formatted message before output
|
||||
std::ostringstream print_stream_;
|
||||
|
||||
// The severity level of this message
|
||||
LoggingSeverity severity_;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Macros which automatically disable logging when WEBRTC_LOGGING == 0
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LOG
|
||||
// The following non-obvious technique for implementation of a
|
||||
// conditional log stream was stolen from google3/base/logging.h.
|
||||
|
||||
// This class is used to explicitly ignore values in the conditional
|
||||
// logging macros. This avoids compiler warnings like "value computed
|
||||
// is not used" and "statement has no effect".
|
||||
|
||||
class LogMessageVoidify {
|
||||
public:
|
||||
LogMessageVoidify() { }
|
||||
// This has to be an operator with a precedence lower than << but
|
||||
// higher than ?:
|
||||
void operator&(std::ostream&) { }
|
||||
};
|
||||
|
||||
#if defined(WEBRTC_RESTRICT_LOGGING)
|
||||
// This should compile away logs matching the following condition.
|
||||
#define RESTRICT_LOGGING_PRECONDITION(sev) \
|
||||
sev < webrtc::LS_INFO ? (void) 0 :
|
||||
#else
|
||||
#define RESTRICT_LOGGING_PRECONDITION(sev)
|
||||
#endif
|
||||
|
||||
#define LOG_SEVERITY_PRECONDITION(sev) \
|
||||
RESTRICT_LOGGING_PRECONDITION(sev) !(webrtc::LogMessage::Loggable(sev)) \
|
||||
? (void) 0 \
|
||||
: webrtc::LogMessageVoidify() &
|
||||
|
||||
#define LOG(sev) \
|
||||
LOG_SEVERITY_PRECONDITION(webrtc::sev) \
|
||||
webrtc::LogMessage(__FILE__, __LINE__, webrtc::sev).stream()
|
||||
|
||||
// The _V version is for when a variable is passed in. It doesn't do the
|
||||
// namespace concatination.
|
||||
#define LOG_V(sev) \
|
||||
LOG_SEVERITY_PRECONDITION(sev) \
|
||||
webrtc::LogMessage(__FILE__, __LINE__, sev).stream()
|
||||
|
||||
// The _F version prefixes the message with the current function name.
|
||||
#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
|
||||
#define LOG_F(sev) LOG(sev) << __PRETTY_FUNCTION__ << ": "
|
||||
#else
|
||||
#define LOG_F(sev) LOG(sev) << __FUNCTION__ << ": "
|
||||
#endif
|
||||
|
||||
#define LOG_API0() LOG_F(LS_VERBOSE)
|
||||
#define LOG_API1(v1) LOG_API0() << #v1 << "=" << v1
|
||||
#define LOG_API2(v1, v2) LOG_API1(v1) \
|
||||
<< ", " << #v2 << "=" << v2
|
||||
#define LOG_API3(v1, v2, v3) LOG_API2(v1, v2) \
|
||||
<< ", " << #v3 << "=" << v3
|
||||
|
||||
#define LOG_FERR0(sev, func) LOG(sev) << #func << " failed"
|
||||
#define LOG_FERR1(sev, func, v1) LOG_FERR0(sev, func) \
|
||||
<< ": " << #v1 << "=" << v1
|
||||
#define LOG_FERR2(sev, func, v1, v2) LOG_FERR1(sev, func, v1) \
|
||||
<< ", " << #v2 << "=" << v2
|
||||
#define LOG_FERR3(sev, func, v1, v2, v3) LOG_FERR2(sev, func, v1, v2) \
|
||||
<< ", " << #v3 << "=" << v3
|
||||
#define LOG_FERR4(sev, func, v1, v2, v3, v4) LOG_FERR3(sev, func, v1, v2, v3) \
|
||||
<< ", " << #v4 << "=" << v4
|
||||
|
||||
#endif // LOG
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_
|
139
webrtc/system_wrappers/include/metrics.h
Normal file
139
webrtc/system_wrappers/include/metrics.h
Normal file
@ -0,0 +1,139 @@
|
||||
//
|
||||
// Copyright (c) 2014 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_INCLUDE_METRICS_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "webrtc/common_types.h"
|
||||
|
||||
// Macros for allowing WebRTC clients (e.g. Chrome) to gather and aggregate
|
||||
// statistics.
|
||||
//
|
||||
// Histogram for counters.
|
||||
// RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count);
|
||||
//
|
||||
// Histogram for enumerators.
|
||||
// The boundary should be above the max enumerator sample.
|
||||
// RTC_HISTOGRAM_ENUMERATION(name, sample, boundary);
|
||||
//
|
||||
//
|
||||
// The macros use the methods HistogramFactoryGetCounts,
|
||||
// HistogramFactoryGetEnumeration and HistogramAdd.
|
||||
//
|
||||
// Therefore, WebRTC clients must either:
|
||||
//
|
||||
// - provide implementations of
|
||||
// Histogram* webrtc::metrics::HistogramFactoryGetCounts(
|
||||
// const std::string& name, int sample, int min, int max,
|
||||
// int bucket_count);
|
||||
// Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
|
||||
// const std::string& name, int sample, int boundary);
|
||||
// void webrtc::metrics::HistogramAdd(
|
||||
// Histogram* histogram_pointer, const std::string& name, int sample);
|
||||
//
|
||||
// - or link with the default implementations (i.e.
|
||||
// system_wrappers/system_wrappers.gyp:metrics_default).
|
||||
//
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// RTC_HISTOGRAM_COUNTS("WebRTC.Video.NacksSent", nacks_sent, 1, 100000, 100);
|
||||
//
|
||||
// enum Types {
|
||||
// kTypeX,
|
||||
// kTypeY,
|
||||
// kBoundary,
|
||||
// };
|
||||
//
|
||||
// RTC_HISTOGRAM_ENUMERATION("WebRTC.Types", kTypeX, kBoundary);
|
||||
|
||||
|
||||
// Macros for adding samples to a named histogram.
|
||||
//
|
||||
// NOTE: this is a temporary solution.
|
||||
// The aim is to mimic the behaviour in Chromium's src/base/metrics/histograms.h
|
||||
// However as atomics are not supported in webrtc, this is for now a modified
|
||||
// and temporary solution. Note that the histogram is constructed/found for
|
||||
// each call. Therefore, for now only use this implementation for metrics
|
||||
// that do not need to be updated frequently.
|
||||
// TODO(asapersson): Change implementation when atomics are supported.
|
||||
// Also consider changing string to const char* when switching to atomics.
|
||||
|
||||
// Histogram for counters.
|
||||
#define RTC_HISTOGRAM_COUNTS_100(name, sample) RTC_HISTOGRAM_COUNTS( \
|
||||
name, sample, 1, 100, 50)
|
||||
|
||||
#define RTC_HISTOGRAM_COUNTS_200(name, sample) RTC_HISTOGRAM_COUNTS( \
|
||||
name, sample, 1, 200, 50)
|
||||
|
||||
#define RTC_HISTOGRAM_COUNTS_1000(name, sample) RTC_HISTOGRAM_COUNTS( \
|
||||
name, sample, 1, 1000, 50)
|
||||
|
||||
#define RTC_HISTOGRAM_COUNTS_10000(name, sample) RTC_HISTOGRAM_COUNTS( \
|
||||
name, sample, 1, 10000, 50)
|
||||
|
||||
#define RTC_HISTOGRAM_COUNTS_100000(name, sample) RTC_HISTOGRAM_COUNTS( \
|
||||
name, sample, 1, 100000, 50)
|
||||
|
||||
#define RTC_HISTOGRAM_COUNTS(name, sample, min, max, bucket_count) \
|
||||
RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
|
||||
webrtc::metrics::HistogramFactoryGetCounts( \
|
||||
name, min, max, bucket_count))
|
||||
|
||||
// Histogram for percentage.
|
||||
#define RTC_HISTOGRAM_PERCENTAGE(name, sample) \
|
||||
RTC_HISTOGRAM_ENUMERATION(name, sample, 101)
|
||||
|
||||
// Histogram for enumerators.
|
||||
// |boundary| should be above the max enumerator sample.
|
||||
#define RTC_HISTOGRAM_ENUMERATION(name, sample, boundary) \
|
||||
RTC_HISTOGRAM_COMMON_BLOCK(name, sample, \
|
||||
webrtc::metrics::HistogramFactoryGetEnumeration(name, boundary))
|
||||
|
||||
#define RTC_HISTOGRAM_COMMON_BLOCK(constant_name, sample, \
|
||||
factory_get_invocation) \
|
||||
do { \
|
||||
webrtc::metrics::Histogram* histogram_pointer = factory_get_invocation; \
|
||||
webrtc::metrics::HistogramAdd(histogram_pointer, constant_name, sample); \
|
||||
} while (0)
|
||||
|
||||
|
||||
namespace webrtc {
|
||||
namespace metrics {
|
||||
|
||||
// Time that should have elapsed for stats that are gathered once per call.
|
||||
enum { kMinRunTimeInSeconds = 10 };
|
||||
|
||||
class Histogram;
|
||||
|
||||
// Functions for getting pointer to histogram (constructs or finds the named
|
||||
// histogram).
|
||||
|
||||
// Get histogram for counters.
|
||||
Histogram* HistogramFactoryGetCounts(
|
||||
const std::string& name, int min, int max, int bucket_count);
|
||||
|
||||
// Get histogram for enumerators.
|
||||
// |boundary| should be above the max enumerator sample.
|
||||
Histogram* HistogramFactoryGetEnumeration(
|
||||
const std::string& name, int boundary);
|
||||
|
||||
// Function for adding a |sample| to a histogram.
|
||||
// |name| can be used to verify that it matches the histogram name.
|
||||
void HistogramAdd(
|
||||
Histogram* histogram_pointer, const std::string& name, int sample);
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
|
||||
|
68
webrtc/system_wrappers/include/rw_lock_wrapper.h
Normal file
68
webrtc/system_wrappers/include/rw_lock_wrapper.h
Normal file
@ -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_INCLUDE_RW_LOCK_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_RW_LOCK_WRAPPER_H_
|
||||
|
||||
#include "webrtc/base/thread_annotations.h"
|
||||
|
||||
// Note, Windows pre-Vista version of RW locks are not supported natively. For
|
||||
// these OSs regular critical sections have been used to approximate RW lock
|
||||
// functionality and will therefore have worse performance.
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class LOCKABLE RWLockWrapper {
|
||||
public:
|
||||
static RWLockWrapper* CreateRWLock();
|
||||
virtual ~RWLockWrapper() {}
|
||||
|
||||
virtual void AcquireLockExclusive() EXCLUSIVE_LOCK_FUNCTION() = 0;
|
||||
virtual void ReleaseLockExclusive() UNLOCK_FUNCTION() = 0;
|
||||
|
||||
virtual void AcquireLockShared() SHARED_LOCK_FUNCTION() = 0;
|
||||
virtual void ReleaseLockShared() UNLOCK_FUNCTION() = 0;
|
||||
};
|
||||
|
||||
// RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
|
||||
// provides more compact locking syntax.
|
||||
class SCOPED_LOCKABLE ReadLockScoped {
|
||||
public:
|
||||
ReadLockScoped(RWLockWrapper& rw_lock) SHARED_LOCK_FUNCTION(rw_lock)
|
||||
: rw_lock_(rw_lock) {
|
||||
rw_lock_.AcquireLockShared();
|
||||
}
|
||||
|
||||
~ReadLockScoped() UNLOCK_FUNCTION() {
|
||||
rw_lock_.ReleaseLockShared();
|
||||
}
|
||||
|
||||
private:
|
||||
RWLockWrapper& rw_lock_;
|
||||
};
|
||||
|
||||
class SCOPED_LOCKABLE WriteLockScoped {
|
||||
public:
|
||||
WriteLockScoped(RWLockWrapper& rw_lock) EXCLUSIVE_LOCK_FUNCTION(rw_lock)
|
||||
: rw_lock_(rw_lock) {
|
||||
rw_lock_.AcquireLockExclusive();
|
||||
}
|
||||
|
||||
~WriteLockScoped() UNLOCK_FUNCTION() {
|
||||
rw_lock_.ReleaseLockExclusive();
|
||||
}
|
||||
|
||||
private:
|
||||
RWLockWrapper& rw_lock_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_RW_LOCK_WRAPPER_H_
|
157
webrtc/system_wrappers/include/scoped_vector.h
Normal file
157
webrtc/system_wrappers/include/scoped_vector.h
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2014 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.
|
||||
*/
|
||||
|
||||
// Borrowed from Chromium's src/base/memory/scoped_vector.h.
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/system_wrappers/include/stl_util.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// ScopedVector wraps a vector deleting the elements from its
|
||||
// destructor.
|
||||
template <class T>
|
||||
class ScopedVector {
|
||||
public:
|
||||
typedef typename std::vector<T*>::allocator_type allocator_type;
|
||||
typedef typename std::vector<T*>::size_type size_type;
|
||||
typedef typename std::vector<T*>::difference_type difference_type;
|
||||
typedef typename std::vector<T*>::pointer pointer;
|
||||
typedef typename std::vector<T*>::const_pointer const_pointer;
|
||||
typedef typename std::vector<T*>::reference reference;
|
||||
typedef typename std::vector<T*>::const_reference const_reference;
|
||||
typedef typename std::vector<T*>::value_type value_type;
|
||||
typedef typename std::vector<T*>::iterator iterator;
|
||||
typedef typename std::vector<T*>::const_iterator const_iterator;
|
||||
typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
|
||||
typedef typename std::vector<T*>::const_reverse_iterator
|
||||
const_reverse_iterator;
|
||||
|
||||
ScopedVector() {}
|
||||
~ScopedVector() { clear(); }
|
||||
|
||||
// Move construction and assignment.
|
||||
ScopedVector(ScopedVector&& other) {
|
||||
*this = static_cast<ScopedVector&&>(other);
|
||||
}
|
||||
ScopedVector& operator=(ScopedVector&& other) {
|
||||
std::swap(v_, other.v_); // The arguments are std::vectors, so std::swap
|
||||
// is the one that we want.
|
||||
other.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Deleted copy constructor and copy assignment, to make the type move-only.
|
||||
ScopedVector(const ScopedVector& other) = delete;
|
||||
ScopedVector& operator=(const ScopedVector& other) = delete;
|
||||
|
||||
// Get an rvalue reference. (sv.Pass() does the same thing as std::move(sv).)
|
||||
ScopedVector&& Pass() { return static_cast<ScopedVector&&>(*this); }
|
||||
|
||||
reference operator[](size_t index) { return v_[index]; }
|
||||
const_reference operator[](size_t index) const { return v_[index]; }
|
||||
|
||||
bool empty() const { return v_.empty(); }
|
||||
size_t size() const { return v_.size(); }
|
||||
|
||||
reverse_iterator rbegin() { return v_.rbegin(); }
|
||||
const_reverse_iterator rbegin() const { return v_.rbegin(); }
|
||||
reverse_iterator rend() { return v_.rend(); }
|
||||
const_reverse_iterator rend() const { return v_.rend(); }
|
||||
|
||||
iterator begin() { return v_.begin(); }
|
||||
const_iterator begin() const { return v_.begin(); }
|
||||
iterator end() { return v_.end(); }
|
||||
const_iterator end() const { return v_.end(); }
|
||||
|
||||
const_reference front() const { return v_.front(); }
|
||||
reference front() { return v_.front(); }
|
||||
const_reference back() const { return v_.back(); }
|
||||
reference back() { return v_.back(); }
|
||||
|
||||
void push_back(T* elem) { v_.push_back(elem); }
|
||||
|
||||
void pop_back() {
|
||||
RTC_DCHECK(!empty());
|
||||
delete v_.back();
|
||||
v_.pop_back();
|
||||
}
|
||||
|
||||
std::vector<T*>& get() { return v_; }
|
||||
const std::vector<T*>& get() const { return v_; }
|
||||
void swap(std::vector<T*>& other) { v_.swap(other); }
|
||||
void swap(ScopedVector<T>& other) { v_.swap(other.v_); }
|
||||
void release(std::vector<T*>* out) {
|
||||
out->swap(v_);
|
||||
v_.clear();
|
||||
}
|
||||
|
||||
void reserve(size_t capacity) { v_.reserve(capacity); }
|
||||
|
||||
// Resize, deleting elements in the disappearing range if we are shrinking.
|
||||
void resize(size_t new_size) {
|
||||
if (v_.size() > new_size)
|
||||
STLDeleteContainerPointers(v_.begin() + new_size, v_.end());
|
||||
v_.resize(new_size);
|
||||
}
|
||||
|
||||
template<typename InputIterator>
|
||||
void assign(InputIterator begin, InputIterator end) {
|
||||
v_.assign(begin, end);
|
||||
}
|
||||
|
||||
void clear() { STLDeleteElements(&v_); }
|
||||
|
||||
// Like |clear()|, but doesn't delete any elements.
|
||||
void weak_clear() { v_.clear(); }
|
||||
|
||||
// Lets the ScopedVector take ownership of |x|.
|
||||
iterator insert(iterator position, T* x) {
|
||||
return v_.insert(position, x);
|
||||
}
|
||||
|
||||
// Lets the ScopedVector take ownership of elements in [first,last).
|
||||
template<typename InputIterator>
|
||||
void insert(iterator position, InputIterator first, InputIterator last) {
|
||||
v_.insert(position, first, last);
|
||||
}
|
||||
|
||||
iterator erase(iterator position) {
|
||||
delete *position;
|
||||
return v_.erase(position);
|
||||
}
|
||||
|
||||
iterator erase(iterator first, iterator last) {
|
||||
STLDeleteContainerPointers(first, last);
|
||||
return v_.erase(first, last);
|
||||
}
|
||||
|
||||
// Like |erase()|, but doesn't delete the element at |position|.
|
||||
iterator weak_erase(iterator position) {
|
||||
return v_.erase(position);
|
||||
}
|
||||
|
||||
// Like |erase()|, but doesn't delete the elements in [first, last).
|
||||
iterator weak_erase(iterator first, iterator last) {
|
||||
return v_.erase(first, last);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<T*> v_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SCOPED_VECTOR_H_
|
24
webrtc/system_wrappers/include/sleep.h
Normal file
24
webrtc/system_wrappers/include/sleep.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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.
|
||||
*/
|
||||
// An OS-independent sleep function.
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SLEEP_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SLEEP_H_
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// This function sleeps for the specified number of milliseconds.
|
||||
// It may return early if the thread is woken by some other event,
|
||||
// such as the delivery of a signal on Unix.
|
||||
void SleepMs(int msecs);
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SLEEP_H_
|
153
webrtc/system_wrappers/include/static_instance.h
Normal file
153
webrtc/system_wrappers/include/static_instance.h
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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_INCLUDE_STATIC_INSTANCE_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_STATIC_INSTANCE_H_
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
|
||||
#ifdef _WIN32
|
||||
#include "webrtc/system_wrappers/include/fix_interlocked_exchange_pointer_win.h"
|
||||
#endif
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
enum CountOperation {
|
||||
kRelease,
|
||||
kAddRef,
|
||||
kAddRefNoCreate
|
||||
};
|
||||
enum CreateOperation {
|
||||
kInstanceExists,
|
||||
kCreate,
|
||||
kDestroy
|
||||
};
|
||||
|
||||
template <class T>
|
||||
// Construct On First Use idiom. Avoids
|
||||
// "static initialization order fiasco".
|
||||
static T* GetStaticInstance(CountOperation count_operation) {
|
||||
// TODO (hellner): use atomic wrapper instead.
|
||||
static volatile long instance_count = 0;
|
||||
static T* volatile instance = NULL;
|
||||
CreateOperation state = kInstanceExists;
|
||||
#ifndef _WIN32
|
||||
// This memory is staticly allocated once. The application does not try to
|
||||
// free this memory. This approach is taken to avoid issues with
|
||||
// destruction order for statically allocated memory. The memory will be
|
||||
// reclaimed by the OS and memory leak tools will not recognize memory
|
||||
// reachable from statics leaked so no noise is added by doing this.
|
||||
static CriticalSectionWrapper* crit_sect(
|
||||
CriticalSectionWrapper::CreateCriticalSection());
|
||||
CriticalSectionScoped lock(crit_sect);
|
||||
|
||||
if (count_operation ==
|
||||
kAddRefNoCreate && instance_count == 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (count_operation ==
|
||||
kAddRef ||
|
||||
count_operation == kAddRefNoCreate) {
|
||||
instance_count++;
|
||||
if (instance_count == 1) {
|
||||
state = kCreate;
|
||||
}
|
||||
} else {
|
||||
instance_count--;
|
||||
if (instance_count == 0) {
|
||||
state = kDestroy;
|
||||
}
|
||||
}
|
||||
if (state == kCreate) {
|
||||
instance = T::CreateInstance();
|
||||
} else if (state == kDestroy) {
|
||||
T* old_instance = instance;
|
||||
instance = NULL;
|
||||
// The state will not change past this point. Release the critical
|
||||
// section while deleting the object in case it would be blocking on
|
||||
// access back to this object. (This is the case for the tracing class
|
||||
// since the thread owned by the tracing class also traces).
|
||||
// TODO(hellner): this is a bit out of place but here goes, de-couple
|
||||
// thread implementation with trace implementation.
|
||||
crit_sect->Leave();
|
||||
if (old_instance) {
|
||||
delete old_instance;
|
||||
}
|
||||
// Re-acquire the lock since the scoped critical section will release
|
||||
// it.
|
||||
crit_sect->Enter();
|
||||
return NULL;
|
||||
}
|
||||
#else // _WIN32
|
||||
if (count_operation ==
|
||||
kAddRefNoCreate && instance_count == 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (count_operation == kAddRefNoCreate) {
|
||||
if (1 == InterlockedIncrement(&instance_count)) {
|
||||
// The instance has been destroyed by some other thread. Rollback.
|
||||
InterlockedDecrement(&instance_count);
|
||||
assert(false);
|
||||
return NULL;
|
||||
}
|
||||
// Sanity to catch corrupt state.
|
||||
if (instance == NULL) {
|
||||
assert(false);
|
||||
InterlockedDecrement(&instance_count);
|
||||
return NULL;
|
||||
}
|
||||
} else if (count_operation == kAddRef) {
|
||||
if (instance_count == 0) {
|
||||
state = kCreate;
|
||||
} else {
|
||||
if (1 == InterlockedIncrement(&instance_count)) {
|
||||
// InterlockedDecrement because reference count should not be
|
||||
// updated just yet (that's done when the instance is created).
|
||||
InterlockedDecrement(&instance_count);
|
||||
state = kCreate;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int new_value = InterlockedDecrement(&instance_count);
|
||||
if (new_value == 0) {
|
||||
state = kDestroy;
|
||||
}
|
||||
}
|
||||
|
||||
if (state == kCreate) {
|
||||
// Create instance and let whichever thread finishes first assign its
|
||||
// local copy to the global instance. All other threads reclaim their
|
||||
// local copy.
|
||||
T* new_instance = T::CreateInstance();
|
||||
if (1 == InterlockedIncrement(&instance_count)) {
|
||||
InterlockedExchangePointer(reinterpret_cast<void * volatile*>(&instance),
|
||||
new_instance);
|
||||
} else {
|
||||
InterlockedDecrement(&instance_count);
|
||||
if (new_instance) {
|
||||
delete static_cast<T*>(new_instance);
|
||||
}
|
||||
}
|
||||
} else if (state == kDestroy) {
|
||||
T* old_value = static_cast<T*>(InterlockedExchangePointer(
|
||||
reinterpret_cast<void * volatile*>(&instance), NULL));
|
||||
if (old_value) {
|
||||
delete static_cast<T*>(old_value);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif // #ifndef _WIN32
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namspace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_STATIC_INSTANCE_H_
|
265
webrtc/system_wrappers/include/stl_util.h
Normal file
265
webrtc/system_wrappers/include/stl_util.h
Normal file
@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Copyright (c) 2014 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.
|
||||
*/
|
||||
|
||||
// Borrowed from Chromium's src/base/stl_util.h.
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_STL_UTIL_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_STL_UTIL_H_
|
||||
|
||||
#include <assert.h>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Clears internal memory of an STL object.
|
||||
// STL clear()/reserve(0) does not always free internal memory allocated
|
||||
// This function uses swap/destructor to ensure the internal memory is freed.
|
||||
template<class T>
|
||||
void STLClearObject(T* obj) {
|
||||
T tmp;
|
||||
tmp.swap(*obj);
|
||||
// Sometimes "T tmp" allocates objects with memory (arena implementation?).
|
||||
// Hence using additional reserve(0) even if it doesn't always work.
|
||||
obj->reserve(0);
|
||||
}
|
||||
|
||||
// For a range within a container of pointers, calls delete (non-array version)
|
||||
// on these pointers.
|
||||
// NOTE: for these three functions, we could just implement a DeleteObject
|
||||
// functor and then call for_each() on the range and functor, but this
|
||||
// requires us to pull in all of algorithm.h, which seems expensive.
|
||||
// For hash_[multi]set, it is important that this deletes behind the iterator
|
||||
// because the hash_set may call the hash function on the iterator when it is
|
||||
// advanced, which could result in the hash function trying to deference a
|
||||
// stale pointer.
|
||||
template <class ForwardIterator>
|
||||
void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {
|
||||
while (begin != end) {
|
||||
ForwardIterator temp = begin;
|
||||
++begin;
|
||||
delete *temp;
|
||||
}
|
||||
}
|
||||
|
||||
// For a range within a container of pairs, calls delete (non-array version) on
|
||||
// BOTH items in the pairs.
|
||||
// NOTE: Like STLDeleteContainerPointers, it is important that this deletes
|
||||
// behind the iterator because if both the key and value are deleted, the
|
||||
// container may call the hash function on the iterator when it is advanced,
|
||||
// which could result in the hash function trying to dereference a stale
|
||||
// pointer.
|
||||
template <class ForwardIterator>
|
||||
void STLDeleteContainerPairPointers(ForwardIterator begin,
|
||||
ForwardIterator end) {
|
||||
while (begin != end) {
|
||||
ForwardIterator temp = begin;
|
||||
++begin;
|
||||
delete temp->first;
|
||||
delete temp->second;
|
||||
}
|
||||
}
|
||||
|
||||
// For a range within a container of pairs, calls delete (non-array version) on
|
||||
// the FIRST item in the pairs.
|
||||
// NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
|
||||
template <class ForwardIterator>
|
||||
void STLDeleteContainerPairFirstPointers(ForwardIterator begin,
|
||||
ForwardIterator end) {
|
||||
while (begin != end) {
|
||||
ForwardIterator temp = begin;
|
||||
++begin;
|
||||
delete temp->first;
|
||||
}
|
||||
}
|
||||
|
||||
// For a range within a container of pairs, calls delete.
|
||||
// NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
|
||||
// Deleting the value does not always invalidate the iterator, but it may
|
||||
// do so if the key is a pointer into the value object.
|
||||
template <class ForwardIterator>
|
||||
void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
|
||||
ForwardIterator end) {
|
||||
while (begin != end) {
|
||||
ForwardIterator temp = begin;
|
||||
++begin;
|
||||
delete temp->second;
|
||||
}
|
||||
}
|
||||
|
||||
// To treat a possibly-empty vector as an array, use these functions.
|
||||
// If you know the array will never be empty, you can use &*v.begin()
|
||||
// directly, but that is undefined behaviour if |v| is empty.
|
||||
template<typename T>
|
||||
inline T* vector_as_array(std::vector<T>* v) {
|
||||
return v->empty() ? NULL : &*v->begin();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline const T* vector_as_array(const std::vector<T>* v) {
|
||||
return v->empty() ? NULL : &*v->begin();
|
||||
}
|
||||
|
||||
// Return a mutable char* pointing to a string's internal buffer,
|
||||
// which may not be null-terminated. Writing through this pointer will
|
||||
// modify the string.
|
||||
//
|
||||
// string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
|
||||
// next call to a string method that invalidates iterators.
|
||||
//
|
||||
// As of 2006-04, there is no standard-blessed way of getting a
|
||||
// mutable reference to a string's internal buffer. However, issue 530
|
||||
// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
|
||||
// proposes this as the method. According to Matt Austern, this should
|
||||
// already work on all current implementations.
|
||||
inline char* string_as_array(std::string* str) {
|
||||
// DO NOT USE const_cast<char*>(str->data())
|
||||
return str->empty() ? NULL : &*str->begin();
|
||||
}
|
||||
|
||||
// The following functions are useful for cleaning up STL containers whose
|
||||
// elements point to allocated memory.
|
||||
|
||||
// STLDeleteElements() deletes all the elements in an STL container and clears
|
||||
// the container. This function is suitable for use with a vector, set,
|
||||
// hash_set, or any other STL container which defines sensible begin(), end(),
|
||||
// and clear() methods.
|
||||
//
|
||||
// If container is NULL, this function is a no-op.
|
||||
//
|
||||
// As an alternative to calling STLDeleteElements() directly, consider
|
||||
// STLElementDeleter (defined below), which ensures that your container's
|
||||
// elements are deleted when the STLElementDeleter goes out of scope.
|
||||
template <class T>
|
||||
void STLDeleteElements(T* container) {
|
||||
if (!container)
|
||||
return;
|
||||
STLDeleteContainerPointers(container->begin(), container->end());
|
||||
container->clear();
|
||||
}
|
||||
|
||||
// Given an STL container consisting of (key, value) pairs, STLDeleteValues
|
||||
// deletes all the "value" components and clears the container. Does nothing
|
||||
// in the case it's given a NULL pointer.
|
||||
template <class T>
|
||||
void STLDeleteValues(T* container) {
|
||||
if (!container)
|
||||
return;
|
||||
for (typename T::iterator i(container->begin()); i != container->end(); ++i)
|
||||
delete i->second;
|
||||
container->clear();
|
||||
}
|
||||
|
||||
|
||||
// The following classes provide a convenient way to delete all elements or
|
||||
// values from STL containers when they goes out of scope. This greatly
|
||||
// simplifies code that creates temporary objects and has multiple return
|
||||
// statements. Example:
|
||||
//
|
||||
// vector<MyProto *> tmp_proto;
|
||||
// STLElementDeleter<vector<MyProto *> > d(&tmp_proto);
|
||||
// if (...) return false;
|
||||
// ...
|
||||
// return success;
|
||||
|
||||
// Given a pointer to an STL container this class will delete all the element
|
||||
// pointers when it goes out of scope.
|
||||
template<class T>
|
||||
class STLElementDeleter {
|
||||
public:
|
||||
STLElementDeleter<T>(T* container) : container_(container) {}
|
||||
~STLElementDeleter<T>() { STLDeleteElements(container_); }
|
||||
|
||||
private:
|
||||
T* container_;
|
||||
};
|
||||
|
||||
// Given a pointer to an STL container this class will delete all the value
|
||||
// pointers when it goes out of scope.
|
||||
template<class T>
|
||||
class STLValueDeleter {
|
||||
public:
|
||||
STLValueDeleter<T>(T* container) : container_(container) {}
|
||||
~STLValueDeleter<T>() { STLDeleteValues(container_); }
|
||||
|
||||
private:
|
||||
T* container_;
|
||||
};
|
||||
|
||||
// Test to see if a set, map, hash_set or hash_map contains a particular key.
|
||||
// Returns true if the key is in the collection.
|
||||
template <typename Collection, typename Key>
|
||||
bool ContainsKey(const Collection& collection, const Key& key) {
|
||||
return collection.find(key) != collection.end();
|
||||
}
|
||||
|
||||
// Returns true if the container is sorted.
|
||||
template <typename Container>
|
||||
bool STLIsSorted(const Container& cont) {
|
||||
// Note: Use reverse iterator on container to ensure we only require
|
||||
// value_type to implement operator<.
|
||||
return std::adjacent_find(cont.rbegin(), cont.rend(),
|
||||
std::less<typename Container::value_type>())
|
||||
== cont.rend();
|
||||
}
|
||||
|
||||
// Returns a new ResultType containing the difference of two sorted containers.
|
||||
template <typename ResultType, typename Arg1, typename Arg2>
|
||||
ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
|
||||
assert(STLIsSorted(a1));
|
||||
assert(STLIsSorted(a2));
|
||||
ResultType difference;
|
||||
std::set_difference(a1.begin(), a1.end(),
|
||||
a2.begin(), a2.end(),
|
||||
std::inserter(difference, difference.end()));
|
||||
return difference;
|
||||
}
|
||||
|
||||
// Returns a new ResultType containing the union of two sorted containers.
|
||||
template <typename ResultType, typename Arg1, typename Arg2>
|
||||
ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) {
|
||||
assert(STLIsSorted(a1));
|
||||
assert(STLIsSorted(a2));
|
||||
ResultType result;
|
||||
std::set_union(a1.begin(), a1.end(),
|
||||
a2.begin(), a2.end(),
|
||||
std::inserter(result, result.end()));
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns a new ResultType containing the intersection of two sorted
|
||||
// containers.
|
||||
template <typename ResultType, typename Arg1, typename Arg2>
|
||||
ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) {
|
||||
assert(STLIsSorted(a1));
|
||||
assert(STLIsSorted(a2));
|
||||
ResultType result;
|
||||
std::set_intersection(a1.begin(), a1.end(),
|
||||
a2.begin(), a2.end(),
|
||||
std::inserter(result, result.end()));
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns true if the sorted container |a1| contains all elements of the sorted
|
||||
// container |a2|.
|
||||
template <typename Arg1, typename Arg2>
|
||||
bool STLIncludes(const Arg1& a1, const Arg2& a2) {
|
||||
assert(STLIsSorted(a1));
|
||||
assert(STLIsSorted(a2));
|
||||
return std::includes(a1.begin(), a1.end(),
|
||||
a2.begin(), a2.end());
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_STL_UTIL_H_
|
95
webrtc/system_wrappers/include/thread_wrapper.h
Normal file
95
webrtc/system_wrappers/include/thread_wrapper.h
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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_INCLUDE_THREAD_WRAPPER_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_THREAD_WRAPPER_H_
|
||||
|
||||
#if defined(WEBRTC_WIN)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "webrtc/base/scoped_ptr.h"
|
||||
#include "webrtc/common_types.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Callback function that the spawned thread will enter once spawned.
|
||||
// A return value of false is interpreted as that the function has no
|
||||
// more work to do and that the thread can be released.
|
||||
typedef bool(*ThreadRunFunction)(void*);
|
||||
|
||||
enum ThreadPriority {
|
||||
#ifdef WEBRTC_WIN
|
||||
kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
|
||||
kNormalPriority = THREAD_PRIORITY_NORMAL,
|
||||
kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
|
||||
kHighestPriority = THREAD_PRIORITY_HIGHEST,
|
||||
kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
|
||||
#else
|
||||
kLowPriority = 1,
|
||||
kNormalPriority = 2,
|
||||
kHighPriority = 3,
|
||||
kHighestPriority = 4,
|
||||
kRealtimePriority = 5
|
||||
#endif
|
||||
};
|
||||
|
||||
// Represents a simple worker thread. The implementation must be assumed
|
||||
// to be single threaded, meaning that all methods of the class, must be
|
||||
// called from the same thread, including instantiation.
|
||||
// TODO(tommi): There's no need for this to be a virtual interface since there's
|
||||
// only ever a single implementation of it.
|
||||
class ThreadWrapper {
|
||||
public:
|
||||
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.
|
||||
// thread_name NULL terminated thread name, will be visable in the Windows
|
||||
// debugger.
|
||||
static rtc::scoped_ptr<ThreadWrapper> CreateThread(ThreadRunFunction func,
|
||||
void* obj, const char* thread_name);
|
||||
|
||||
// Get the current thread's thread ID.
|
||||
// NOTE: This is a static method. It returns the id of the calling thread,
|
||||
// *not* the id of the worker thread that a ThreadWrapper instance represents.
|
||||
// TODO(tommi): Move outside of the ThreadWrapper class to avoid confusion.
|
||||
static uint32_t GetThreadId();
|
||||
|
||||
// Tries to spawns a thread and returns true if that was successful.
|
||||
// Additionally, it tries to set thread priority according to the priority
|
||||
// from when CreateThread was called. However, failure to set priority will
|
||||
// not result in a false return value.
|
||||
virtual bool Start() = 0;
|
||||
|
||||
// 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;
|
||||
|
||||
// Set the priority of the worker thread. Must be called when thread
|
||||
// is running.
|
||||
virtual bool SetPriority(ThreadPriority priority) = 0;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_THREAD_WRAPPER_H_
|
92
webrtc/system_wrappers/include/trace.h
Normal file
92
webrtc/system_wrappers/include/trace.h
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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 independent wrapper for logging runtime information to file.
|
||||
* Note: All log messages will be written to the same trace file.
|
||||
* Note: If too many messages are written to file there will be a build up of
|
||||
* messages. Apply filtering to avoid that.
|
||||
*/
|
||||
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
|
||||
|
||||
#include "webrtc/common_types.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
#if defined(WEBRTC_RESTRICT_LOGGING)
|
||||
// Disable all TRACE macros. The LOG macro is still functional.
|
||||
#define WEBRTC_TRACE true ? (void) 0 : Trace::Add
|
||||
#else
|
||||
#define WEBRTC_TRACE Trace::Add
|
||||
#endif
|
||||
|
||||
class Trace {
|
||||
public:
|
||||
// The length of the trace text preceeding the log message.
|
||||
static const int kBoilerplateLength;
|
||||
// The position of the timestamp text within a trace.
|
||||
static const int kTimestampPosition;
|
||||
// The length of the timestamp (without "delta" field).
|
||||
static const int kTimestampLength;
|
||||
|
||||
// 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 void set_level_filter(int filter);
|
||||
|
||||
// Returns what type of messages are written to the trace file.
|
||||
static int level_filter();
|
||||
|
||||
// Sets the file name. If add_file_counter 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 int32_t SetTraceFile(const char* file_name,
|
||||
const bool add_file_counter = false);
|
||||
|
||||
// Returns the name of the file that the trace is currently writing to.
|
||||
static int32_t TraceFile(char file_name[1024]);
|
||||
|
||||
// Registers callback to receive trace messages.
|
||||
// TODO(hellner): Why not use OutStream instead? Why is TraceCallback not
|
||||
// defined in this file?
|
||||
static int32_t 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 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 coming.
|
||||
// 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 ellipsis 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 int32_t id,
|
||||
const char* msg, ...);
|
||||
|
||||
private:
|
||||
static volatile int level_filter_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_TRACE_H_
|
Reference in New Issue
Block a user