codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / runtime / server / host-health-monitor.h
blob4c9833fdbdb0a1495f78da9fd4b5e59f08b8b694
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_SERVER_MEMORY_PROTECTOR_H_
18 #define incl_HPHP_SERVER_MEMORY_PROTECTOR_H_
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23 #include <vector>
24 #include <boost/container/flat_set.hpp>
26 #include "hphp/runtime/base/config.h"
27 #include "hphp/util/async-func.h"
28 #include "hphp/util/health-monitor-types.h"
30 namespace HPHP {
32 // This class must be used as a singleton.
33 struct HostHealthMonitor {
34 void subscribe(IHostHealthObserver* observer) {
35 assert(observer != nullptr);
36 std::lock_guard<std::mutex> g(m_lock);
37 m_observers.insert(observer);
39 bool unsubscribe(IHostHealthObserver* observer) {
40 std::lock_guard<std::mutex> g(m_lock);
41 return !!m_observers.erase(observer);
43 void addMetric(IHealthMonitorMetric* metric);
45 void start();
46 void stop();
47 void waitForEnd();
49 private:
50 HealthLevel evaluate();
51 void notifyObservers(HealthLevel newStatus);
52 void monitor();
54 std::vector<IHealthMonitorMetric*> m_metrics;
55 boost::container::flat_set<IHostHealthObserver*> m_observers;
56 std::mutex m_lock; // protects metrics/observers
57 HealthLevel m_status{HealthLevel::Bold};
58 std::mutex m_stopped_lock;
59 std::condition_variable m_condition;
60 bool m_stopped{true};
61 std::unique_ptr<AsyncFunc<HostHealthMonitor>> m_monitor_func;
66 #endif