-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Messages sent based on a assigned priority Higher priority messages gets preference over lower priority ones Add environment variable for setting high water mark for zmq
- Loading branch information
1 parent
411c9bb
commit db61897
Showing
10 changed files
with
162 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (c) 2015 by Contributors | ||
*/ | ||
#ifndef PS_INTERNAL_THREADSAFE_PQUEUE_H_ | ||
#define PS_INTERNAL_THREADSAFE_PQUEUE_H_ | ||
#include <queue> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <memory> | ||
#include <utility> | ||
#include <vector> | ||
#include "ps/base.h" | ||
namespace ps { | ||
|
||
/** | ||
* \brief thread-safe queue allowing push and waited pop | ||
*/ | ||
class ThreadsafePQueue { | ||
public: | ||
ThreadsafePQueue() { } | ||
~ThreadsafePQueue() { } | ||
|
||
/** | ||
* \brief push an value into the end. threadsafe. | ||
* \param new_value the value | ||
*/ | ||
void Push(Message new_value) { | ||
mu_.lock(); | ||
queue_.push(std::move(new_value)); | ||
mu_.unlock(); | ||
cond_.notify_all(); | ||
} | ||
|
||
/** | ||
* \brief wait until pop an element from the beginning, threadsafe | ||
* \param value the poped value | ||
*/ | ||
void WaitAndPop(Message* value) { | ||
std::unique_lock<std::mutex> lk(mu_); | ||
cond_.wait(lk, [this]{return !queue_.empty();}); | ||
*value = std::move(queue_.top()); | ||
queue_.pop(); | ||
} | ||
|
||
private: | ||
class Compare { | ||
public: | ||
bool operator()(const Message &l, const Message &r) { | ||
return l.meta.priority <= r.meta.priority; | ||
} | ||
}; | ||
mutable std::mutex mu_; | ||
std::priority_queue<Message, std::vector<Message>, Compare> queue_; | ||
std::condition_variable cond_; | ||
}; | ||
|
||
} // namespace ps | ||
|
||
#endif // PS_INTERNAL_THREADSAFE_PQUEUE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2015 by Contributors | ||
*/ | ||
#ifndef PS_P3_VAN_H_ | ||
#define PS_P3_VAN_H_ | ||
#include <memory> | ||
namespace ps { | ||
|
||
/** | ||
* \brief P3 based Van implementation | ||
*/ | ||
class P3Van : public ZMQVan { | ||
public: | ||
P3Van() {} | ||
virtual ~P3Van() {} | ||
|
||
protected: | ||
void Start(int customer_id) override { | ||
start_mu_.lock(); | ||
if (init_stage == 0) { | ||
// start sender | ||
sender_thread_ = std::unique_ptr<std::thread>( | ||
new std::thread(&P3Van::Sending, this)); | ||
init_stage++; | ||
} | ||
start_mu_.unlock(); | ||
ZMQVan::Start(customer_id); | ||
} | ||
|
||
void Stop() override { | ||
ZMQVan::Stop(); | ||
sender_thread_->join(); | ||
} | ||
|
||
int SendMsg(const Message& msg) override { | ||
send_queue_.Push(msg); | ||
return 0; | ||
} | ||
|
||
void Sending() { | ||
while (true) { | ||
Message msg; | ||
send_queue_.WaitAndPop(&msg); | ||
ZMQVan::SendMsg(msg); | ||
if (!msg.meta.control.empty() && | ||
msg.meta.control.cmd == Control::TERMINATE) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private: | ||
/** the thread for sending messages */ | ||
std::unique_ptr<std::thread> sender_thread_; | ||
ThreadsafePQueue send_queue_; | ||
int init_stage = 0; | ||
}; | ||
} // namespace ps | ||
|
||
#endif // PS_P3_VAN_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters