create_thread_identity.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2017 The Abseil Authors.
  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. * https://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. // Interface for getting the current ThreadIdentity, creating one if necessary.
  17. // See thread_identity.h.
  18. //
  19. // This file is separate from thread_identity.h because creating a new
  20. // ThreadIdentity requires slightly higher level libraries (per_thread_sem
  21. // and low_level_alloc) than accessing an existing one. This separation allows
  22. // us to have a smaller //absl/base:base.
  23. #ifndef ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_
  24. #define ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_
  25. #include "absl/base/internal/thread_identity.h"
  26. #include "absl/base/port.h"
  27. namespace absl {
  28. ABSL_NAMESPACE_BEGIN
  29. namespace synchronization_internal {
  30. // Allocates and attaches a ThreadIdentity object for the calling thread.
  31. // For private use only.
  32. base_internal::ThreadIdentity* CreateThreadIdentity();
  33. // Returns the ThreadIdentity object representing the calling thread; guaranteed
  34. // to be unique for its lifetime. The returned object will remain valid for the
  35. // program's lifetime; although it may be re-assigned to a subsequent thread.
  36. // If one does not exist for the calling thread, allocate it now.
  37. inline base_internal::ThreadIdentity* GetOrCreateCurrentThreadIdentity() {
  38. base_internal::ThreadIdentity* identity =
  39. base_internal::CurrentThreadIdentityIfPresent();
  40. if (ABSL_PREDICT_FALSE(identity == nullptr)) {
  41. return CreateThreadIdentity();
  42. }
  43. return identity;
  44. }
  45. } // namespace synchronization_internal
  46. ABSL_NAMESPACE_END
  47. } // namespace absl
  48. #endif // ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_