1// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_THREAD_REGISTRY_H_
6#define RUNTIME_VM_THREAD_REGISTRY_H_
7
8#include "vm/globals.h"
9#include "vm/growable_array.h"
10#include "vm/isolate.h"
11#include "vm/lockers.h"
12#include "vm/stack_frame.h"
13#include "vm/thread.h"
14
15namespace dart {
16
17#ifndef PRODUCT
18class JSONStream;
19class JSONArray;
20#endif
21
22// Unordered collection of threads relating to a particular isolate group.
23class ThreadRegistry {
24 public:
25 ThreadRegistry()
26 : threads_lock_(),
27 active_list_(nullptr),
28 free_list_(nullptr),
29 active_isolates_count_(0) {}
30 ~ThreadRegistry();
31
32 void VisitObjectPointers(IsolateGroup* isolate_group_of_interest,
33 ObjectPointerVisitor* visitor,
34 ValidationPolicy validate_frames);
35
36 void ForEachThread(std::function<void(Thread* thread)> callback);
37 void ReleaseStoreBuffers();
38 void AcquireMarkingStacks();
39 void ReleaseMarkingStacks();
40
41 // Concurrent-approximate number of active isolates in the active_list
42 intptr_t active_isolates_count() { return active_isolates_count_.load(); }
43
44 Monitor* threads_lock() const { return &threads_lock_; }
45
46#ifndef PRODUCT
47 void PrintJSON(JSONStream* stream) const;
48#endif
49
50 private:
51 Thread* active_list() const { return active_list_; }
52
53 Thread* GetFreeThreadLocked(bool is_vm_isolate);
54 void ReturnThreadLocked(Thread* thread);
55 void AddToActiveListLocked(Thread* thread);
56 void RemoveFromActiveListLocked(Thread* thread);
57 Thread* GetFromFreelistLocked(bool is_vm_isolate);
58 void ReturnToFreelistLocked(Thread* thread);
59
60 // This monitor protects the threads list for an isolate, it is used whenever
61 // we need to iterate over threads (both active and free) in an isolate.
62 mutable Monitor threads_lock_;
63 Thread* active_list_; // List of active threads in the isolate.
64 Thread* free_list_; // Free list of Thread objects that can be reused.
65 RelaxedAtomic<intptr_t> active_isolates_count_;
66
67 friend class Thread;
68 friend class SafepointHandler;
69 DISALLOW_COPY_AND_ASSIGN(ThreadRegistry);
70};
71
72} // namespace dart
73
74#endif // RUNTIME_VM_THREAD_REGISTRY_H_
75

source code of dart_sdk/runtime/vm/thread_registry.h