@@ -255,21 +255,6 @@ void Scheduler::SleepSwitch(int timeout_ms)
255255 sleep_wait_.CoSwitch (timeout_ms);
256256}
257257
258- bool Scheduler::UserBlockWait (uint32_t type, uint64_t wait_id)
259- {
260- return BlockWait ((int64_t )type, wait_id);
261- }
262-
263- bool Scheduler::TryUserBlockWait (uint32_t type, uint64_t wait_id)
264- {
265- return TryBlockWait ((int64_t )type, wait_id);
266- }
267-
268- uint32_t Scheduler::UserBlockWakeup (uint32_t type, uint64_t wait_id, uint32_t wakeup_count)
269- {
270- return BlockWakeup ((int64_t )type, wait_id, wakeup_count);
271- }
272-
273258TimerId Scheduler::ExpireAt (CoTimerMgr::TimePoint const & time_point,
274259 CoTimer::fn_t const & fn)
275260{
@@ -299,78 +284,4 @@ ThreadPool& Scheduler::GetThreadPool()
299284 return *thread_pool_;
300285}
301286
302- bool Scheduler::BlockWait (int64_t type, uint64_t wait_id)
303- {
304- if (!IsCoroutine ()) return false ;
305- Task* tk = GetLocalInfo ().current_task ;
306- tk->user_wait_type_ = type;
307- tk->user_wait_id_ = wait_id;
308- tk->state_ = type < 0 ? TaskState::sys_block : TaskState::user_block;
309- DebugPrint (dbg_wait, " task(%s) %s. wait_type=%lld, wait_id=%llu" ,
310- tk->DebugInfo (), type < 0 ? " sys_block" : " user_block" ,
311- (long long int )tk->user_wait_type_ , (long long unsigned )tk->user_wait_id_ );
312- CoYield ();
313- return true ;
314- }
315-
316- bool Scheduler::TryBlockWait (int64_t type, uint64_t wait_id)
317- {
318- std::unique_lock<LFLock> locker (user_wait_lock_);
319- auto it = user_wait_tasks_.find (type);
320- if (user_wait_tasks_.end () == it) return false ;
321-
322- auto &zone = it->second ;
323- auto it2 = zone.find (wait_id);
324- if (zone.end () == it2) return false ;
325-
326- auto &wait_pair = it2->second ;
327- if (wait_pair.first > 0 ) {
328- --wait_pair.first ;
329- ClearWaitPairWithoutLock (type, wait_id, zone, wait_pair);
330- return true ;
331- }
332-
333- return false ;
334- }
335-
336- uint32_t Scheduler::BlockWakeup (int64_t type, uint64_t wait_id, uint32_t wakeup_count)
337- {
338- std::unique_lock<LFLock> locker (user_wait_lock_);
339- auto &zone = user_wait_tasks_[type];
340- auto &wait_pair = zone[wait_id];
341- auto &task_queue = wait_pair.second ;
342- SList<Task> tasks = task_queue.pop (wakeup_count);
343- std::size_t c = tasks.size ();
344- if (c < wakeup_count) // 允许提前设置唤醒标志, 以便多线程同步。
345- wait_pair.first += wakeup_count - c;
346- ClearWaitPairWithoutLock (type, wait_id, zone, wait_pair);
347- uint32_t domain_wakeup = wait_pair.first ;
348- locker.unlock ();
349-
350- for (auto &task: tasks)
351- {
352- ++c;
353- Task *tk = &task;
354- DebugPrint (dbg_wait, " %s wakeup task(%s). wait_type=%lld, wait_id=%llu" ,
355- type < 0 ? " sys_block" : " user_block" , tk->DebugInfo (), (long long int )type, (long long unsigned )wait_id);
356- AddTaskRunnable (tk);
357- }
358-
359- DebugPrint (dbg_wait, " %s wakeup %u tasks, domain wakeup=%u. wait_type=%lld, wait_id=%llu" ,
360- type < 0 ? " sys_block" : " user_block" , (unsigned )c, domain_wakeup, (long long int )type, (long long unsigned )wait_id);
361- return c;
362- }
363-
364- void Scheduler::ClearWaitPairWithoutLock (int64_t type,
365- uint64_t wait_id, WaitZone& zone, WaitPair& wait_pair)
366- {
367- if (wait_pair.second .empty () && wait_pair.first == 0 ) {
368- if (zone.size () > 1 ) {
369- zone.erase (wait_id);
370- } else {
371- user_wait_tasks_.erase (type);
372- }
373- }
374- }
375-
376287} // namespace co
0 commit comments