virtual void Run() { now = v8::base::OS::TimeCurrentMillis(); while (1) { AsyncEvent *p; std::multimap<double, AsyncEvent *>::iterator e; wait(); now = v8::base::OS::TimeCurrentMillis(); while (1) { p = s_acSleep.get(); if (p == NULL) break; s_tms.insert(std::make_pair(now + p->result(), p)); } while (1) { e = s_tms.begin(); if (e == s_tms.end()) break; if (e->first > now) break; e->second->apost(0); s_tms.erase(e); } } }
static void PASCAL Timer(unsigned int uTimerID, unsigned int uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) { AsyncEvent *p; double tm; std::multimap<double, AsyncEvent *>::iterator e; while (1) { p = s_acSleep.get(); if (p == NULL) break; tm = s_time + s_now + p->result(); s_tms.insert(std::make_pair(tm, p)); } atom_xchg(&s_now, (int)(v8::internal::OS::TimeCurrentMillis() - s_time)); while (1) { e = s_tms.begin(); if (e == s_tms.end()) break; if (e->first > s_time + s_now) break; e->second->apost(0); s_tms.erase(e); } }
void worker_proc() { Runtime rt(NULL); AsyncEvent *p; m_idleWorkers.dec(); while (true) { if (m_idleWorkers.inc() > MAX_IDLE_WORKERS) { if (m_idleWorkers.dec() > 0) break; m_idleWorkers.inc(); } p = m_pool.get(); if (m_idleWorkers.dec() == 0) if (m_idleWorkers.CompareAndSwap(0, 1) == 0) new_worker(); p->invoke(); } }
void Service::switchtonext() { while (1) { // First switch if we have work to do. if (!m_resume.empty()) { Fiber *old = m_running; m_running = m_resume.get(); fb_switch(&old->m_cntxt, &m_running->m_cntxt); if (m_recycle) { m_recycle->m_joins.set(); m_recycle->Unref(); m_recycle = NULL; } break; } // Then weakup async event. while (1) { AsyncEvent *p = m_aEvents.get(); if (p == NULL) break; p->callback(); } if (!m_resume.empty()) continue; // doing smoething when we have time. if (m_Idle) m_Idle(); if (!m_resume.empty()) continue; // if we still have time, weakup yield fiber. while (1) { AsyncEvent *p = m_yieldList.get(); if (p == NULL) break; p->callback(); } if (!m_resume.empty()) continue; // still no work, we wait, and wait, and wait..... m_aEvents.wait()->callback(); } }
void Fiber::sleep(int ms) { if (Service::hasService()) { if (ms <= 0) yield(); else { AsyncEvent as; as.sleep(ms); as.wait(); } } else OSThread::Sleep(ms); }
void *FiberBase::fiber_proc(void *p) { Isolate* isolate = (Isolate*)p; Runtime rt; rt.m_pDateCache = &isolate->m_dc; Runtime::reg(&rt); v8::Locker locker(isolate->m_isolate); v8::Isolate::Scope isolate_scope(isolate->m_isolate); v8::HandleScope handle_scope(isolate->m_isolate); v8::Context::Scope context_scope( v8::Local<v8::Context>::New(isolate->m_isolate, isolate->m_context)); isolate->m_idleFibers --; while (1) { AsyncEvent *ae; if ((ae = (AsyncEvent*)isolate->m_jobs.tryget()) == NULL) { isolate->m_idleFibers ++; if (isolate->m_idleFibers > g_spareFibers) { isolate->m_idleFibers --; break; } { v8::Unlocker unlocker(isolate->m_isolate); ae = (AsyncEvent*)isolate->m_jobs.get(); } isolate->m_idleFibers --; } { v8::HandleScope handle_scope(isolate->m_isolate); ae->js_invoke(); } } isolate->m_currentFibers --; return NULL; }
//要得到返回值 to do.... int JtEventServer::DoInSync(AsyncEvent Event) { int res = -1; if(IsInThread()) { res = Event.m_Functor(Event.m_Cmd,Event.m_Seq); } else { if(1)///////////////?????????????????????????? z在那个线程循环时才发的??????to do。。。。。 { // 1.先添加把异步Affair的同步成员初始完毕 ST_AFFAIR_CALLBACK AffairCallBack((long)this,NULL,NULL,-1); tr1::shared_ptr<CCachedAffair> pCachedAffairItem(new CCachedAffair(&AffairCallBack)); unsigned long long AffairId = CCachedAffairMap::Static_PushNewAffair_Pre(m_cachedMap, m_Lock , pCachedAffairItem, Event.m_Seq, Event.m_Cmd, Event.m_Cmd); ExCommand Head; Head.nSrcType = 0; Head.nCmdType = Event.m_Cmd; Head.nCmdSeq = Event.m_Seq; Head.nContentSize = 0; { //lock.... JtMutexAutoLock L(m_EventLock); m_AsynEventS.push_back(Event); } if(!InHandling) { res = pEventPairPipe->SendCmd((const char*)&Head, sizeof(Head)); if(res) { CCachedAffairMap::Static_CancelAffair_Pre(m_cachedMap, m_Lock, Event.m_Seq); jtprintf("[%s]Static_CancelAffair_Pre--------------------------------------\n", __FUNCTION__); return res; } } // 2.等待该同步事件 if(AffairCallBack.m_pOnGotData==NULL) { res = CCachedAffairMap::Static_PushNewAffair(m_cachedMap, m_Lock , pCachedAffairItem, Event.m_Seq); } } } if(res) jtprintf("[%s]res-------------------%d-------------------\n", __FUNCTION__, res); return res; }
void *FiberBase::fiber_proc(void *p) { Isolate* isolate = Isolate::now(); v8::Locker locker(isolate->m_isolate); v8::Isolate::Scope isolate_scope(isolate->m_isolate); v8::HandleScope handle_scope(isolate->m_isolate); v8::Context::Scope context_scope( v8::Local<v8::Context>::New(isolate->m_isolate, isolate->m_context)); s_idleFibers --; while (1) { AsyncEvent *ae; if ((ae = g_jobs.tryget()) == NULL) { s_idleFibers ++; if (s_idleFibers > g_spareFibers) { s_idleFibers --; break; } { v8::Unlocker unlocker(isolate->m_isolate); ae = g_jobs.get(); } s_idleFibers --; } { v8::HandleScope handle_scope(isolate->m_isolate); ae->js_invoke(); } } s_fibers --; return NULL; }
result_t TcpServer::asyncRun() { class asyncCall : public AsyncState { public: asyncCall(TcpServer* pThis) : AsyncState(NULL) , m_pThis(pThis) { set(accept); } public: static int32_t accept(AsyncState* pState, int32_t n) { asyncCall* pThis = (asyncCall*)pState; return pThis->m_pThis->run(pThis); } private: obj_ptr<TcpServer> m_pThis; }; if (!m_socket) return CHECK_ERROR(CALL_E_INVALID_CALL); if (m_running) return CHECK_ERROR(CALL_E_INVALID_CALL); AsyncEvent* ac = new asyncCall(this); obj_ptr<Holder> h = new Holder(); h->setJSObject(); h->m_server.Reset(holder()->m_isolate, wrap()); ac->m_ctx.resize(1); ac->m_ctx[0] = h; ac->apost(0); return 0; }
void Service::yield() { AsyncEvent ae; m_yieldList.put(&ae); ae.wait(); }