uThreads  0.3.0
Cluster.h
1 /*******************************************************************************
2  * Copyright © 2015, 2016 Saman Barghi
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 as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *******************************************************************************/
17 
18 #ifndef UTHREADS_CLUSTER_H_
19 #define UTHREADS_CLUSTER_H_
20 
21 #include <mutex>
22 #include <atomic>
23 #include <vector>
24 #include <assert.h>
25 #include "../generic/basics.h"
26 #include "../generic/IntrusiveContainers.h"
27 
28 class uThread;
29 class IOHandler;
30 class Scheduler;
31 class kThread;
32 class ClusterVar;
33 
61 class Cluster {
62  friend class kThread;
63  friend class uThread;
64  friend class Connection;
65  friend class IOHandler;
66  friend class Scheduler;
67 private:
68  // First 64 bytes (CACHELINE_SIZE)
69  Scheduler* scheduler; //(8 bytes)
70 
71  /*
72  * Scheduler's defined cluster variables
73  */
74  ClusterVar* clustervar; //(8 bytes)
75 
76  // First 64 bytes (CACHELINE_SIZE)
77 
78  static std::vector<Cluster*> clusterList;
79 
80  /*
81  * last kThread assigned to a uThread,
82  * for now kThread assignment is round robin
83  * manner.
84  */
85  std::atomic<size_t> ktLast;
86 
87  std::atomic_uint numberOfkThreads; //Number of kThreads in this Cluster
88  /*
89  * Vector of kThreads in this Cluster
90  */
91  std::vector<kThread*> ktVector;
92 
93  uint64_t clusterID; //Current Cluster ID
94 
95  std::mutex mtx; //Mutex used for initializations
96 
97  static std::atomic_ushort clusterMasterID; //Global cluster ID holder
98 
99 
100 
109  static Cluster defaultCluster;
110 
111  void initialSynchronization();
112 
113  /*
114  * Add a new kThread to this Cluster
115  */
116  void addNewkThread(kThread&);
117 
118  /*
119  * Assign a kThread to the requesting uThread
120  */
121  kThread* assignkThread();
122 
123 public:
127  Cluster();
128  virtual ~Cluster();
129 
133  Cluster(const Cluster&) = delete;
135  const Cluster& operator=(const Cluster&) = delete;
136 
144  return defaultCluster;
145  }
151  uint64_t getID() const {
152  return clusterID;
153  }
158  size_t getNumberOfkThreads() const {
159  return numberOfkThreads.load();
160  }
161 };
162 
163 #endif /* UTHREADS_CLUSTER_H_ */
static Cluster & getDefaultCluster()
Definition: Cluster.h:143
const Cluster & operator=(const Cluster &)=delete
Cluster()
Definition: Cluster.cpp:26
uint64_t getID() const
Get the ID of Cluster.
Definition: Cluster.h:151
Object to represent kernel threads.
Definition: kThread.h:54
size_t getNumberOfkThreads() const
Total number of kThreads belonging to this cluster.
Definition: Cluster.h:158
user-level threads (fiber)
Definition: uThread.h:63
Scheduler and Cluster of kThreads.
Definition: Cluster.h:61
Represents a network connection.
Definition: Network.h:33