1 /*
2    Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef NDB_THREAD_H
26 #define NDB_THREAD_H
27 
28 #include <ndb_global.h>
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 typedef enum NDB_THREAD_PRIO_ENUM {
35   NDB_THREAD_PRIO_HIGHEST,
36   NDB_THREAD_PRIO_HIGH,
37   NDB_THREAD_PRIO_MEAN,
38   NDB_THREAD_PRIO_LOW,
39   NDB_THREAD_PRIO_LOWEST
40 } NDB_THREAD_PRIO;
41 
42 typedef enum NDB_THREAD_TLS_ENUM {
43   NDB_THREAD_TLS_JAM,           /* Jam buffer pointer. */
44   NDB_THREAD_TLS_THREAD,        /* Thread self pointer. */
45   NDB_THREAD_TLS_MAX
46 } NDB_THREAD_TLS;
47 
48 typedef void* (NDB_THREAD_FUNC)(void*);
49 typedef void* NDB_THREAD_ARG;
50 typedef size_t NDB_THREAD_STACKSIZE;
51 
52 struct NdbThread;
53 
54 /*
55   Method to block/unblock thread from receiving KILL signal with
56   signum set in g_ndb_shm_signum in a portable manner.
57 */
58 #ifdef NDB_SHM_TRANSPORTER
59 void NdbThread_set_shm_sigmask(my_bool block);
60 #endif
61 
62 /**
63  * Create a thread
64  *
65  *  * p_thread_func: pointer of the function to run in the thread
66  *  * p_thread_arg: pointer to argument to be passed to the thread
67  *  * thread_stack_size: stack size for this thread, 0 => default size
68  *  * p_thread_name: pointer to name of this thread
69  *  * returnvalue: pointer to the created thread
70  */
71 struct NdbThread* NdbThread_Create(NDB_THREAD_FUNC *p_thread_func,
72                                    NDB_THREAD_ARG *p_thread_arg,
73                                    const NDB_THREAD_STACKSIZE thread_stack_size,
74                                    const char* p_thread_name,
75                                    NDB_THREAD_PRIO thread_prio);
76 
77 
78 /**
79  * Create a thread-object for "main" that can be used with
80  *   other NdbThread_*-functions
81  */
82 struct NdbThread* NdbThread_CreateObject(const char * name);
83 
84 /**
85  * Destroy a thread
86  *  Deallocates memory for thread
87  *  And NULL the pointer
88  *
89  */
90 void NdbThread_Destroy(struct NdbThread** p_thread);
91 
92 /**
93  * Waitfor a thread, suspend the execution of the calling thread
94  * until the wait_thread_id completes
95  *
96  * * p_wait_thread, pointer to the thread to wait for
97  * * status: exit code from thread waited for
98  * * returnvalue: true = succeded, false = failed
99  */
100 int NdbThread_WaitFor(struct NdbThread* p_wait_thread, void** status);
101 
102 /**
103  * Exit thread, terminates the calling thread
104  *
105  * *  status: exit code
106  */
107 void NdbThread_Exit(void *status);
108 
109 /**
110  * Set thread concurrency level
111  *
112  * *
113  */
114 int NdbThread_SetConcurrencyLevel(int level);
115 
116 /**
117  * Get "Tid" for thread
118  *   should only be used for printing etc...
119  *   return -1, if not supported on platform
120  */
121 int NdbThread_GetTid(struct NdbThread*);
122 
123 /**
124  * Set Scheduler for thread
125  */
126 int NdbThread_SetScheduler(struct NdbThread*, my_bool rt_prio, my_bool high_prio);
127 
128 /**
129  * Lock Thread to CPU
130  */
131 int NdbThread_LockCPU(struct NdbThread*, Uint32 cpu);
132 
133 /**
134  * Fetch and set thread-local storage entry.
135  */
136 void *NdbThread_GetTlsKey(NDB_THREAD_TLS key);
137 void NdbThread_SetTlsKey(NDB_THREAD_TLS key, void *value);
138 
139 /**
140  * Set properties for NDB_THREAD_PRIO_HIGH
141  *
142  * NOTE 1: should be set *prior* to starting thread
143  * NOTE 2: if these properties *can* be applied is not checked
144  *         if they can not, then it will be silently ignored
145  *
146  * @param spec <fifo | rr>[,<prio>]
147  *
148  * @return 0  - parse ok
149  *  return -1 - Invalid spec
150  */
151 int NdbThread_SetHighPrioProperties(const char * spec);
152 
153 #ifdef	__cplusplus
154 }
155 #endif
156 
157 #endif
158