MySQL  8.0.20
Source Code Documentation
os0thread.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2019, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 /** @file include/os0thread.h
28  The interface to the operating system
29  process and thread control primitives
30 
31  Created 9/8/1995 Heikki Tuuri
32  *******************************************************/
33 
34 #ifndef os0thread_h
35 #define os0thread_h
36 
37 #include <atomic>
38 #include <future>
39 #include <thread>
40 
41 class IB_thread {
42  public:
44 
45  State state() const {
46  return (m_state == nullptr ? State::INVALID : m_state->load());
47  }
48 
49  void start();
50  void wait(State state_to_wait_for = State::STOPPED);
51  void join();
52 
53  private:
54  std::shared_future<void> m_shared_future;
55  std::shared_ptr<std::atomic<State>> m_state;
56 
57  friend class Runnable;
58 
59  void init(std::promise<void> &promise);
60  void set_state(State state);
61 };
62 
63 /** Operating system thread native handle */
65 
66 /** Returns the thread identifier of current thread. Currently the thread
67 identifier in Unix is the thread handle itself.
68 @return current thread native handle */
70 
71 bool os_thread_set_priority(int priority);
72 
73 void os_thread_set_priority(int priority, const char *thread_name);
74 
75 /** Return the thread handle. The purpose of this function is to cast the
76 native handle to an integer type for consistency
77 @return the current thread ID cast to an uint64_t */
78 #define os_thread_handle() ((uint64_t)(os_thread_get_curr_id()))
79 
80 /** Compares two thread ids for equality.
81 @param[in] lhs OS thread or thread id
82 @param[in] rhs OS thread or thread id
83 return true if equal */
84 #define os_thread_eq(lhs, rhs) ((lhs) == (rhs))
85 
86 /** Advises the OS to give up remainder of the thread's time slice. */
87 #define os_thread_yield() \
88  do { \
89  std::this_thread::yield(); \
90  } while (false)
91 
92 /** The thread sleeps at least the time given in microseconds.
93 @param[in] usecs time in microseconds */
94 #define os_thread_sleep(usecs) \
95  do { \
96  std::this_thread::sleep_for(std::chrono::microseconds(usecs)); \
97  } while (false)
98 
99 #endif /* !os0thread_h */
void start()
Definition: os0thread.cc:55
Definition: os0thread.h:41
void join()
Definition: os0thread.cc:78
std::shared_ptr< std::atomic< State > > m_state
Definition: os0thread.h:55
int native_handle_type
Definition: routing.h:113
void init(std::promise< void > &promise)
Definition: os0thread.cc:80
void set_state(State state)
Definition: os0thread.cc:85
std::shared_future< void > m_shared_future
Definition: os0thread.h:54
Wrapper for a callable, it will count the number of registered Runnable instances and will register t...
Definition: os0thread-create.h:71
os_thread_id_t os_thread_get_curr_id()
Returns the thread identifier of current thread.
Definition: os0thread.cc:93
State
Definition: os0thread.h:43
bool os_thread_set_priority(int priority)
Set priority for current thread.
Definition: os0thread.cc:105
State state() const
Definition: os0thread.h:45
std::thread::native_handle_type os_thread_id_t
Operating system thread native handle.
Definition: os0thread.h:64
void wait(State state_to_wait_for=State::STOPPED)
Definition: os0thread.cc:67