1 /* 2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_UTILITIES_WORKGROUP_HPP 26 #define SHARE_VM_UTILITIES_WORKGROUP_HPP 27 28 #ifdef TARGET_OS_FAMILY_linux 29 # include "thread_linux.inline.hpp" 30 #endif 31 #ifdef TARGET_OS_FAMILY_solaris 32 # include "thread_solaris.inline.hpp" 33 #endif 34 #ifdef TARGET_OS_FAMILY_windows 35 # include "thread_windows.inline.hpp" 36 #endif 37 38 // Forward declarations of classes defined here 39 40 class WorkGang; 41 class GangWorker; 42 class YieldingFlexibleGangWorker; 43 class YieldingFlexibleGangTask; 44 class WorkData; 45 46 // An abstract task to be worked on by a gang. 47 // You subclass this to supply your own work() method 48 class AbstractGangTask VALUE_OBJ_CLASS_SPEC { 49 public: 50 // The abstract work method. 51 // The argument tells you which member of the gang you are. 52 virtual void work(int i) = 0; 53 54 // Debugging accessor for the name. 55 const char* name() const PRODUCT_RETURN_(return NULL;); 56 int counter() { return _counter; } 57 void set_counter(int value) { _counter = value; } 58 int *address_of_counter() { return &_counter; } 59 60 // RTTI 61 NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const { 62 return false; 63 }) 64 65 private: 66 NOT_PRODUCT(const char* _name;) 67 // ??? Should a task have a priority associated with it? 68 // ??? Or can the run method adjust priority as needed? 69 int _counter; 70 71 protected: 72 // Constructor and desctructor: only construct subclasses. 73 AbstractGangTask(const char* name) { 74 NOT_PRODUCT(_name = name); 75 _counter = 0; 76 } 77 virtual ~AbstractGangTask() { } 78 }; 79 80 81 // Class AbstractWorkGang: 82 // An abstract class representing a gang of workers. 83 // You subclass this to supply an implementation of run_task(). 84 class AbstractWorkGang: public CHeapObj { 85 // Here's the public interface to this class. 86 public: 87 // Constructor and destructor. 88 AbstractWorkGang(const char* name, bool are_GC_task_threads, 89 bool are_ConcurrentGC_threads); 90 ~AbstractWorkGang(); 91 // Run a task, returns when the task is done (or terminated). 92 virtual void run_task(AbstractGangTask* task) = 0; 93 // Stop and terminate all workers. 94 virtual void stop(); 95 public: 96 // Debugging. 97 const char* name() const; 98 protected: 99 // Initialize only instance data. 100 const bool _are_GC_task_threads; 101 const bool _are_ConcurrentGC_threads; 102 // Printing support. 103 const char* _name; 104 // The monitor which protects these data, 105 // and notifies of changes in it. 106 Monitor* _monitor; 107 // The count of the number of workers in the gang. 108 int _total_workers; 109 // Whether the workers should terminate. 110 bool _terminate; 111 // The array of worker threads for this gang. 112 // This is only needed for cleaning up. 113 GangWorker** _gang_workers; 114 // The task for this gang. 115 AbstractGangTask* _task; 116 // A sequence number for the current task. 117 int _sequence_number; 118 // The number of started workers. 119 int _started_workers; 120 // The number of finished workers. 121 int _finished_workers; 122 public: 123 // Accessors for fields 124 Monitor* monitor() const { 125 return _monitor; 126 } 127 int total_workers() const { 128 return _total_workers; 129 } 130 bool terminate() const { 131 return _terminate; 132 } 133 GangWorker** gang_workers() const { 134 return _gang_workers; 135 } 136 AbstractGangTask* task() const { 137 return _task; 138 } 139 int sequence_number() const { 140 return _sequence_number; 141 } 142 int started_workers() const { 143 return _started_workers; 144 } 145 int finished_workers() const { 146 return _finished_workers; 147 } 148 bool are_GC_task_threads() const { 149 return _are_GC_task_threads; 150 } 151 bool are_ConcurrentGC_threads() const { 152 return _are_ConcurrentGC_threads; 153 } 154 // Predicates. 155 bool is_idle() const { 156 return (task() == NULL); 157 } 158 // Return the Ith gang worker. 159 GangWorker* gang_worker(int i) const; 160 161 void threads_do(ThreadClosure* tc) const; 162 163 // Printing 164 void print_worker_threads_on(outputStream *st) const; 165 void print_worker_threads() const { 166 print_worker_threads_on(tty); 167 } 168 169 protected: 170 friend class GangWorker; 171 friend class YieldingFlexibleGangWorker; 172 // Note activation and deactivation of workers. 173 // These methods should only be called with the mutex held. 174 void internal_worker_poll(WorkData* data) const; 175 void internal_note_start(); 176 void internal_note_finish(); 177 }; 178 179 class WorkData: public StackObj { 180 // This would be a struct, but I want accessor methods. 181 private: 182 bool _terminate; 183 AbstractGangTask* _task; 184 int _sequence_number; 185 public: 186 // Constructor and destructor 187 WorkData() { 188 _terminate = false; 189 _task = NULL; 190 _sequence_number = 0; 191 } 192 ~WorkData() { 193 } 194 // Accessors and modifiers 195 bool terminate() const { return _terminate; } 196 void set_terminate(bool value) { _terminate = value; } 197 AbstractGangTask* task() const { return _task; } 198 void set_task(AbstractGangTask* value) { _task = value; } 199 int sequence_number() const { return _sequence_number; } 200 void set_sequence_number(int value) { _sequence_number = value; } 201 202 YieldingFlexibleGangTask* yf_task() const { 203 return (YieldingFlexibleGangTask*)_task; 204 } 205 }; 206 207 // Class WorkGang: 208 class WorkGang: public AbstractWorkGang { 209 public: 210 // Constructor 211 WorkGang(const char* name, int workers, 212 bool are_GC_task_threads, bool are_ConcurrentGC_threads); 213 // Run a task, returns when the task is done (or terminated). 214 virtual void run_task(AbstractGangTask* task); 215 }; 216 217 // Class GangWorker: 218 // Several instances of this class run in parallel as workers for a gang. 219 class GangWorker: public WorkerThread { 220 public: 221 // Constructors and destructor. 222 GangWorker(AbstractWorkGang* gang, uint id); 223 224 // The only real method: run a task for the gang. 225 virtual void run(); 226 // Predicate for Thread 227 virtual bool is_GC_task_thread() const; 228 virtual bool is_ConcurrentGC_thread() const; 229 // Printing 230 void print_on(outputStream* st) const; 231 virtual void print() const { print_on(tty); } 232 protected: 233 AbstractWorkGang* _gang; 234 235 virtual void initialize(); 236 virtual void loop(); 237 238 public: 239 AbstractWorkGang* gang() const { return _gang; } 240 }; 241 242 // A class that acts as a synchronisation barrier. Workers enter 243 // the barrier and must wait until all other workers have entered 244 // before any of them may leave. 245 246 class WorkGangBarrierSync : public StackObj { 247 protected: 248 Monitor _monitor; 249 int _n_workers; 250 int _n_completed; 251 bool _should_reset; 252 253 Monitor* monitor() { return &_monitor; } 254 int n_workers() { return _n_workers; } 255 int n_completed() { return _n_completed; } 256 bool should_reset() { return _should_reset; } 257 258 void zero_completed() { _n_completed = 0; } 259 void inc_completed() { _n_completed++; } 260 261 void set_should_reset(bool v) { _should_reset = v; } 262 263 public: 264 WorkGangBarrierSync(); 265 WorkGangBarrierSync(int n_workers, const char* name); 266 267 // Set the number of workers that will use the barrier. 268 // Must be called before any of the workers start running. 269 void set_n_workers(int n_workers); 270 271 // Enter the barrier. A worker that enters the barrier will 272 // not be allowed to leave until all other threads have 273 // also entered the barrier. 274 void enter(); 275 }; 276 277 // A class to manage claiming of subtasks within a group of tasks. The 278 // subtasks will be identified by integer indices, usually elements of an 279 // enumeration type. 280 281 class SubTasksDone: public CHeapObj { 282 jint* _tasks; 283 int _n_tasks; 284 int _n_threads; 285 jint _threads_completed; 286 #ifdef ASSERT 287 jint _claimed; 288 #endif 289 290 // Set all tasks to unclaimed. 291 void clear(); 292 293 public: 294 // Initializes "this" to a state in which there are "n" tasks to be 295 // processed, none of the which are originally claimed. The number of 296 // threads doing the tasks is initialized 1. 297 SubTasksDone(int n); 298 299 // True iff the object is in a valid state. 300 bool valid(); 301 302 // Set the number of parallel threads doing the tasks to "t". Can only 303 // be called before tasks start or after they are complete. 304 void set_par_threads(int t); 305 306 // Returns "false" if the task "t" is unclaimed, and ensures that task is 307 // claimed. The task "t" is required to be within the range of "this". 308 bool is_task_claimed(int t); 309 310 // The calling thread asserts that it has attempted to claim all the 311 // tasks that it will try to claim. Every thread in the parallel task 312 // must execute this. (When the last thread does so, the task array is 313 // cleared.) 314 void all_tasks_completed(); 315 316 // Destructor. 317 ~SubTasksDone(); 318 }; 319 320 // As above, but for sequential tasks, i.e. instead of claiming 321 // sub-tasks from a set (possibly an enumeration), claim sub-tasks 322 // in sequential order. This is ideal for claiming dynamically 323 // partitioned tasks (like striding in the parallel remembered 324 // set scanning). Note that unlike the above class this is 325 // a stack object - is there any reason for it not to be? 326 327 class SequentialSubTasksDone : public StackObj { 328 protected: 329 jint _n_tasks; // Total number of tasks available. 330 jint _n_claimed; // Number of tasks claimed. 331 jint _n_threads; // Total number of parallel threads. 332 jint _n_completed; // Number of completed threads. 333 334 void clear(); 335 336 public: 337 SequentialSubTasksDone() { clear(); } 338 ~SequentialSubTasksDone() {} 339 340 // True iff the object is in a valid state. 341 bool valid(); 342 343 // number of tasks 344 jint n_tasks() const { return _n_tasks; } 345 346 // Set the number of parallel threads doing the tasks to t. 347 // Should be called before the task starts but it is safe 348 // to call this once a task is running provided that all 349 // threads agree on the number of threads. 350 void set_par_threads(int t) { _n_threads = t; } 351 352 // Set the number of tasks to be claimed to t. As above, 353 // should be called before the tasks start but it is safe 354 // to call this once a task is running provided all threads 355 // agree on the number of tasks. 356 void set_n_tasks(int t) { _n_tasks = t; } 357 358 // Returns false if the next task in the sequence is unclaimed, 359 // and ensures that it is claimed. Will set t to be the index 360 // of the claimed task in the sequence. Will return true if 361 // the task cannot be claimed and there are none left to claim. 362 bool is_task_claimed(int& t); 363 364 // The calling thread asserts that it has attempted to claim 365 // all the tasks it possibly can in the sequence. Every thread 366 // claiming tasks must promise call this. Returns true if this 367 // is the last thread to complete so that the thread can perform 368 // cleanup if necessary. 369 bool all_tasks_completed(); 370 }; 371 372 // Represents a set of free small integer ids. 373 class FreeIdSet { 374 enum { 375 end_of_list = -1, 376 claimed = -2 377 }; 378 379 int _sz; 380 Monitor* _mon; 381 382 int* _ids; 383 int _hd; 384 int _waiters; 385 int _claimed; 386 387 static bool _safepoint; 388 typedef FreeIdSet* FreeIdSetPtr; 389 static const int NSets = 10; 390 static FreeIdSetPtr _sets[NSets]; 391 static bool _stat_init; 392 int _index; 393 394 public: 395 FreeIdSet(int sz, Monitor* mon); 396 ~FreeIdSet(); 397 398 static void set_safepoint(bool b); 399 400 // Attempt to claim the given id permanently. Returns "true" iff 401 // successful. 402 bool claim_perm_id(int i); 403 404 // Returns an unclaimed parallel id (waiting for one to be released if 405 // necessary). Returns "-1" if a GC wakes up a wait for an id. 406 int claim_par_id(); 407 408 void release_par_id(int id); 409 }; 410 411 #endif // SHARE_VM_UTILITIES_WORKGROUP_HPP