Add logging for comparison behaviors
[hiphop-php.git] / hphp / util / synchronizable-multi.h
blob019d7ff107ccbe5adfab3cce24b416ea24758d6d
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 notifySpecific(int id);
55 void notify();
56 void notifyAll();
57 void setNumGroups(int num_groups);
59 Mutex &getMutex() { return m_mutex;}
61 private:
62 Mutex m_mutex;
63 int m_group;
65 struct alignas(64) CondVarNode {
66 pthread_cond_t m_cond;
67 folly::IntrusiveListHook m_listHook;
69 CondVarNode() {
70 pthread_cond_init(&m_cond, nullptr);
72 ~CondVarNode() {
73 pthread_cond_destroy(&m_cond);
75 /* implicit */ operator pthread_cond_t*() {
76 return &m_cond;
78 void unlink() {
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;
89 bool empty() const {
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();
96 void pop_front() {
97 if (!m_highPriList.empty()) m_highPriList.pop_front();
98 else m_midLowPriList.pop_front();
101 void push(CondVarNode& c, SynchronizableMulti::Priority pri) {
102 c.unlink();
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);
109 } else {
110 assertx(pri == Priority::Low);
111 m_midLowPriList.push_back(c);
115 private:
116 using CondVarIList =
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_