Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::port Namespace Reference

Namespaces

 android
 

Classes

class  Barrier
 The Barrier class defines a multi-thread barrier that allows N threads to synchronize execution. More...
 
class  LogEntryWriter
 Abstract class which can be overridden to integrate Ion logging with other logging systems. More...
 
class  MemoryMappedFile
 Read-only in-memory view of an entire file on disk. More...
 
class  Mutex
 A Mutex is used to ensure that only one thread or process can access a block of code at one time. More...
 
class  Semaphore
 A Semaphore enables threads and process synchronization. More...
 
class  StackTrace
 StackTrace acquires a stack trace for the current thread (not suitable for calling in an interrupt handler) on construction and supports conversion of raw stack pointers to a string of symbolic function names. More...
 
class  Timer
 

Typedefs

typedef pthread_t ThreadId
 Defines a type that can be used to identify a thread. More...
 
typedef pthread_key_t ThreadLocalStorageKey
 Defines a type that is used to access thread-local storage. More...
 
typedef bool(* ThreadFuncPtr )()
 Thread types and constants. More...
 
typedef std::function< bool()> ThreadStdFunc
 

Enumerations

enum  LogSeverity {
  INFO,
  WARNING,
  ERROR,
  FATAL,
  DFATAL
}
 

Functions

bool IsDebuggerAttached ()
 Don't break on unsupported platforms. More...
 
void Break ()
 If a debugger is attached, this function interrupts the program execution and causes any attached debugger to break. More...
 
void BreakOrAbort ()
 Calls Break() if running in a debugger, abort() otherwise. More...
 
const std::string GetEnvironmentVariableValue (const std::string &name)
 Returns the value of the named environment variable. More...
 
void SetEnvironmentVariableValue (const std::string &name, const std::string &value)
 Sets the named environment variable to the passed value. More...
 
std::string GetCanonicalFilePath (const std::string &path)
 Returns a canonical version of a file path string. More...
 
std::string GetCurrentWorkingDirectory ()
 Returns a platform-dependent string that is the current working directory. More...
 
bool GetFileModificationTime (const std::string &path, std::chrono::system_clock::time_point *time)
 Reads the last modification time of the passed file path into time and returns true, iff the file exists. More...
 
std::string GetTemporaryDirectory ()
 Returns a platform-dependent string that names the temporary directory. More...
 
std::string GetTemporaryFilename ()
 Returns a platform-dependent string that names a valid filename which may be opened for reading or writing. More...
 
FILE * OpenFile (const std::string &path, const std::string &mode)
 Opens the file at path and returns a FILE pointer suitable for passing to fread, fwrite, fclose, etc. More...
 
bool ReadDataFromFile (const std::string &path, std::string *out)
 Opens the file at path and read the contents of the file into a string. More...
 
bool RemoveFile (const std::string &path)
 Attempts to remove the file at path and returns whether the file was successfully removed. More...
 
std::vector< std::string > ListDirectory (const std::string &path)
 Returns the contents of path, non-recursively. More...
 
ION_API LogEntryWriterCreateDefaultLogEntryWriter ()
 Instantiate a new LogEntryWriter of the default type for the current platform... More...
 
uint64 GetProcessResidentMemorySize ()
 Return size of the current process in bytes. More...
 
uint64 GetSystemMemorySize ()
 Return the hardware RAM size in bytes. More...
 
ThreadId SpawnThread (const ThreadFuncPtr func_ptr)
 Platform-independent public functions. More...
 
ThreadId SpawnThreadStd (const ThreadStdFunc *func)
 
bool IsThreadNamingSupported ()
 Thread naming functions. More...
 
bool IsMainThread ()
 Returns true if the current thread is the main thread. More...
 
void SetMainThreadId (ThreadId id)
 Sets the given thread ID to be considered the main thread; IsMainThread() will return true only for this thread. More...
 
bool JoinThread (ThreadId id)
 Windows-specific public functions. More...
 
size_t GetMaxThreadNameLength ()
 Returns the maximum length of a thread name if restricted by the platform. More...
 
bool SetThreadName (const std::string &name)
 Sets the name of the current thread. More...
 
void YieldThread ()
 Causes the calling thread to relinquish the CPU if there are other threads waiting to execute. More...
 
ThreadId GetCurrentThreadId ()
 Thread ID functions. More...
 
ThreadLocalStorageKey CreateThreadLocalStorageKey ()
 Thread-local storage functions. More...
 
bool SetThreadLocalStorage (ThreadLocalStorageKey key, void *ptr)
 Associates ptr with the thread-local storage area indicated by key. More...
 
void * GetThreadLocalStorage (ThreadLocalStorageKey key)
 Returns the pointer to the thread-local storage area indicated by key. More...
 
bool DeleteThreadLocalStorageKey (ThreadLocalStorageKey key)
 Deletes a key returned by CreateThreadLocalStorageKey(). More...
 

Typedef Documentation

typedef bool(* ion::port::ThreadFuncPtr)()

Thread types and constants.

These types define a function that is called when a thread is spawned. They are supposed to return false if an error occurred. One is easiest to use if you have pointer to a simple function that does not take any arguments. The std::function version can be useful to package up a function that takes arbitrary arguments.

