CUBRID Engine  latest
thread_entry.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * thread_entry - interface of thread contextual cache. for backward compatibility it has the unintuitive name entry
21  */
22 
23 #ifndef _THREAD_ENTRY_HPP_
24 #define _THREAD_ENTRY_HPP_
25 
26 #if !defined (SERVER_MODE) && !defined (SA_MODE)
27 #error Wrong module
28 #endif // not SERVER_MODE and not SA_MODE
29 
30 #include "error_context.hpp"
32 #include "porting.h" // for pthread_mutex_t, drand48_data
33 #include "system.h" // for UINTPTR, INT64, HL_HEAPID
34 
35 #include <atomic>
36 #include <thread>
37 
38 #include <cassert>
39 
40 // forward definitions
41 // from adjustable_array.h
42 struct adj_array;
43 // from connection_defs.h
44 struct css_conn_entry;
45 // from fault_injection.h
46 struct fi_test_item;
47 // from log_system_tran.hpp
48 class log_system_tdes;
49 // from log_compress.h
50 struct log_zip;
51 // from vacuum.h
52 struct vacuum_worker;
53 // from xasl_unpack_info.hpp
54 struct xasl_unpack_info;
55 
56 // forward resource trackers
57 namespace cubbase
58 {
59  template <typename Res>
60  class resource_tracker;
61 
62  // trackers
63  // memory allocations
65  // page fix
67 }
68 namespace cubsync
69 {
70  class critical_section_tracker;
71 }
72 namespace cubload
73 {
74  class driver;
75 }
76 
77 // for lock-free - FIXME
78 enum
79 {
92 };
93 #define THREAD_TS_COUNT THREAD_TS_LAST
94 struct lf_tran_entry;
95 
96 // for what?? - FIXME
97 /* stats for event logging */
98 typedef struct event_stat EVENT_STAT;
99 struct event_stat
100 {
101  // todo - replace timeval with std::chrono::milliseconds
102  /* slow query stats */
103  struct timeval cs_waits;
104  struct timeval lock_waits;
105  struct timeval latch_waits;
106 
107  /* temp volume expand stats */
108  struct timeval temp_expand_time;
110 
111  /* save PRM_ID_SQL_TRACE_SLOW_MSECS for performance */
113 
114  /* log flush thread wait time */
116 };
117 
119 
120 // FIXME - move these enum to cubthread::entry
122 {
131 };
132 
134 {
160 };
161 
162 namespace cubthread
163 {
164 
165  // cubthread::entry
166  //
167  // description
168  // this is the thread context used by most server module functions to access thread-specific information quickly
169  //
170  // note
171  // in CUBRID, thread entries are pooled and dispatched by cubthread::manager. see thread_manager.hpp for details.
172  //
173  // this is an implementation in progress. for backward compatibility, all legacy members in this class are public;
174  // however, they will be gradually converted into private members with access functions
175  //
176  // this header is paired with thread_compat.hpp which is used for compatibility between modules. only server
177  // module (with its SERVER_MODE and SA_MODE versions) has access to the content of this entry. client modules only
178  // sees a void pointer.
179  //
180  // to avoid major refactoring, the THREAD_ENTRY alias is kept
181  //
182  // todo
183  // make member variable private
184  //
185  // remove content that does not belong here
186  //
187  // migrate here thread entry related functionality from thread.c/h
188  //
189  class entry
190  {
191  public:
192  entry ();
193  ~entry ();
194 
195  // enumerations
196  enum class status
197  {
198  TS_DEAD,
199  TS_FREE,
200  TS_RUN,
201  TS_WAIT,
202  TS_CHECK
203  };
204 
205  // public functions
206  void request_lock_free_transactions (void); // todo: lock-free refactoring
207 
208  // The rules of thumbs is to always use private members. Until a complete refactoring, these members will remain
209  // public
210  int index; /* thread entry index */
211  thread_type type; /* thread type */
212  thread_id_t emulate_tid; /* emulated thread id; applies to non-worker threads, when works on behalf of a worker
213  * thread */
214  int client_id; /* client id whom this thread is responding */
215  int tran_index; /* tran index to which this thread belongs */
216  int private_lru_index; /* private lru index when transaction quota is used */
217  pthread_mutex_t tran_index_lock;
218  unsigned int rid; /* request id which this thread is processing */
219  status m_status; /* thread status */
220 
221  pthread_mutex_t th_entry_lock; /* latch for this thread entry */
222  pthread_cond_t wakeup_cond; /* wakeup condition */
223 
224  HL_HEAPID private_heap_id; /* id of thread private memory allocator */
225  adj_array *cnv_adj_buffer[3]; /* conversion buffer */
226 
227  css_conn_entry *conn_entry; /* conn entry ptr */
228 
229  xasl_unpack_info *xasl_unpack_info_ptr; /* XASL_UNPACK_INFO * */
230  int xasl_errcode; /* xasl errorcode */
232 
233  unsigned int rand_seed; /* seed for rand_r() */
234  struct drand48_data rand_buf; /* seed for lrand48_r(), drand48_r() */
235 
237  int request_latch_mode; /* for page latch support */
240  bool interrupted; /* is this request/transaction interrupted ? */
241  std::atomic_bool shutdown; /* is server going down? */
242  bool check_interrupt; /* check_interrupt == false, during fl_alloc* function call. */
243  bool wait_for_latch_promote; /* this thread is waiting for latch promotion */
245 
246  void *lockwait;
247  INT64 lockwait_stime; /* time in milliseconds */
248  int lockwait_msecs; /* time in milliseconds */
250  void *query_entry;
252  entry *worker_thrd_list; /* worker thread on job queue */
253 
258 
260 
261  int net_request_index; /* request index of net server functions */
262 
263  struct vacuum_worker *vacuum_worker; /* Vacuum worker info */
264 
266 
268 
269  /* for query profile */
271  bool on_trace;
273 
274  /* for lock free structures */
276 
277 #if !defined(NDEBUG)
279 
281 #endif
283 
285 
286  thread_id_t get_id ();
287  pthread_t get_posix_id ();
288  void register_id ();
289  void unregister_id ();
290  bool is_on_current_thread () const;
291 
293 
294  void lock (void);
295  void unlock (void);
296 
298  {
299  return m_error;
300  }
301 
303  {
304  return m_alloc_tracker;
305  }
307  {
308  return m_pgbuf_tracker;
309  }
311  {
312  return m_csect_tracker;
313  }
314 
316  {
317  return m_systdes;
318  }
320  {
321  m_systdes = sys_tdes;
322  }
323  void reset_system_tdes (void)
324  {
325  m_systdes = NULL;
326  }
327  void claim_system_worker ();
328  void retire_system_worker ();
329 
330  void end_resource_tracks (void);
331  void push_resource_tracks (void);
332  void pop_resource_tracks (void);
333 
334  void assign_lf_tran_index (lockfree::tran::index idx);
335  lockfree::tran::index pull_lf_tran_index ();
336  lockfree::tran::index get_lf_tran_index ();
337 
338  private:
339  void clear_resources (void);
340 
342 
343  // error manager context
345 
346  // TODO: move all members her
347  bool m_cleared;
348 
349  // trackers
354 
356  };
357 
358 } // namespace cubthread
359 
360 #ifndef _THREAD_COMPAT_HPP_
361 // The whole code uses THREAD_ENTRY... It is ridiculous to change entire code to rename.
364 #endif // _THREAD_COMPAT_HPP_
365 
367 // alias functions for C legacy code
368 //
369 // use inline functions instead definitions
371 
372 inline int
374 {
375  return thread_p->xasl_recursion_depth;
376 }
377 
378 inline void
380 {
381  thread_p->xasl_recursion_depth++;
382 }
383 
384 inline void
386 {
387  thread_p->xasl_recursion_depth--;
388 }
389 
390 inline void
392 {
393  thread_p->xasl_recursion_depth = 0;
394 }
395 
396 inline void
398 {
399  thread_p->on_trace = true;
400 }
401 
402 inline void
404 {
405  thread_p->trace_format = format;
406 }
407 
408 inline bool
410 {
411  return thread_p->on_trace;
412 }
413 
414 inline void
416 {
417  thread_p->clear_trace = clear;
418 }
419 
420 inline bool
422 {
423  return thread_p->clear_trace;
424 }
425 
426 inline bool
428 {
429  return thread_p->sort_stats_active;
430 }
431 
432 inline bool
434 {
435  bool old_flag = thread_p->sort_stats_active;
436  thread_p->sort_stats_active = new_flag;
437  return old_flag;
438 }
439 
440 inline void
442 {
443  thread_p->lock ();
444 }
445 
446 inline void
448 {
449  thread_p->unlock ();
450 }
451 
454  thread_resume_suspend_status suspended_reason);
457  thread_resume_suspend_status suspend_reason);
459 int thread_suspend_with_other_mutex (cubthread::entry *p, pthread_mutex_t *mutexp, int timeout, struct timespec *to,
460  thread_resume_suspend_status suspended_reason);
461 
462 const char *thread_type_to_string (thread_type type);
465 #endif // _THREAD_ENTRY_HPP_
int thread_suspend_with_other_mutex(cubthread::entry *p, pthread_mutex_t *mutexp, int timeout, struct timespec *to, thread_resume_suspend_status suspended_reason)
void thread_suspend_wakeup_and_unlock_entry(cubthread::entry *p, thread_resume_suspend_status suspended_reason)
void reset_system_tdes(void)
cubbase::pgbuf_tracker & get_pgbuf_tracker(void)
int temp_expand_pages
EVENT_STAT event_stats
unsigned int rid
int thread_get_recursion_depth(cubthread::entry *thread_p)
css_conn_entry * conn_entry
int thread_suspend_timeout_wakeup_and_unlock_entry(cubthread::entry *p, struct timespec *t, thread_resume_suspend_status suspended_reason)
cuberr::context m_error
bool thread_need_clear_trace(cubthread::entry *thread_p)
unsigned long int thread_id_t
Definition: lock_free.h:120
bool thread_set_sort_stats_active(cubthread::entry *thread_p, bool new_flag)
bool trace_slow_query
std::atomic_bool shutdown
thread_resume_suspend_status resume_status
void thread_wakeup_already_had_mutex(cubthread::entry *p, thread_resume_suspend_status resume_reason)
bool thread_is_on_trace(cubthread::entry *thread_p)
std::thread::id thread_id_t
thread_resume_suspend_status
pthread_mutex_t th_entry_lock
struct log_zip * log_zip_undo
cubbase::alloc_tracker & get_alloc_tracker(void)
thread_type type
void thread_set_trace_format(cubthread::entry *thread_p, int format)
void set_system_tdes(log_system_tdes *sys_tdes)
void thread_check_suspend_reason_and_wakeup(cubthread::entry *thread_p, thread_resume_suspend_status resume_reason, thread_resume_suspend_status suspend_reason)
thread_id_t m_id
pthread_mutex_t tran_index_lock
cuberr::context & get_error_context(void)
fi_test_item * fi_test_array
thread_type
cubsync::critical_section_tracker & get_csect_tracker(void)
cubload::driver * m_loaddb_driver
void thread_set_clear_trace(cubthread::entry *thread_p, bool clear)
#define THREAD_TS_COUNT
#define NULL
Definition: freelistheap.h:34
cubbase::alloc_tracker & m_alloc_tracker
struct log_zip * log_zip_redo
void thread_lock_entry(cubthread::entry *thread_p)
void return_lock_free_transaction_entries(void)
cubthread::entry THREAD_ENTRY
void thread_trace_on(cubthread::entry *thread_p)
unsigned int rand_seed
void thread_unlock_entry(cubthread::entry *thread_p)
entry * worker_thrd_list
thread_id_t emulate_tid
log_system_tdes * get_system_tdes(void)
void thread_dec_recursion_depth(cubthread::entry *thread_p)
void thread_clear_recursion_depth(cubthread::entry *thread_p)
pthread_cond_t wakeup_cond
log_system_tdes * m_systdes
const char * thread_resume_status_to_string(thread_resume_suspend_status resume_status)
HL_HEAPID private_heap_id
void thread_wakeup(cubthread::entry *p, thread_resume_suspend_status resume_reason)
const char * thread_status_to_string(cubthread::entry::status status)
cubbase::pgbuf_tracker & m_pgbuf_tracker
void thread_inc_recursion_depth(cubthread::entry *thread_p)
const char * thread_type_to_string(thread_type type)
xasl_unpack_info * xasl_unpack_info_ptr
int trace_log_flush_time
lockfree::tran::index m_lf_tran_index
const char ** p
Definition: dynamic_load.c:945
cubsync::critical_section_tracker & m_csect_tracker
void clear(cub_regex_object *&regex, char *&pattern)
struct vacuum_worker * vacuum_worker
bool thread_get_sort_stats_active(cubthread::entry *thread_p)