1 /* Copyright (c) 2012, 2014 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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 #ifndef GLOBAL_THREADS_INCLUDED
24 #define GLOBAL_THREADS_INCLUDED
25 
26 #include <my_global.h>
27 #include <my_pthread.h>
28 #include <set>
29 
30 class THD;
31 
32 extern mysql_mutex_t LOCK_thread_count;
33 extern mysql_mutex_t LOCK_thd_remove;
34 extern mysql_cond_t COND_thread_count;
35 
36 /**
37   We maintain a set of all registered threads.
38   We provide accessors (global_thread_list_begin, global_thread_list_end)
39   to iterate over all threads. There is no guarantee on the order of THDs
40   when iterating. The caller of these two accessors is expected to
41   hold lock on LOCK_thread_count mutex before calling these accessors inorder
42   to make sure that there are no new threads are added to the system while
43   processing this thread list.
44 
45   We also provide mutators for inserting, and removing an element:
46     > add_global_thread() inserts a THD into the set, and increments the counter.
47        The caller of this function is expected to hold lock on LOCK_thread_count
48        mutex before calling this function to avoid any parallel operations
49        happening on the set.
50     > remove_global_thread() removes a THD from the set, and decrements the counter.
51        This function acquires lock on LOCK_thd_remove followed by lock on
52        LOCK_thread_count. At the end of the function, it releases both the
53        locks. remove_global_thread() also broadcasts COND_thread_count.
54 
55   We also provide a function (copy_global_thread_list) to copy global thread list
56   into a new set. The caller of this function is expected to hold lock on
57   LOCK_thd_remove to avoid any removal from the copied set. This function also
58   acquires lock on LOCK_thread_count before the copy operation to
59   avoid any parallel modification to the set and will release the lock at the
60   end of the function.
61  */
62 typedef std::set<THD*>::iterator Thread_iterator;
63 Thread_iterator global_thread_list_begin();
64 Thread_iterator global_thread_list_end();
65 void copy_global_thread_list(std::set<THD*> *new_copy);
66 void add_global_thread(THD *);
67 void remove_global_thread(THD *);
68 
69 /*
70   We maintain a separate counter for the number of threads,
71   which can be accessed without LOCK_thread_count.
72   An un-locked read, means that the result is fuzzy of course.
73   This accessor is used by DBUG printing, by signal handlers,
74   and by the 'mysqladmin status' command.
75 */
76 uint get_thread_count();
77 
78 #endif  // GLOBAL_THREADS_INCLUDED
79