Definition at line 58 of file threadutils.h.

typedef pthread_t ion::port::ThreadId

Defines a type that can be used to identify a thread.

Definition at line 40 of file threadutils.h.

typedef pthread_key_t ion::port::ThreadLocalStorageKey

Defines a type that is used to access thread-local storage.

Definition at line 42 of file threadutils.h.

typedef std::function<bool()> ion::port::ThreadStdFunc

Definition at line 59 of file threadutils.h.

Enumeration Type Documentation

Enumerator
INFO 
WARNING 
ERROR 
FATAL 
DFATAL 

Definition at line 26 of file logging.h.

Function Documentation

ION_API void ion::port::Break ( )

If a debugger is attached, this function interrupts the program execution and causes any attached debugger to break.

It does so by raising SIGINT on Posix platforms and __debugbreak on Windows.

Currently this is supported for Linux, Mac, iOS, Android and Windows, but adding support for more operating systems is encouraged.

Definition at line 127 of file break.cc.

References IsDebuggerAttached().

Referenced by BreakOrAbort().

ION_API void ion::port::BreakOrAbort ( )

Calls Break() if running in a debugger, abort() otherwise.

The abort occurs on any platform.

Definition at line 141 of file break.cc.

References Break(), and IsDebuggerAttached().

Referenced by ion::base::RestoreDefaultBreakHandler().

ion::port::LogEntryWriter * ion::port::CreateDefaultLogEntryWriter ( )

Instantiate a new LogEntryWriter of the default type for the current platform...

don't call this repeatedly or you'll leak memory!

Definition at line 87 of file logging_android.cc.

Referenced by ion::base::GetDefaultLogEntryWriter().

ION_API ThreadLocalStorageKey ion::port::CreateThreadLocalStorageKey ( )

Thread-local storage functions.

Creates and returns a key that can be used to define thread-local storage areas. This returns kInvalidThreadLocalStorageKey if an error occurs.

Definition at line 367 of file threadutils.cc.

ION_API bool ion::port::DeleteThreadLocalStorageKey ( ThreadLocalStorageKey  key)

Deletes a key returned by CreateThreadLocalStorageKey().

Returns false on error.

Definition at line 387 of file threadutils.cc.

Referenced by ion::base::ThreadLocalObject< NamedTraceRecorderArray >::~ThreadLocalObject().

ION_API std::string ion::port::GetCanonicalFilePath ( const std::string &  path)

Returns a canonical version of a file path string.

Canonical form uses Unix-style separators ('/') on all platforms. Note that this changes paths only on platforms that don't use Unix-style separators.

Definition at line 40 of file fileutils.cc.

References length.

Referenced by GetCurrentWorkingDirectory(), GetTemporaryDirectory(), GetTemporaryFilename(), OpenFile(), and ion::base::ZipAssetManager::RegisterAssetData().

ION_API ThreadId ion::port::GetCurrentThreadId ( )

Thread ID functions.

Returns the ID of the currently-running thread.

Definition at line 363 of file threadutils.cc.

Referenced by IsMainThread(), ion::port::Mutex::Lock(), SetThreadName(), ion::port::Mutex::TryLock(), and ion::port::Mutex::Unlock().

ION_API std::string ion::port::GetCurrentWorkingDirectory ( )

Returns a platform-dependent string that is the current working directory.

Definition at line 57 of file fileutils.cc.

References GetCanonicalFilePath().

ION_API const std::string ion::port::GetEnvironmentVariableValue ( const std::string &  name)

Returns the value of the named environment variable.

Returns an empty string if the variable does not exist.

Definition at line 29 of file environment.cc.

Referenced by ion::base::EnvironmentSetting< T >::EnvironmentSetting().

ION_API bool ion::port::GetFileModificationTime ( const std::string &  path,
std::chrono::system_clock::time_point *  time 
)

Reads the last modification time of the passed file path into time and returns true, iff the file exists.

Otherwise returns false.

Definition at line 74 of file fileutils.cc.

References handle.

Referenced by ion::base::ZipAssetManager::RegisterAssetData(), and ion::base::ZipAssetManager::UpdateFileIfChanged().

ION_API size_t ion::port::GetMaxThreadNameLength ( )

Returns the maximum length of a thread name if restricted by the platform.

Otherwise, returns 0.

Definition at line 329 of file threadutils.cc.

Referenced by SetThreadName().

ION_API uint64 ion::port::GetProcessResidentMemorySize ( )

Return size of the current process in bytes.

Definition at line 78 of file memory.cc.

ION_API uint64 ion::port::GetSystemMemorySize ( )

Return the hardware RAM size in bytes.

Definition at line 98 of file memory.cc.

ION_API std::string ion::port::GetTemporaryDirectory ( )

Returns a platform-dependent string that names the temporary directory.

This is mostly useful for tests. If you need an actual file you can write, use GetTemporaryFilename.

Definition at line 136 of file fileutils.cc.

References GetCanonicalFilePath().

Referenced by GetTemporaryFilename().

ION_API std::string ion::port::GetTemporaryFilename ( )

