/src/brpc/src/bthread/task_group_inl.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_GROUP_INL_H |
23 | | #define BTHREAD_TASK_GROUP_INL_H |
24 | | |
25 | | namespace bthread { |
26 | | |
27 | | // Utilities to manipulate bthread_t |
28 | 0 | inline bthread_t make_tid(uint32_t version, butil::ResourceId<TaskMeta> slot) { |
29 | 0 | return (((bthread_t)version) << 32) | (bthread_t)slot.value; |
30 | 0 | } |
31 | | |
32 | 0 | inline butil::ResourceId<TaskMeta> get_slot(bthread_t tid) { |
33 | 0 | butil::ResourceId<TaskMeta> id = { (tid & 0xFFFFFFFFul) }; |
34 | 0 | return id; |
35 | 0 | } |
36 | 0 | inline uint32_t get_version(bthread_t tid) { |
37 | 0 | return (uint32_t)((tid >> 32) & 0xFFFFFFFFul); |
38 | 0 | } |
39 | | |
40 | 0 | inline TaskMeta* TaskGroup::address_meta(bthread_t tid) { |
41 | | // TaskMeta * m = address_resource<TaskMeta>(get_slot(tid)); |
42 | | // if (m != NULL && m->version == get_version(tid)) { |
43 | | // return m; |
44 | | // } |
45 | | // return NULL; |
46 | 0 | return address_resource(get_slot(tid)); |
47 | 0 | } |
48 | | |
49 | 0 | inline void TaskGroup::exchange(TaskGroup** pg, TaskMeta* next_meta) { |
50 | 0 | TaskGroup* g = *pg; |
51 | 0 | if (g->is_current_pthread_task()) { |
52 | 0 | return g->ready_to_run(next_meta); |
53 | 0 | } |
54 | 0 | ReadyToRunArgs args = { g->tag(), g->_cur_meta, false }; |
55 | 0 | g->set_remained((g->current_task()->about_to_quit |
56 | 0 | ? ready_to_run_in_worker_ignoresignal |
57 | 0 | : ready_to_run_in_worker), |
58 | 0 | &args); |
59 | 0 | TaskGroup::sched_to(pg, next_meta); |
60 | 0 | } |
61 | | |
62 | 0 | inline void TaskGroup::sched_to(TaskGroup** pg, bthread_t next_tid) { |
63 | 0 | TaskMeta* next_meta = address_meta(next_tid); |
64 | 0 | if (next_meta->stack == NULL) { |
65 | | #ifdef BUTIL_USE_ASAN |
66 | | ContextualStack* stk = get_stack(next_meta->stack_type(), asan_task_runner); |
67 | | #else |
68 | 0 | ContextualStack* stk = get_stack(next_meta->stack_type(), task_runner); |
69 | 0 | #endif // BUTIL_USE_ASAN |
70 | 0 | if (stk) { |
71 | 0 | next_meta->set_stack(stk); |
72 | 0 | } else { |
73 | | // stack_type is BTHREAD_STACKTYPE_PTHREAD or out of memory, |
74 | | // In latter case, attr is forced to be BTHREAD_STACKTYPE_PTHREAD. |
75 | | // This basically means that if we can't allocate stack, run |
76 | | // the task in pthread directly. |
77 | 0 | next_meta->attr.stack_type = BTHREAD_STACKTYPE_PTHREAD; |
78 | 0 | next_meta->set_stack((*pg)->_main_stack); |
79 | 0 | } |
80 | 0 | } |
81 | | // Update now_ns only when wait_task did yield. |
82 | 0 | sched_to(pg, next_meta); |
83 | 0 | } |
84 | | |
85 | 0 | inline void TaskGroup::push_rq(bthread_t tid) { |
86 | 0 | while (!_rq.push(tid)) { |
87 | | // Created too many bthreads: a promising approach is to insert the |
88 | | // task into another TaskGroup, but we don't use it because: |
89 | | // * There're already many bthreads to run, inserting the bthread |
90 | | // into other TaskGroup does not help. |
91 | | // * Insertions into other TaskGroups perform worse when all workers |
92 | | // are busy at creating bthreads (proved by test_input_messenger in |
93 | | // brpc) |
94 | 0 | flush_nosignal_tasks(); |
95 | 0 | LOG_EVERY_SECOND(ERROR) << "_rq is full, capacity=" << _rq.capacity(); |
96 | | // TODO(gejun): May cause deadlock when all workers are spinning here. |
97 | | // A better solution is to pop and run existing bthreads, however which |
98 | | // make set_remained()-callbacks do context switches and need extensive |
99 | | // reviews on related code. |
100 | 0 | ::usleep(1000); |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | 0 | inline void TaskGroup::flush_nosignal_tasks_remote() { |
105 | 0 | if (_remote_num_nosignal) { |
106 | 0 | _remote_rq._mutex.lock(); |
107 | 0 | flush_nosignal_tasks_remote_locked(_remote_rq._mutex); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | } // namespace bthread |
112 | | |
113 | | #endif // BTHREAD_TASK_GROUP_INL_H |