ThreadName.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include <thread>
  19. #include <folly/Optional.h>
  20. #include <folly/Range.h>
  21. #include <folly/portability/Config.h>
  22. #include <folly/portability/PThread.h>
  23. namespace folly {
  24. /**
  25. * This returns true if the current platform supports setting the name of the
  26. * current thread.
  27. */
  28. bool canSetCurrentThreadName();
  29. /**
  30. * This returns true if the current platform supports setting the name of
  31. * threads other than the one currently executing.
  32. */
  33. bool canSetOtherThreadName();
  34. /**
  35. * Get the name of the given thread, or nothing if an error occurs
  36. * or the functionality is not available.
  37. */
  38. Optional<std::string> getThreadName(std::thread::id tid);
  39. /**
  40. * Equivalent to getThreadName(std::this_thread::get_id());
  41. */
  42. Optional<std::string> getCurrentThreadName();
  43. /**
  44. * Set the name of the given thread.
  45. * Returns false on failure, if an error occurs or the functionality
  46. * is not available.
  47. */
  48. bool setThreadName(std::thread::id tid, StringPiece name);
  49. bool setThreadName(pthread_t pid, StringPiece name);
  50. /**
  51. * Equivalent to setThreadName(std::this_thread::get_id(), name);
  52. */
  53. bool setThreadName(StringPiece name);
  54. } // namespace folly