/* * task.c * ---------------- * Simple cooperative tasking system. * * Copyright (c) 2017, NORDUnet A/S All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the NORDUnet nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Dead-simple fully-cooperative tasker. There are no priorities; tasks * are run in a strictly round-robin fashion. There is no preemption; * tasks explicitly yield control. Tasks are created at system init time, * and are expected to run an infinite loop; tasks do not return, nor are * tasks deleted. */ #include "stm-init.h" #include "task.h" /* Task Control Block. The structure is private, in case we want to change * it later without having to change the API. In any case, external code * shouldn't poke its fingers in the internal details. */ struct task_cb { struct task_cb *next; task_state_t state; char *name; funcp_t func; void *cookie; void *stack_base; size_t stack_len; void *stack_ptr; }; /* Number of tasks. Default is number of RPC dispatch tasks, plus CLI task. */ #ifndef MAX_TASK #ifdef NUM_RPC_TASK #define MAX_TASK (NUM_RPC_TASK + 2) #else #define MAX_TASK 6 #endif #endif static tcb_t tcbs[MAX_TASK]; static size_t num_task = 0; /* We have a circular list of tasks. New tasks are added at the tail, and * tail->next is the head. */ static tcb_t *tail = NULL; /* Currently running task */ static tcb_t *cur_task = NULL; #define STACK_GUARD_WORD 0x55AA5A5A #ifdef DO_TASK_METRICS static uint32_t tick_start = 0; static uint32_t tick_idle = 0; static uint32_t tick_max = 0; static uint32_t nyield = 0; #endif static uint32_t tick_prev = 0; #ifndef TASK_YIELD_THRESHOLD #define TASK_YIELD_THRESHOLD 100 #endif /* Add a task. */ tcb_t *task_add(char *name, funcp_t func, void *cookie, void *stack, size_t stack_len) { if (num_task >= MAX_TASK) return NULL; if (name == NULL || func == NULL || stack == NULL) return NULL; tcb_t *t = &tcbs[num_task++]; t->state = TASK_INIT; t->name = name; t->func = func; t->cookie = cookie; t->stack_base = stack; t->stack_len = stack_len; t->stack_ptr = stack + stack_len; for (uint32_t *p = (uint32_t *)t->stack_base; p < (uint32_t *)t->stack_ptr; ++p) *p = STACK_GUARD_WORD; if (tail == NULL) { /* Empty list; initialize it to this task. */ t->next = t; } else { /* Otherwise insert at the end of the list. */ t->next = tail->next; tail->next = t; } tail = t; return t; } /* Reinitalize the current task. * NOTE: This will destroy any state in the running task. * DO NOT CALL THIS UNLESS YOU ARE REALLY SURE THAT'S WHAT YOU WANT TO DO. */ void task_mod(char *name, funcp_t func, void *cookie) { tcb_t *t = cur_task; t->name = name; t->func = func; t->cookie = cookie; t->state = TASK_INIT; t->stack_ptr = t->stack_base + t->stack_len; for (uint32_t *p = (uint32_t *)t->stack_base; p < (uint32_t *)t->stack_ptr; ++p) *p = STACK_GUARD_WORD; __set_MSP((uint32_t)cur_task->stack_ptr); task_yield(); } /* Set the idle hook function pointer. * * This function is called repeatedly when the system is idle (there are * no runnable tasks). * * The idle function should NOT call task_delay or HAL_Delay, because that * will cause fatal recursion. We could add a recursion guard to * task_yield, but we're not currently using the idle hook, and I'm * thinking about removing it entirely. */ static void default_idle_hook(void) { } static funcp_t idle_hook = default_idle_hook; void task_set_idle_hook(funcp_t func) { idle_hook = (func == NULL) ? default_idle_hook : func; } /* Find the next runnable task. */ static tcb_t *next_task(void) { tcb_t *t; /* If the tasker isn't running yet, return the first task. */ if (cur_task == NULL) return (tail == NULL) ? NULL : tail->next; // XXX critical section? /* find the next runnable task */ for (t = cur_task->next; t != cur_task; t = t->next) { if (t->state != TASK_WAITING) return t; } /* searched all the way back to cur_task - is it runnable? */ return (cur_task->state == TASK_WAITING) ? NULL : cur_task; } /* Check for stack overruns. */ static void check_stack(tcb_t *t) { if (t->stack_ptr < t->stack_base || t->stack_ptr >= t->stack_base + t->stack_len || *(uint32_t *)t->stack_base != STACK_GUARD_WORD) Error_Handler(); } /* Yield control to the next runnable task. */ void task_yield(void) { tcb_t *next; /* If there are no defined tasks, exit immediately so we don't get * caught in the idle loop. */ if (tail == NULL) return; #ifdef DO_TASK_METRICS uint32_t tick0 = HAL_GetTick(); #endif /* Find the next runnable task. Loop if every task is waiting. */ while (1) { next = next_task(); if (next == NULL) idle_hook(); else break; } /* If we decide we don't need the idle hook, the preceding loop could * devolve to something like this: * * do { * next = next_task(); * } while (next == NULL); */ #ifdef DO_TASK_METRICS uint32_t tick = HAL_GetTick(); tick_idle += (tick - tick0); if (tick_start == 0) tick_start = tick; if (tick_prev != 0) { uint32_t duration = tick0 - tick_prev; if (duration > tick_max) tick_max = duration; } tick_prev = tick; ++nyield; #else tick_prev = HAL_GetTick(); #endif /* If there are no other runnable tasks (and cur_task is runnable), * we don't need to context-switch. */ if (next == cur_task && cur_task->state != TASK_INIT) return; /* Save current context, if there is one. */ if (cur_task != NULL && cur_task->state != TASK_INIT) { __asm("push {r0-r12, lr}"); cur_task->stack_ptr = (void *)__get_MSP(); /* Check for stack overruns. */ check_stack(cur_task); } cur_task = next; /* If task is in init state, call its entry point. */ if (cur_task->state == TASK_INIT) { __set_MSP((uint32_t)cur_task->stack_ptr); cur_task->state = TASK_READY; cur_task->func(); /*NOTREACHED*/ } /* Otherwise, restore the task's context. */ else { __set_MSP((uint32_t)cur_task->stack_ptr); __asm("pop {r0-r12, lr}"); return; } } /* Yield if it's been "too long" since the last yield. */ void task_yield_maybe(void) { if (HAL_GetTick() - tick_prev >= TASK_YIELD_THRESHOLD) task_yield(); } /* Put the current task to sleep (make it non-runnable). */ void task_sleep(void) { if (cur_task != NULL) cur_task->state = TASK_WAITING; task_yield(); } /* Wake a task (make it runnable). */ void task_wake(tcb_t *t) { if (t != NULL) t->state = TASK_READY; } /* Accessor functions */ tcb_t *task_get_tcb(void) { return cur_task; } char *task_get_name(tcb_t *t) { if (t == NULL) t = cur_task; return t->name; } funcp_t task_get_func(tcb_t *t) { if (t == NULL) t = cur_task; return t->func; } void *task_get_cookie(tcb_t *t) { if (t == NULL) t = cur_task; return t->cookie; } task_state_t task_get_state(tcb_t *t) { if (t == NULL) t = cur_task; return t->state; } void *task_get_stack(tcb_t *t) { if (t == NULL) t = cur_task; return t->stack_ptr; } /* stupid linear search for first non guard word */ size_t task_get_stack_highwater(tcb_t *t) { if (t == NULL) t = cur_task; const uint32_t * const b = (uint32_t *)t->stack_base; for (size_t i = 0; i < t->stack_len/4; ++i) { if (b[i] != STACK_GUARD_WORD) { return (t->stack_len - (i * 4)); } } return 0; } /* Iterate through tasks. * * Returns the next task control block, or NULL at the end of the list. */ tcb_t *task_iterate(tcb_t *t) { if (t == NULL) return (tail == NULL) ? NULL : tail->next; if (t == tail) return NULL; return t->next; } /* Delay a number of 1ms ticks. */ void task_delay(uint32_t delay) { uint32_t tickstart = HAL_GetTick(); while ((HAL_GetTick() - tickstart) < delay) task_yield(); } void HAL_Delay(uint32_t delay) __attribute__((alias("task_delay"))); /* Simple mutex-like locks. A real mutex would require the unlocker to be * the current owner, but then we have to define and return errors, when * all we want at the moment is simple mutual exclusion. */ void task_mutex_lock(task_mutex_t *mutex) { while (mutex->locked) task_yield(); mutex->locked = 1; } void task_mutex_unlock(task_mutex_t *mutex) { if (mutex != NULL) mutex->locked = 0; } #ifdef DO_TASK_METRICS void task_get_metrics(struct task_metrics *tm) { if (tm != NULL) { tm->avg.tv_sec = 0; tm->avg.tv_usec = (HAL_GetTick() - tick_start - tick_idle) * 1000 / nyield; if (tm->avg.tv_usec > 1000000) { tm->avg.tv_sec = tm->avg.tv_usec / 1000000; tm->avg.tv_usec = tm->avg.tv_usec % 1000000; } tm->max.tv_sec = tick_max / 1000; tm->max.tv_usec = (tick_max % 1000) * 1000; } } void task_reset_metrics(void) { tick_start = HAL_GetTick(); tick_prev = tick_idle = tick_max = nyield = 0; } #endif p;id=3d3f71cae431ec4e0c5df627c525bacc475e47d7'>3d3f71c
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840