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
);
54 void notifySpecific(int id
);
57 void setNumGroups(int num_groups
);
59 Mutex
&getMutex() { return m_mutex
;}
65 struct alignas(64) CondVarNode
{
66 pthread_cond_t m_cond
;
67 folly::IntrusiveListHook m_listHook
;
70 pthread_cond_init(&m_cond
, nullptr);
73 pthread_cond_destroy(&m_cond
);
75 /* implicit */ operator pthread_cond_t
*() {
79 if (m_listHook
.is_linked()) m_listHook
.unlink();
83 std::vector
<CondVarNode
> m_conds
;
85 // List that supports four priorities, implemented using two intrusive lists.
86 struct alignas(64) CondVarList
{
87 CondVarList() = default;
90 return m_highPriList
.empty() && m_midLowPriList
.empty();
92 CondVarNode
& front() {
93 if (!m_highPriList
.empty()) return m_highPriList
.front();
94 return m_midLowPriList
.front();
97 if (!m_highPriList
.empty()) m_highPriList
.pop_front();
98 else m_midLowPriList
.pop_front();
101 void push(CondVarNode
& c
, SynchronizableMulti::Priority pri
) {
103 if (pri
== Priority::Highest
) {
104 m_highPriList
.push_front(c
);
105 } else if (pri
== Priority::High
) {
106 m_highPriList
.push_back(c
);
107 } else if (pri
== Priority::Normal
) {
108 m_midLowPriList
.push_front(c
);
110 assertx(pri
== Priority::Low
);
111 m_midLowPriList
.push_back(c
);
117 folly::IntrusiveList
<CondVarNode
, &CondVarNode::m_listHook
>;
118 CondVarIList m_highPriList
;
119 CondVarIList m_midLowPriList
;
121 std::vector
<CondVarList
> m_cond_list_vec
;
123 bool waitImpl(int id
, int q
, Priority pri
, timespec
*ts
);
126 ///////////////////////////////////////////////////////////////////////////////
129 #endif // incl_HPHP_SYNCHRONIZABLE_MULTI_H_