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.
23class ThreadRegistry {
24 public:
25 ThreadRegistry() : threads_lock_(), active_list_(NULL), free_list_(NULL) {}
26 ~ThreadRegistry();
27
28 void VisitObjectPointers(IsolateGroup* isolate_group_of_interest,
29 ObjectPointerVisitor* visitor,
30 ValidationPolicy validate_frames);
31
32 void ReleaseStoreBuffers();
33 void AcquireMarkingStacks();
34 void ReleaseMarkingStacks();
35
36#ifndef PRODUCT
37 void PrintJSON(JSONStream* stream) const;
38#endif
39
40 intptr_t CountZoneHandles(Isolate* isolate_of_interest) const;
41 intptr_t CountScopedHandles(Isolate* isolate_of_interest) const;
42
43 private:
44 Thread* active_list() const { return active_list_; }
45 Monitor* threads_lock() const { return &threads_lock_; }
46
47 Thread* GetFreeThreadLocked(bool is_vm_isolate);
48 void ReturnThreadLocked(Thread* thread);
49 void AddToActiveListLocked(Thread* thread);
50 void RemoveFromActiveListLocked(Thread* thread);
51 Thread* GetFromFreelistLocked(bool is_vm_isolate);
52 void ReturnToFreelistLocked(Thread* thread);
53
54 // This monitor protects the threads list for an isolate, it is used whenever
55 // we need to iterate over threads (both active and free) in an isolate.
56 mutable Monitor threads_lock_;
57 Thread* active_list_; // List of active threads in the isolate.
58 Thread* free_list_; // Free list of Thread objects that can be reused.
59
60 friend class Isolate;
61 friend class IsolateGroup;
62 friend class SafepointHandler;
63 friend class Scavenger;
64 DISALLOW_COPY_AND_ASSIGN(ThreadRegistry);
65};
66
67} // namespace dart
68
69#endif // RUNTIME_VM_THREAD_REGISTRY_H_
70