You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

2 years ago
  1. #include "zos_schduler.hpp"
  2. #include "ticket.hpp"
  3. using namespace std;
  4. using namespace iflytop;
  5. void ZOSSchduler::regPeriodJob(function<void(Context& context)> job, uint32_t period_ms) {
  6. PeriodJob* periodJob = new PeriodJob(job, period_ms);
  7. ZASSERT(periodJob != NULL);
  8. m_periodJobs.push_back(periodJob);
  9. }
  10. void ZOSSchduler::loop() {
  11. Context context;
  12. if (zos_haspassedms(ticket) < 1) {
  13. return;
  14. }
  15. ticket = zos_get_tick();
  16. for (auto iter = m_periodJobs.begin(); iter != m_periodJobs.end(); iter++) {
  17. auto periodJob = *iter;
  18. if ((uint32_t)zos_haspassedms2(periodJob->lastcall, ticket) < periodJob->period_ms) {
  19. continue;
  20. }
  21. context.periodJob = periodJob;
  22. periodJob->lastcall = ticket;
  23. periodJob->job(context);
  24. periodJob->schedule_times++;
  25. }
  26. }
  27. void ZOSSchduler::startSchedule(const char* threadname, int stack_size, osPriority priority) {
  28. m_thread.init(threadname, stack_size, priority);
  29. m_thread.run([this]() {
  30. while (true) {
  31. loop();
  32. osDelay(1);
  33. }
  34. });
  35. }