blob: e121f68377b1e525081ca3a13c8d048b48ac5322 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001/*
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
17// Interface for getting the current ThreadIdentity, creating one if necessary.
18// See thread_identity.h.
19//
20// This file is separate from thread_identity.h because creating a new
21// ThreadIdentity requires slightly higher level libraries (per_thread_sem
22// and low_level_alloc) than accessing an existing one. This separation allows
23// us to have a smaller //absl/base:base.
24
25#ifndef ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_
26#define ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_
27
28#include "absl/base/internal/thread_identity.h"
29#include "absl/base/port.h"
30
31namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080032ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070033namespace synchronization_internal {
34
35// Allocates and attaches a ThreadIdentity object for the calling thread.
36// For private use only.
37base_internal::ThreadIdentity* CreateThreadIdentity();
38
Austin Schuhb4691e92020-12-31 12:37:18 -080039// A per-thread destructor for reclaiming associated ThreadIdentity objects.
40// For private use only.
41void ReclaimThreadIdentity(void* v);
42
Austin Schuh36244a12019-09-21 17:52:38 -070043// Returns the ThreadIdentity object representing the calling thread; guaranteed
44// to be unique for its lifetime. The returned object will remain valid for the
45// program's lifetime; although it may be re-assigned to a subsequent thread.
46// If one does not exist for the calling thread, allocate it now.
47inline base_internal::ThreadIdentity* GetOrCreateCurrentThreadIdentity() {
48 base_internal::ThreadIdentity* identity =
49 base_internal::CurrentThreadIdentityIfPresent();
50 if (ABSL_PREDICT_FALSE(identity == nullptr)) {
51 return CreateThreadIdentity();
52 }
53 return identity;
54}
55
56} // namespace synchronization_internal
Austin Schuhb4691e92020-12-31 12:37:18 -080057ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070058} // namespace absl
59
60#endif // ABSL_SYNCHRONIZATION_INTERNAL_CREATE_THREAD_IDENTITY_H_