1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <deque>
34 
35 #include "mongo/base/status.h"
36 #include "mongo/platform/atomic_word.h"
37 #include "mongo/stdx/condition_variable.h"
38 #include "mongo/stdx/mutex.h"
39 #include "mongo/transport/service_executor.h"
40 #include "mongo/transport/service_executor_task_names.h"
41 
42 namespace mongo {
43 namespace transport {
44 
45 /**
46  * The reserved service executor emulates a thread per connection.
47  * Each connection has its own worker thread where jobs get scheduled.
48  *
49  * The executor will start reservedThreads on start, and create a new thread every time it
50  * starts a new thread, ensuring there are always reservedThreads available for work - this
51  * means that even when you hit the NPROC ulimit, there will still be threads ready to
52  * accept work. When threads exit, they will go back to waiting for work if there are fewer
53  * than reservedThreads available.
54  */
55 class ServiceExecutorReserved final : public ServiceExecutor {
56 public:
57     explicit ServiceExecutorReserved(ServiceContext* ctx, std::string name, size_t reservedThreads);
58 
59     Status start() override;
60     Status shutdown(Milliseconds timeout) override;
61     Status schedule(Task task, ScheduleFlags flags, ServiceExecutorTaskName taskName) override;
62 
transportMode()63     Mode transportMode() const override {
64         return Mode::kSynchronous;
65     }
66 
67     void appendStats(BSONObjBuilder* bob) const override;
68 
69 private:
70     Status _startWorker();
71 
72     static thread_local std::deque<Task> _localWorkQueue;
73     static thread_local int _localRecursionDepth;
74     static thread_local int64_t _localThreadIdleCounter;
75 
76     AtomicBool _stillRunning{false};
77 
78     mutable stdx::mutex _mutex;
79     stdx::condition_variable _threadWakeup;
80     stdx::condition_variable _shutdownCondition;
81 
82     std::deque<Task> _readyTasks;
83 
84     AtomicUInt32 _numRunningWorkerThreads{0};
85     size_t _numReadyThreads{0};
86     size_t _numStartingThreads{0};
87 
88     const std::string _name;
89     const size_t _reservedThreads;
90 };
91 
92 }  // namespace transport
93 }  // namespace mongo
94