2 +----------------------------------------------------------------------+
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"
22 #include <folly/IntrusiveList.h>
26 ///////////////////////////////////////////////////////////////////////////////
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
48 * "id" is an arbitrary number that locates the conditional variable to wait
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
);
56 void setNumGroups(int num_groups
);
58 Mutex
&getMutex() { return m_mutex
;}
64 struct alignas(64) CondVarNode
{
65 pthread_cond_t m_cond
;
66 folly::IntrusiveListHook m_listHook
;
69 pthread_cond_init(&m_cond
, nullptr);
72 pthread_cond_destroy(&m_cond
);
74 /* implicit */ operator pthread_cond_t
*() {
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;
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();
96 if (!m_highPriList
.empty()) m_highPriList
.pop_front();
97 else m_midLowPriList
.pop_front();
100 void push(CondVarNode
& c
, SynchronizableMulti::Priority pri
) {
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
);
109 assertx(pri
== Priority::Low
);
110 m_midLowPriList
.push_back(c
);
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_