MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
thread_utils.h
1 /* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #ifndef SQL_THREAD_INCLUDED
17 #define SQL_THREAD_INCLUDED
18 
19 #include <my_global.h>
20 #include <my_pthread.h>
21 
22 namespace thread {
23 
24 class Thread_start_arg;
25 
26 /*
27  An abstract class for creating/running/joining threads.
28  Thread::start() will create a new pthread, and execute the run() function.
29 */
30 class Thread
31 {
32 public:
33  Thread() : m_thread_id(0)
34 #ifdef __WIN__
35  , m_thread_handle(NULL)
36 #endif
37  {}
38  virtual ~Thread();
39 
40  /*
41  Will create a new pthread, and invoke run();
42  Returns the value from pthread_create().
43  */
44  int start();
45 
46  /*
47  You may invoke this to wait for the thread to finish.
48  You should probalbly join() a thread before deleting it.
49  */
50  void join();
51 
52  // The id of the thread (valid only if it is actually running).
53  pthread_t thread_id() const { return m_thread_id; }
54 
55  /*
56  A wrapper for the run() function.
57  Users should *not* call this function directly, they should rather
58  invoke the start() function.
59  */
60  static void run_wrapper(Thread_start_arg*);
61 
62 protected:
63  /*
64  Define this function in derived classes.
65  Users should *not* call this function directly, they should rather
66  invoke the start() function.
67  */
68  virtual void run() = 0;
69 
70 private:
71  pthread_t m_thread_id;
72 #ifdef __WIN__
73  // We need an open handle to the thread in order to join() it.
74  HANDLE m_thread_handle;
75 #endif
76 
77  Thread(const Thread&); /* Not copyable. */
78  void operator=(const Thread&); /* Not assignable. */
79 };
80 
81 
82 // A simple wrapper around a mutex:
83 // Grabs the mutex in the CTOR, releases it in the DTOR.
85 {
86 public:
87  Mutex_lock(mysql_mutex_t *mutex);
88  ~Mutex_lock();
89 private:
90  mysql_mutex_t *m_mutex;
91 
92  Mutex_lock(const Mutex_lock&); /* Not copyable. */
93  void operator=(const Mutex_lock&); /* Not assignable. */
94 };
95 
96 
97 // A barrier which can be used for one-time synchronization between threads.
99 {
100 public:
101  Notification();
102  ~Notification();
103 
104  bool has_been_notified();
105  void wait_for_notification();
106  void notify();
107 private:
108  bool m_notified;
109  mysql_cond_t m_cond;
110  mysql_mutex_t m_mutex;
111 
112  Notification(const Notification&); /* Not copyable. */
113  void operator=(const Notification&); /* Not assignable. */
114 };
115 
116 } // namespace thread
117 
118 #endif // SQL_THREAD_INCLUDED