Skip to content

Commit

Permalink
thread_migrate strategy changes from random to round-robin (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Jul 25, 2023
1 parent e8d082e commit 13ec413
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions thread/workerpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include <algorithm>
#include <random>
#include <thread>
#include <atomic>

namespace photon {

Expand All @@ -34,19 +35,17 @@ class WorkPool::impl {
photon::mutex worker_mtx;
std::vector<std::thread> owned_std_threads;
std::vector<photon::vcpu_base *> vcpus;
std::atomic<uint64_t> vcpu_index{0};
photon::semaphore queue_sem;
photon::semaphore ready_vcpu;
photon::condition_variable exit_cv;
photon::common::RingChannel<
LockfreeMPMCRingQueue<Delegate<void>, RING_SIZE>>
ring;

std::random_device rd;
std::mt19937 gen;
int mode;

impl(size_t vcpu_num, int ev_engine, int io_engine, int mode)
: queue_sem(0), ready_vcpu(0), gen(rd()), mode(mode) {
: queue_sem(0), ready_vcpu(0), mode(mode) {
for (size_t i = 0; i < vcpu_num; ++i) {
owned_std_threads.emplace_back(
&WorkPool::impl::worker_thread_routine, this, ev_engine,
Expand Down Expand Up @@ -134,8 +133,9 @@ class WorkPool::impl {
}

photon::vcpu_base *get_vcpu_in_pool(size_t index) {
if (index >= vcpus.size()) {
index = gen() % vcpus.size();
auto size = vcpus.size();
if (index >= size) {
index = vcpu_index++ % size;
}
return vcpus[index];
}
Expand Down
2 changes: 1 addition & 1 deletion thread/workerpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class WorkPool {
*
* @param th Photon thread that goint to migrate
* @param index Which vcpu in pool to migrate to. if index is not in range
* [0, vcpu_num), it will choose random one in pool.
* [0, vcpu_num), it will choose the next one in pool (round-robin).
* @return int 0 for success, and <0 means failed to migrate.
*/
int thread_migrate(photon::thread* th = CURRENT, size_t index = -1UL) {
Expand Down

0 comments on commit 13ec413

Please sign in to comment.