1// Copyright (c) 2019, 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_STATE_H_
6#define RUNTIME_VM_THREAD_STATE_H_
7
8#include "include/dart_api.h"
9#include "vm/os_thread.h"
10
11namespace dart {
12
13class HandleScope;
14class LongJumpScope;
15class Zone;
16
17// ThreadState is a container for auxiliary thread-local state: e.g. it
18// owns a stack of Zones for allocation and a stack of StackResources
19// for stack unwinding.
20//
21// Important: this class is shared between compiler and runtime and
22// as such it should not expose any runtime internals due to layering
23// restrictions.
24class ThreadState : public BaseThread {
25 public:
26 // The currently executing thread, or NULL if not yet initialized.
27 static ThreadState* Current() {
28#if defined(HAS_C11_THREAD_LOCAL)
29 return OSThread::CurrentVMThread();
30#else
31 BaseThread* thread = OSThread::GetCurrentTLS();
32 if (thread == NULL || thread->is_os_thread()) {
33 return NULL;
34 }
35 return static_cast<ThreadState*>(thread);
36#endif
37 }
38
39 explicit ThreadState(bool is_os_thread);
40 virtual ~ThreadState();
41
42 // OSThread corresponding to this thread.
43 OSThread* os_thread() const { return os_thread_; }
44 void set_os_thread(OSThread* os_thread) { os_thread_ = os_thread; }
45
46 // The topmost zone used for allocation in this thread.
47 Zone* zone() const { return zone_; }
48
49 bool ZoneIsOwnedByThread(Zone* zone) const;
50
51 void IncrementMemoryCapacity(uintptr_t value) {
52 current_zone_capacity_ += value;
53 if (current_zone_capacity_ > zone_high_watermark_) {
54 zone_high_watermark_ = current_zone_capacity_;
55 }
56 }
57
58 void DecrementMemoryCapacity(uintptr_t value) {
59 ASSERT(current_zone_capacity_ >= value);
60 current_zone_capacity_ -= value;
61 }
62
63 uintptr_t current_zone_capacity() const { return current_zone_capacity_; }
64 uintptr_t zone_high_watermark() const { return zone_high_watermark_; }
65
66 void ResetHighWatermark() { zone_high_watermark_ = current_zone_capacity_; }
67
68 StackResource* top_resource() const { return top_resource_; }
69 void set_top_resource(StackResource* value) { top_resource_ = value; }
70 static intptr_t top_resource_offset() {
71 return OFFSET_OF(ThreadState, top_resource_);
72 }
73
74 LongJumpScope* long_jump_base() const { return long_jump_base_; }
75 void set_long_jump_base(LongJumpScope* value) { long_jump_base_ = value; }
76
77 bool IsValidZoneHandle(Dart_Handle object) const;
78 intptr_t CountZoneHandles() const;
79 bool IsValidScopedHandle(Dart_Handle object) const;
80 intptr_t CountScopedHandles() const;
81
82 virtual bool MayAllocateHandles() = 0;
83
84 HandleScope* top_handle_scope() const {
85#if defined(DEBUG)
86 return top_handle_scope_;
87#else
88 return 0;
89#endif
90 }
91
92 void set_top_handle_scope(HandleScope* handle_scope) {
93#if defined(DEBUG)
94 top_handle_scope_ = handle_scope;
95#endif
96 }
97
98 private:
99 void set_zone(Zone* zone) { zone_ = zone; }
100
101 OSThread* os_thread_ = nullptr;
102 Zone* zone_ = nullptr;
103 uintptr_t current_zone_capacity_ = 0;
104 uintptr_t zone_high_watermark_ = 0;
105 StackResource* top_resource_ = nullptr;
106 LongJumpScope* long_jump_base_ = nullptr;
107
108 // This field is only used in the DEBUG builds, but we don't exclude it
109 // because it would cause RELEASE and DEBUG builds to have different
110 // offsets for various Thread fields that are used from generated code.
111 HandleScope* top_handle_scope_ = nullptr;
112
113 friend class ApiZone;
114 friend class StackZone;
115};
116
117} // namespace dart
118
119#endif // RUNTIME_VM_THREAD_STATE_H_
120