/src/brpc/src/bthread/task_meta.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // bthread - An M:N threading library to make applications more concurrent. |
19 | | |
20 | | // Date: Tue Jul 10 17:40:58 CST 2012 |
21 | | |
22 | | #ifndef BTHREAD_TASK_META_H |
23 | | #define BTHREAD_TASK_META_H |
24 | | |
25 | | #include <pthread.h> // pthread_spin_init |
26 | | #include "bthread/butex.h" // butex_construct/destruct |
27 | | #include "butil/atomicops.h" // butil::atomic |
28 | | #include "bthread/types.h" // bthread_attr_t |
29 | | #include "bthread/stack.h" // ContextualStack |
30 | | #include "bthread/timer_thread.h" |
31 | | |
32 | | namespace bthread { |
33 | | |
34 | | struct TaskStatistics { |
35 | | int64_t cputime_ns; |
36 | | int64_t nswitch; |
37 | | int64_t cpu_usage_ns; |
38 | | }; |
39 | | |
40 | | class KeyTable; |
41 | | struct ButexWaiter; |
42 | | |
43 | | struct LocalStorage { |
44 | | KeyTable* keytable; |
45 | | void* assigned_data; |
46 | | void* rpcz_parent_span; |
47 | | }; |
48 | | |
49 | | #define BTHREAD_LOCAL_STORAGE_INITIALIZER { NULL, NULL, NULL } |
50 | | |
51 | | const static LocalStorage LOCAL_STORAGE_INIT = BTHREAD_LOCAL_STORAGE_INITIALIZER; |
52 | | |
53 | | enum TaskStatus { |
54 | | TASK_STATUS_UNKNOWN, |
55 | | TASK_STATUS_CREATED, |
56 | | TASK_STATUS_FIRST_READY, |
57 | | TASK_STATUS_READY, |
58 | | TASK_STATUS_JUMPING, |
59 | | TASK_STATUS_RUNNING, |
60 | | TASK_STATUS_SUSPENDED, |
61 | | TASK_STATUS_END, |
62 | | }; |
63 | | |
64 | | struct TaskMeta { |
65 | | // [Not Reset] |
66 | | butil::atomic<ButexWaiter*> current_waiter{NULL}; |
67 | | uint64_t current_sleep{TimerThread::INVALID_TASK_ID}; |
68 | | |
69 | | // A flag to mark if the Timer scheduling failed. |
70 | | bool sleep_failed{false}; |
71 | | |
72 | | // A builtin flag to mark if the thread is stopping. |
73 | | bool stop{false}; |
74 | | |
75 | | // The thread is interrupted and should wake up from some blocking ops. |
76 | | bool interrupted{false}; |
77 | | |
78 | | // Scheduling of the thread can be delayed. |
79 | | bool about_to_quit{false}; |
80 | | |
81 | | // [Not Reset] guarantee visibility of version_butex. |
82 | | pthread_spinlock_t version_lock{}; |
83 | | |
84 | | // [Not Reset] only modified by one bthread at any time, no need to be atomic |
85 | | uint32_t* version_butex{NULL}; |
86 | | |
87 | | // The identifier. It does not have to be here, however many code is |
88 | | // simplified if they can get tid from TaskMeta. |
89 | | bthread_t tid{INVALID_BTHREAD}; |
90 | | |
91 | | // User function and argument |
92 | | void* (*fn)(void*){NULL}; |
93 | | void* arg{NULL}; |
94 | | |
95 | | // Stack of this task. |
96 | | ContextualStack* stack{NULL}; |
97 | | |
98 | | // Attributes creating this task |
99 | | bthread_attr_t attr{BTHREAD_ATTR_NORMAL}; |
100 | | |
101 | | // Statistics |
102 | | int64_t cpuwide_start_ns{0}; |
103 | | TaskStatistics stat{}; |
104 | | |
105 | | // bthread local storage, sync with tls_bls (defined in task_group.cpp) |
106 | | // when the bthread is created or destroyed. |
107 | | // DO NOT use this field directly, use tls_bls instead. |
108 | | LocalStorage local_storage{}; |
109 | | |
110 | | // Only used when TaskTracer is enabled. |
111 | | // Bthread status. |
112 | | TaskStatus status{TASK_STATUS_UNKNOWN}; |
113 | | // Whether bthread is traced? |
114 | | bool traced{false}; |
115 | | // Worker thread id. |
116 | | pthread_t worker_tid{}; |
117 | | |
118 | | public: |
119 | | // Only initialize [Not Reset] fields, other fields will be reset in |
120 | | // bthread_start* functions |
121 | 0 | TaskMeta() { |
122 | 0 | pthread_spin_init(&version_lock, 0); |
123 | 0 | version_butex = butex_create_checked<uint32_t>(); |
124 | 0 | *version_butex = 1; |
125 | 0 | } |
126 | | |
127 | 0 | ~TaskMeta() { |
128 | 0 | butex_destroy(version_butex); |
129 | 0 | version_butex = NULL; |
130 | 0 | pthread_spin_destroy(&version_lock); |
131 | 0 | } |
132 | | |
133 | 0 | void set_stack(ContextualStack* s) { |
134 | 0 | stack = s; |
135 | 0 | } |
136 | | |
137 | 0 | ContextualStack* release_stack() { |
138 | 0 | ContextualStack* tmp = stack; |
139 | 0 | stack = NULL; |
140 | 0 | return tmp; |
141 | 0 | } |
142 | | |
143 | 0 | StackType stack_type() const { |
144 | 0 | return static_cast<StackType>(attr.stack_type); |
145 | 0 | } |
146 | | }; |
147 | | |
148 | | } // namespace bthread |
149 | | |
150 | | #endif // BTHREAD_TASK_META_H |