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

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