blob: 119cf1f0e85b7482df3fe3e01da0f6c023e52831 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// This file includes routines to find out characteristics
16// of the machine a program is running on. It is undoubtedly
17// system-dependent.
18
19// Functions listed here that accept a pid_t as an argument act on the
20// current process if the pid_t argument is 0
21// All functions here are thread-hostile due to file caching unless
22// commented otherwise.
23
24#ifndef ABSL_BASE_INTERNAL_SYSINFO_H_
25#define ABSL_BASE_INTERNAL_SYSINFO_H_
26
27#ifndef _WIN32
28#include <sys/types.h>
Austin Schuh36244a12019-09-21 17:52:38 -070029#endif
30
Austin Schuhb4691e92020-12-31 12:37:18 -080031#include <cstdint>
32
33#include "absl/base/config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070034#include "absl/base/port.h"
35
36namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080037ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070038namespace base_internal {
39
40// Nominal core processor cycles per second of each processor. This is _not_
41// necessarily the frequency of the CycleClock counter (see cycleclock.h)
42// Thread-safe.
43double NominalCPUFrequency();
44
45// Number of logical processors (hyperthreads) in system. Thread-safe.
46int NumCPUs();
47
48// Return the thread id of the current thread, as told by the system.
49// No two currently-live threads implemented by the OS shall have the same ID.
50// Thread ids of exited threads may be reused. Multiple user-level threads
51// may have the same thread ID if multiplexed on the same OS thread.
52//
53// On Linux, you may send a signal to the resulting ID with kill(). However,
54// it is recommended for portability that you use pthread_kill() instead.
55#ifdef _WIN32
Austin Schuhb4691e92020-12-31 12:37:18 -080056// On Windows, process id and thread id are of the same type according to the
57// return types of GetProcessId() and GetThreadId() are both DWORD, an unsigned
58// 32-bit type.
59using pid_t = uint32_t;
Austin Schuh36244a12019-09-21 17:52:38 -070060#endif
61pid_t GetTID();
62
Austin Schuhb4691e92020-12-31 12:37:18 -080063// Like GetTID(), but caches the result in thread-local storage in order
64// to avoid unnecessary system calls. Note that there are some cases where
65// one must call through to GetTID directly, which is why this exists as a
66// separate function. For example, GetCachedTID() is not safe to call in
67// an asynchronous signal-handling context nor right after a call to fork().
68pid_t GetCachedTID();
69
Austin Schuh36244a12019-09-21 17:52:38 -070070} // namespace base_internal
Austin Schuhb4691e92020-12-31 12:37:18 -080071ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070072} // namespace absl
73
74#endif // ABSL_BASE_INTERNAL_SYSINFO_H_