diff --git a/thread/workerpool.cpp b/thread/workerpool.cpp index 757584de..3f5caf95 100644 --- a/thread/workerpool.cpp +++ b/thread/workerpool.cpp @@ -24,6 +24,7 @@ limitations under the License. #include #include #include +#include namespace photon { @@ -34,19 +35,17 @@ class WorkPool::impl { photon::mutex worker_mtx; std::vector owned_std_threads; std::vector vcpus; + std::atomic vcpu_index{0}; photon::semaphore queue_sem; photon::semaphore ready_vcpu; photon::condition_variable exit_cv; photon::common::RingChannel< LockfreeMPMCRingQueue, 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, @@ -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]; } diff --git a/thread/workerpool.h b/thread/workerpool.h index b863bad3..72981c77 100644 --- a/thread/workerpool.h +++ b/thread/workerpool.h @@ -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) {