int swTimer_init(long msec) { if (SwooleGS->start && (swIsMaster() || swIsManager())) { swWarn("cannot use timer in master and manager process."); return SW_ERR; } if (swReactorTimer_now(&SwooleG.timer.basetime) < 0) { return SW_ERR; } SwooleG.timer._current_id = -1; SwooleG.timer._next_msec = msec; SwooleG.timer._next_id = 1; SwooleG.timer.heap = swHeap_new(1024, SW_MIN_HEAP); if (!SwooleG.timer.heap) { return SW_ERR; } if (swIsTaskWorker()) { swSystemTimer_init(msec, SwooleG.use_timer_pipe); } else { swReactorTimer_init(msec); } return SW_OK; }
static sw_inline int64_t swTimer_get_relative_msec() { struct timeval now; if (swReactorTimer_now(&now) < 0) { return SW_ERR; } int64_t msec1 = (now.tv_sec - SwooleG.timer.basetime.tv_sec) * 1000; int64_t msec2 = (now.tv_usec - SwooleG.timer.basetime.tv_usec) / 1000; return msec1 + msec2; }