Flutter Engine
The Flutter Engine
thread_state.h
Go to the documentation of this file.
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 nullptr if not yet initialized.
28
29 explicit ThreadState(bool is_os_thread);
30 virtual ~ThreadState();
31
32 // OSThread corresponding to this thread.
33 OSThread* os_thread() const { return os_thread_; }
34 void set_os_thread(OSThread* os_thread) { os_thread_ = os_thread; }
35
36 // The topmost zone used for allocation in this thread.
37 Zone* zone() const { return zone_; }
38
39 bool ZoneIsOwnedByThread(Zone* zone) const;
40
41 StackResource* top_resource() const { return top_resource_; }
42 void set_top_resource(StackResource* value) { top_resource_ = value; }
43 static intptr_t top_resource_offset() {
44 return OFFSET_OF(ThreadState, top_resource_);
45 }
46
47 LongJumpScope* long_jump_base() const { return long_jump_base_; }
48 void set_long_jump_base(LongJumpScope* value) { long_jump_base_ = value; }
49
50 bool IsValidZoneHandle(Dart_Handle object) const;
51 intptr_t CountZoneHandles() const;
52 bool IsValidScopedHandle(Dart_Handle object) const;
53 intptr_t CountScopedHandles() const;
54
55 virtual bool MayAllocateHandles() = 0;
56
58#if defined(DEBUG)
59 return top_handle_scope_;
60#else
61 return 0;
62#endif
63 }
64
65 void set_top_handle_scope(HandleScope* handle_scope) {
66#if defined(DEBUG)
67 top_handle_scope_ = handle_scope;
68#endif
69 }
70
71 private:
72 void set_zone(Zone* zone) { zone_ = zone; }
73
74 OSThread* os_thread_ = nullptr;
75 Zone* zone_ = nullptr;
76 StackResource* top_resource_ = nullptr;
77 LongJumpScope* long_jump_base_ = nullptr;
78
79 // This field is only used in the DEBUG builds, but we don't exclude it
80 // because it would cause RELEASE and DEBUG builds to have different
81 // offsets for various Thread fields that are used from generated code.
82 HandleScope* top_handle_scope_ = nullptr;
83
84 friend class ApiZone;
85 friend class StackZone;
86};
87
88} // namespace dart
89
90#endif // RUNTIME_VM_THREAD_STATE_H_
friend class ThreadState
Definition: os_thread.h:79
bool is_os_thread() const
Definition: os_thread.h:71
friend class OSThread
Definition: os_thread.h:80
static ThreadState * CurrentVMThread()
Definition: os_thread.h:188
bool ZoneIsOwnedByThread(Zone *zone) const
Definition: thread_state.cc:16
virtual bool MayAllocateHandles()=0
Zone * zone() const
Definition: thread_state.h:37
void set_top_handle_scope(HandleScope *handle_scope)
Definition: thread_state.h:65
bool IsValidZoneHandle(Dart_Handle object) const
Definition: thread_state.cc:28
virtual ~ThreadState()
Definition: thread_state.cc:14
void set_os_thread(OSThread *os_thread)
Definition: thread_state.h:34
bool IsValidScopedHandle(Dart_Handle object) const
Definition: thread_state.cc:50
HandleScope * top_handle_scope() const
Definition: thread_state.h:57
void set_top_resource(StackResource *value)
Definition: thread_state.h:42
static intptr_t top_resource_offset()
Definition: thread_state.h:43
intptr_t CountScopedHandles() const
Definition: thread_state.cc:61
void set_long_jump_base(LongJumpScope *value)
Definition: thread_state.h:48
LongJumpScope * long_jump_base() const
Definition: thread_state.h:47
StackResource * top_resource() const
Definition: thread_state.h:41
intptr_t CountZoneHandles() const
Definition: thread_state.cc:39
static ThreadState * Current()
Definition: thread_state.h:27
OSThread * os_thread() const
Definition: thread_state.h:33
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
uint8_t value
Definition: dart_vm.cc:33
#define OFFSET_OF(type, field)
Definition: globals.h:138