use new hugify method only in fb builds
[hiphop-php.git] / hphp / util / synchronizable-multi.h
blob9b49f2864b8244a3ff135b671c27a3f1baee3cca
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_SYNCHRONIZABLE_MULTI_H_
18 #define incl_HPHP_SYNCHRONIZABLE_MULTI_H_
20 #include "hphp/util/mutex.h"
21 #include <pthread.h>
22 #include <folly/IntrusiveList.h>
23 #include <vector>
25 namespace HPHP {
26 ///////////////////////////////////////////////////////////////////////////////
28 /**
29 * A Synchronizable object that has multiple conditional variables. The benefit
30 * is, notify() can choose to wake up a thread that is more favorable (e.g.,
31 * one with stack/heap mapped on huge pages, or one that is recently active).
33 struct SynchronizableMulti {
34 explicit SynchronizableMulti(int size);
35 virtual ~SynchronizableMulti() {}
38 * Threads are notified based on their priority. The priority is decided when
39 * calling wait().
41 enum Priority {
42 Highest,
43 High,
44 Normal,
45 Low
47 /**
48 * "id" is an arbitrary number that locates the conditional variable to wait
49 * on.
51 void wait(int id, int q, Priority pri);
52 bool wait(int id, int q, Priority pri, long seconds); // false if timed out
53 bool wait(int id, int q, Priority pri, long seconds, long long nanosecs);
54 void notify();
55 void notifyAll();
56 void setNumGroups(int num_groups);
58 Mutex &getMutex() { return m_mutex;}
60 private:
61 Mutex m_mutex;
62 int m_group;
64 struct alignas(64) CondVarNode {
65 pthread_cond_t m_cond;
66 folly::IntrusiveListHook m_listHook;
68 CondVarNode() {
69 pthread_cond_init(&m_cond, nullptr);
71 ~CondVarNode() {
72 pthread_cond_destroy(&m_cond);
74 /* implicit */ operator pthread_cond_t*() {
75 return &m_cond;
77 void unlink() {
78 if (m_listHook.is_linked()) m_listHook.unlink();
82 std::vector<CondVarNode> m_conds;
84 // List that supports four priorities, implemented using two intrusive lists.
85 struct alignas(64) CondVarList {
86 CondVarList() = default;
88 bool empty() const {
89 return m_highPriList.empty() && m_midLowPriList.empty();
91 CondVarNode& front() {
92 if (!m_highPriList.empty()) return m_highPriList.front();
93 return m_midLowPriList.front();
95 void pop_front() {
96 if (!m_highPriList.empty()) m_highPriList.pop_front();
97 else m_midLowPriList.pop_front();
100 void push(CondVarNode& c, SynchronizableMulti::Priority pri) {
101 c.unlink();
102 if (pri == Priority::Highest) {
103 m_highPriList.push_front(c);
104 } else if (pri == Priority::High) {
105 m_highPriList.push_back(c);
106 } else if (pri == Priority::Normal) {
107 m_midLowPriList.push_front(c);
108 } else {
109 assertx(pri == Priority::Low);
110 m_midLowPriList.push_back(c);
114 private:
115 using CondVarIList =
116 folly::IntrusiveList<CondVarNode, &CondVarNode::m_listHook>;
117 CondVarIList m_highPriList;
118 CondVarIList m_midLowPriList;
120 std::vector<CondVarList> m_cond_list_vec;
122 bool waitImpl(int id, int q, Priority pri, timespec *ts);
125 ///////////////////////////////////////////////////////////////////////////////
128 #endif // incl_HPHP_SYNCHRONIZABLE_MULTI_H_