Returns a platform-dependent string that names a valid filename which may be opened for reading or writing.

This creates an empty file, so make sure you call RemoveFile when you no longer need it.

Definition at line 156 of file fileutils.cc.

References GetCanonicalFilePath(), and GetTemporaryDirectory().

ION_API void * ion::port::GetThreadLocalStorage ( ThreadLocalStorageKey  key)

Returns the pointer to the thread-local storage area indicated by key.

Returns NULL on error or if no thread-local storage was set.

Definition at line 382 of file threadutils.cc.

Referenced by ion::base::ThreadLocalObject< NamedTraceRecorderArray >::Get(), and ion::profile::CallTraceManager::GetTraceRecorder().

ION_API bool ion::port::IsDebuggerAttached ( )

Don't break on unsupported platforms.

Returns whether a debugger is attached to this process.

Definition at line 121 of file break.cc.

Referenced by Break(), and BreakOrAbort().

ION_API bool ion::port::IsMainThread ( )

Returns true if the current thread is the main thread.

Definition at line 217 of file threadutils.cc.

References GetCurrentThreadId().

ION_API bool ion::port::IsThreadNamingSupported ( )

Thread naming functions.

Returns true if the platform supports named threads.

Todo:
(user): Remove this if LSB builds go away or support named threads.

Definition at line 213 of file threadutils.cc.

References THREAD_NAMING_SUPPORTED.

ION_API bool ion::port::JoinThread ( ThreadId  id)

Windows-specific public functions.

Waits for a thread to terminate.

pthreads-specific public functions. (All non-Windows platforms use pthreads.)

Returns immediately if the thread has already terminated.

Definition at line 322 of file threadutils.cc.

References pthread_join().

Referenced by ion::base::ThreadSpawner::Join().

ION_API std::vector< std::string > ion::port::ListDirectory ( const std::string &  path)

Returns the contents of path, non-recursively.

Only "." and ".." are excluded.

Definition at line 219 of file fileutils.cc.

ION_API FILE * ion::port::OpenFile ( const std::string &  path,
const std::string &  mode 
)

Opens the file at path and returns a FILE pointer suitable for passing to fread, fwrite, fclose, etc.

The mode parameter should have the same format as that passed to fopen ("w", "rb", etc.). Returns NULL if there is any error opening the file.

Definition at line 180 of file fileutils.cc.

References GetCanonicalFilePath().

Referenced by ReadDataFromFile(), ion::base::ZipAssetManager::SaveFileData(), and ion::base::ZipAssetManager::UpdateFileIfChanged().

ION_API bool ion::port::ReadDataFromFile ( const std::string &  path,
std::string *  out 
)

Opens the file at path and read the contents of the file into a string.

Returns false if there is any error opening the file.

Definition at line 191 of file fileutils.cc.

References length, and OpenFile().

ION_API bool ion::port::RemoveFile ( const std::string &  path)

Attempts to remove the file at path and returns whether the file was successfully removed.

Definition at line 209 of file fileutils.cc.

ION_API void ion::port::SetEnvironmentVariableValue ( const std::string &  name,
const std::string &  value 
)

Sets the named environment variable to the passed value.

Definition at line 34 of file environment.cc.

References value.

ION_API void ion::port::SetMainThreadId ( ThreadId  id)

Sets the given thread ID to be considered the main thread; IsMainThread() will return true only for this thread.

This is useful primarily for testing but can also be useful in situations where there is no persistent main thread, and one thread should be considered the main thread. This does nothing if id is kInvalidThreadId.

Definition at line 221 of file threadutils.cc.

ION_API bool ion::port::SetThreadLocalStorage ( ThreadLocalStorageKey  key,
void *  ptr 
)

Associates ptr with the thread-local storage area indicated by key.

Returns false on error.

Definition at line 374 of file threadutils.cc.

ION_API bool ion::port::SetThreadName ( const std::string &  name)

Sets the name of the current thread.

Does nothing but return false if IsThreadNamingSupported() returns false. The thread name will be truncated if GetMaxThreadNameLength() is non-zero and the length of the name exceeds it.

Definition at line 340 of file threadutils.cc.

References GetCurrentThreadId(), GetMaxThreadNameLength(), and name.

ION_API ThreadId ion::port::SpawnThread ( const ThreadFuncPtr  func_ptr)

Platform-independent public functions.

Thread lifetime functions.

Spawns a new named thread that invokes the given function. Returns kInvalidThreadId on error. The second version takes an std::function, passed by pointer because the caller is responsible for ensuring that it persists through the creation of the new thread.

Definition at line 187 of file threadutils.cc.

References id.

ION_API ThreadId ion::port::SpawnThreadStd ( const ThreadStdFunc *  func)

Definition at line 200 of file threadutils.cc.

References id.

ION_API void ion::port::YieldThread ( )

Causes the calling thread to relinquish the CPU if there are other threads waiting to execute.

Definition at line 359 of file threadutils.cc.

Referenced by ion::base::OnceFlag::CallOnce(), ion::base::SpinMutex::Lock(), and ion::port::Semaphore::TimedWaitMs().