Add AsyncSocketExceptionType for early data rejection.
[folly.git] / folly / ThreadName.h
index 2e028d7aa43443c98eb30ba2bf7bcf9b8ef0b5c4..ba7bde25953925e451fdc447af422643365edacd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #pragma once
 
+#include <string>
 #include <thread>
-#include <pthread.h>
+
+#include <folly/Optional.h>
 #include <folly/Range.h>
+#include <folly/portability/Config.h>
+#include <folly/portability/PThread.h>
 
 namespace folly {
 
-// This looks a bit weird, but it's necessary to avoid
-// having an undefined compiler function called.
-#if defined(__GLIBC__) && !defined(__APPLE__) && !defined(__ANDROID__)
-#if __GLIBC_PREREQ(2, 12)
-# define FOLLY_HAS_PTHREAD_SETNAME_NP
-#endif
-#endif
+/**
+ * This returns true if the current platform supports setting the name of the
+ * current thread.
+ */
+bool canSetCurrentThreadName();
 
-template <typename T>
-inline bool setThreadName(T /* id */, StringPiece /* name */) {
-  static_assert(
-      std::is_same<T, pthread_t>::value ||
-      std::is_same<T, std::thread::native_handle_type>::value,
-      "type must be pthread_t or std::thread::native_handle_type");
-  return false;
-}
+/**
+ * This returns true if the current platform supports setting the name of
+ * threads other than the one currently executing.
+ */
+bool canSetOtherThreadName();
 
-#ifdef FOLLY_HAS_PTHREAD_SETNAME_NP
-template <>
-inline bool setThreadName(pthread_t id, StringPiece name) {
-  return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
-}
-#endif
+/**
+ * Get the name of the given thread, or nothing if an error occurs
+ * or the functionality is not available.
+ */
+Optional<std::string> getThreadName(std::thread::id tid);
 
-inline bool setThreadName(StringPiece name) {
-  return setThreadName(pthread_self(), name);
-}
+/**
+ * Equivalent to getThreadName(std::this_thread::get_id());
+ */
+Optional<std::string> getCurrentThreadName();
 
+/**
+ * Set the name of the given thread.
+ * Returns false on failure, if an error occurs or the functionality
+ * is not available.
+ */
+bool setThreadName(std::thread::id tid, StringPiece name);
+#if FOLLY_HAVE_PTHREAD
+bool setThreadName(pthread_t pid, StringPiece name);
+#endif
+
+/**
+ * Equivalent to setThreadName(std::this_thread::get_id(), name);
+ */
+bool setThreadName(StringPiece name);
 }