Bladeren bron

Update: 最大タスク数を32に変更。最大タスク数の拡張を可能に

Satoshi Yoneda 1 maand geleden
bovenliggende
commit
b687b1df46
3 gewijzigde bestanden met toevoegingen van 15 en 11 verwijderingen
  1. 5 3
      tinykernel/include/tinykernel/kernel.hpp
  2. 6 6
      tinykernel/include/tinykernel/sync.hpp
  3. 4 2
      tinykernel/src/scheduler.cpp

+ 5 - 3
tinykernel/include/tinykernel/kernel.hpp

@@ -15,9 +15,9 @@
 #define TK_TIME_SLICE_MS 10u
 #endif
 
-// Total number of task slots (slot 0 is reserved for the built-in idle task).
+// Total number of task slots (slot 0 is reserved for core 0 idle, slot 1 for core 1 idle).
 #ifndef TK_MAX_TASKS
-#define TK_MAX_TASKS 16u
+#define TK_MAX_TASKS 32u
 #endif
 
 // Stack size per task, in bytes. Stacks come from a static pool (no malloc).
@@ -75,6 +75,8 @@ public:
     PreemptGuard& operator=(const PreemptGuard&) = delete;
 };
 
+using task_mask_t = uint32_t;
+
 namespace detail {
 void static_register(const char* name, TaskFn fn, uint32_t prio);
 uint32_t sched_lock();
@@ -82,7 +84,7 @@ void sched_unlock(uint32_t save);
 void block_current_task_locked();
 void wake_task_locked(TaskHandle handle);
 uint8_t current_task_slot();
-uint8_t find_highest_prio_waiter(uint16_t wait_mask);
+uint8_t find_highest_prio_waiter(task_mask_t wait_mask);
 }  // namespace detail
 
 }  // namespace tk

+ 6 - 6
tinykernel/include/tinykernel/sync.hpp

@@ -18,7 +18,7 @@ public:
         uint32_t save = detail::sched_lock();
         while (count_ <= 0) {
             uint8_t slot = detail::current_task_slot();
-            wait_mask_ |= (1u << slot);
+            wait_mask_ |= (task_mask_t{1} << slot);
             detail::block_current_task_locked();
             detail::sched_unlock(save);
             // 他タスクから release() されて再開したら再チェック
@@ -49,7 +49,7 @@ public:
         if (wait_mask_ != 0) {
             uint8_t slot = detail::find_highest_prio_waiter(wait_mask_);
             if (slot != 0xFF) {
-                wait_mask_ &= ~(1u << slot);
+                wait_mask_ &= ~(task_mask_t{1} << slot);
                 detail::wake_task_locked(TaskHandle{slot});
             }
         }
@@ -66,7 +66,7 @@ public:
 private:
     int32_t count_;
     int32_t max_count_;
-    uint16_t wait_mask_{0};
+    task_mask_t wait_mask_{0};
 };
 
 using BinarySemaphore = Semaphore;
@@ -83,7 +83,7 @@ public:
         uint32_t save = detail::sched_lock();
         uint8_t my_slot = detail::current_task_slot();
         while (locked_) {
-            wait_mask_ |= (1u << my_slot);
+            wait_mask_ |= (task_mask_t{1} << my_slot);
             detail::block_current_task_locked();
             detail::sched_unlock(save);
             save = detail::sched_lock();
@@ -118,7 +118,7 @@ public:
             if (wait_mask_ != 0) {
                 uint8_t slot = detail::find_highest_prio_waiter(wait_mask_);
                 if (slot != 0xFF) {
-                    wait_mask_ &= ~(1u << slot);
+                    wait_mask_ &= ~(task_mask_t{1} << slot);
                     detail::wake_task_locked(TaskHandle{slot});
                 }
             }
@@ -136,7 +136,7 @@ public:
 private:
     bool locked_{false};
     uint8_t owner_slot_{0xFF};
-    uint16_t wait_mask_{0};
+    task_mask_t wait_mask_{0};
 };
 
 // ===========================================================================

+ 4 - 2
tinykernel/src/scheduler.cpp

@@ -343,11 +343,13 @@ uint8_t tk::detail::current_task_slot() {
     return g_cur[get_core_num()];
 }
 
-uint8_t tk::detail::find_highest_prio_waiter(uint16_t wait_mask) {
+static_assert(TK_MAX_TASKS <= sizeof(tk::task_mask_t) * 8, "TK_MAX_TASKS must fit in task_mask_t");
+
+uint8_t tk::detail::find_highest_prio_waiter(task_mask_t wait_mask) {
     uint8_t best_slot = 0xFF;
     uint32_t best_prio = 0xFFFFFFFFu;
     for (uint32_t i = 0; i < TK_MAX_TASKS; ++i) {
-        if (wait_mask & (1u << i)) {
+        if (wait_mask & (task_mask_t{1} << i)) {
             if (g_slots[i].prio < best_prio) {
                 best_prio = g_slots[i].prio;
                 best_slot = (uint8_t)i;