osを作る その10

#概要

osを作る。
coopthreadを使ってみた。

#サンプルコード

```

#include "coop_threads.h"

#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
# define THREAD_STACK_SIZE 0x250U
#elif ARDUINO_ARCH_AVR
# define THREAD_STACK_SIZE 0x50U
#else
# define THREAD_STACK_SIZE 0
#endif

#if CONFIG_MAX_THREADS < 5
# error CONFIG_MAX_THREADS >= 5 is required
#endif

extern "C" void thrd_proc(void * arg) {
	int max_cnt = (int) (size_t) arg;
	char msg[16] = {};
	for (int i = 0; i < max_cnt; i++)
	{
		sprintf(msg, "%s: %d\n", coop_thread_name(), i + 1);
		Serial.print(msg);
		coop_yield();
	}
	sprintf(msg, "%s EXIT\n", coop_thread_name());
	Serial.print(msg);
}
void setup() {
	Serial.begin(9600);
	coop_sched_thread(thrd_proc, "thrd_1", THREAD_STACK_SIZE, (void *) 1);
	coop_sched_thread(thrd_proc, "thrd_2", THREAD_STACK_SIZE, (void *) 2);
	coop_sched_thread(thrd_proc, "thrd_3", THREAD_STACK_SIZE, (void *) 3);
	coop_sched_thread(thrd_proc, "thrd_4", THREAD_STACK_SIZE, (void *) 4);
	coop_sched_thread(thrd_proc, "thrd_5", THREAD_STACK_SIZE, (void *) 5);
	coop_sched_service();
	Serial.println("Threads 1-5 finished\n");
	coop_sched_thread(thrd_proc, "thrd_6", THREAD_STACK_SIZE, (void *) 6);
	coop_sched_thread(thrd_proc, "thrd_7", THREAD_STACK_SIZE, (void *) 5);
	coop_sched_thread(thrd_proc, "thrd_8", THREAD_STACK_SIZE, (void *) 4);
	coop_sched_thread(thrd_proc, "thrd_9", THREAD_STACK_SIZE, (void *) 3);
	coop_sched_thread(thrd_proc, "thrd_10", THREAD_STACK_SIZE, (void *) 2);
	coop_sched_service();
	Serial.println("Threads 6-10 finished\n");
	coop_sched_thread(thrd_proc, "thrd_11", THREAD_STACK_SIZE, (void *) 1);
	coop_sched_service();
	Serial.println("Thread 11 finished\n");
}
void loop() {
	static bool info = false;
	if (!info) 
	{
		Serial.println("All scheduled threads finished; loop() reached");
		info = true;
	}
	delay(1000);
}




```

#結果

```
thrd_1: 1
         thrd_2: 1
                  thrd_3: 1
                           thrd_4: 1
                                    thrd_5: 1
                                             thrd_1 EXIT
                                                        thrd_2: 2
                                                                 thrd_3: 2
                                                                          thrd_4: 2
   thrd_5: 2
            thrd_2 EXIT
                       thrd_3: 3
                                thrd_4: 3
                                         thrd_5: 3
                                                  thrd_3 EXIT
                                                             thrd_4: 4
                                                                      thrd_5: 4
                                                                               thrd_4 EXIT
          thrd_5: 5
                   thrd_5 EXIT
                              Threads 1-5 finished

thrd_6: 1
         thrd_7: 1
                  thrd_8: 1
                           thrd_9: 1
                                    thrd_10: 1
                                              thrd_6: 2
                                                       thrd_7: 2
                                                                thrd_8: 2
                                                                         thrd_9: 2
  thrd_10: 2
            thrd_6: 3
                     thrd_7: 3
                              thrd_8: 3
                                       thrd_9: 3
                                                thrd_10 EXIT
                                                            thrd_6: 4
                                                                     thrd_7: 4
                                                                              thrd_8: 4
       thrd_9 EXIT
                  thrd_6: 5
                           thrd_7: 5
                                    thrd_8 EXIT
                                               thrd_6: 6
                                                        thrd_7 EXIT
                                                                   thrd_6 EXIT
                                                                              Threads 6-10 finished

thrd_11: 1
          thrd_11 EXIT
                      Thread 11 finished

All scheduled threads finished; loop() reached


```

以上。