LogCabin
|
00001 /* Copyright (c) 2011-2012 Stanford University 00002 * 00003 * Permission to use, copy, modify, and distribute this software for any 00004 * purpose with or without fee is hereby granted, provided that the above 00005 * copyright notice and this permission notice appear in all copies. 00006 * 00007 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES 00008 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00009 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR 00010 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00011 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00012 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00013 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00014 */ 00015 00016 #include <cinttypes> 00017 #include <string> 00018 00019 #ifndef LOGCABIN_CORE_THREADID_H 00020 #define LOGCABIN_CORE_THREADID_H 00021 00022 namespace LogCabin { 00023 namespace Core { 00024 00025 /** 00026 * Provides a convenient way to get identifiers for threads. 00027 * This is better than std::this_thread::get_id() in a few ways: 00028 * - It returns to you an integer, not some opaque type. 00029 * - The integer it returns is usually short, which is nice for log messages. 00030 * - It's probably faster, since it uses gcc's __thread keyword. 00031 */ 00032 namespace ThreadId { 00033 00034 /** 00035 * A thread ID that will never be assigned to any thread. 00036 */ 00037 const uint64_t NONE = 0; 00038 00039 /** 00040 * Returns a unique identifier for the current thread. 00041 */ 00042 uint64_t getId(); 00043 00044 /** 00045 * Set the friendly name for the current thread. 00046 * This can be later retrieved with getName(). 00047 * Calling setName with an empty string will reset the thread to its default 00048 * name. 00049 */ 00050 void setName(const std::string& name); 00051 00052 /** 00053 * Get the friendly name for the current thread. 00054 * This is useful in messages to users. 00055 * 00056 * You should arrange for setName() to be called when the thread is 00057 * created; otherwise you'll see an unhelpful name like "thread 3". 00058 */ 00059 std::string getName(); 00060 00061 } // namespace LogCabin::Core::ThreadId 00062 } // namespace LogCabin::Core 00063 } // namespace LogCabin 00064 00065 #endif // LOGCABIN_CORE_THREADID_H