Lockless Task Scheduler  v1.0a
A lockless task scheduler
globals.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="globals.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 
12 #include <atomic>
13 #include <cstdint>
14 #include <thread>
15 
16 #include "platform.h"
17 #include "types.h"
18 #include "utils.h"
19 
23 namespace task_scheduler
24 {
25 
29  const thread_num_t max_num_threads = 64;
30 
34  class globals
35  {
36  public:
40  std::mutex io_mutex;
41  ts_windows_only(HANDLE console_handle;);
45  std::atomic< thread_num_t > next_thread_number;
46 
52  : next_thread_number(1)
53  {
54  ts_windows_only(console_handle = GetStdHandle(STD_OUTPUT_HANDLE););
55  }
56 
57  template <class TKey>
58  static globals* instance()
59  {
60  static globals static_data;
61  return &static_data;
62  }
63  };
64 
68  thread_local tstring thread_name;
72  thread_local thread_num_t thread_unique_number;
76  thread_local void *current_thread;
77 
82  inline uint8_t get_thread_number()
83  {
84  if (!thread_unique_number)
85  {
86  thread_unique_number = ++(get<globals>()->next_thread_number);
87  }
88  return thread_unique_number;
89  }
90 
95  template < class T > T *get_current_thread() { return static_cast<T *>(current_thread); }
96 
97 }
thread_local thread_num_t thread_unique_number
The thread unique number
Definition: globals.h:72
std::atomic< thread_num_t > next_thread_number
The next thread number
Definition: globals.h:45
const thread_num_t max_num_threads
The maximum number threads
Definition: globals.h:29
Class stl_allocator.
Definition: allocator.h:16
thread_local void * current_thread
The current thread
Definition: globals.h:76
T * get_current_thread()
Gets the current thread.
Definition: globals.h:95
globals()
Statics the data t.
Definition: globals.h:51
thread_local tstring thread_name
The thread name
Definition: globals.h:68
uint8_t get_thread_number()
Gets the thread number.
Definition: globals.h:82
class globals
Definition: globals.h:34
std::mutex io_mutex
The io mutex
Definition: globals.h:40