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.

process.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <ipxe/list.h>
  20. #include <ipxe/init.h>
  21. #include <ipxe/process.h>
  22. /** @file
  23. *
  24. * Processes
  25. *
  26. * We implement a trivial form of cooperative multitasking, in which
  27. * all processes share a single stack and address space.
  28. */
  29. /** Process run queue */
  30. static LIST_HEAD ( run_queue );
  31. /**
  32. * Add process to process list
  33. *
  34. * @v process Process
  35. *
  36. * It is safe to call process_add() multiple times; further calls will
  37. * have no effect.
  38. */
  39. void process_add ( struct process *process ) {
  40. if ( ! process_running ( process ) ) {
  41. DBGC ( process, "PROCESS %p (%p) starting\n",
  42. process, process->step );
  43. ref_get ( process->refcnt );
  44. list_add_tail ( &process->list, &run_queue );
  45. } else {
  46. DBGC ( process, "PROCESS %p (%p) already started\n",
  47. process, process->step );
  48. }
  49. }
  50. /**
  51. * Remove process from process list
  52. *
  53. * @v process Process
  54. *
  55. * It is safe to call process_del() multiple times; further calls will
  56. * have no effect.
  57. */
  58. void process_del ( struct process *process ) {
  59. if ( process_running ( process ) ) {
  60. DBGC ( process, "PROCESS %p (%p) stopping\n",
  61. process, process->step );
  62. list_del ( &process->list );
  63. INIT_LIST_HEAD ( &process->list );
  64. ref_put ( process->refcnt );
  65. } else {
  66. DBGC ( process, "PROCESS %p (%p) already stopped\n",
  67. process, process->step );
  68. }
  69. }
  70. /**
  71. * Single-step a single process
  72. *
  73. * This executes a single step of the first process in the run queue,
  74. * and moves the process to the end of the run queue.
  75. */
  76. void step ( void ) {
  77. struct process *process;
  78. if ( ( process = list_first_entry ( &run_queue, struct process,
  79. list ) ) ) {
  80. list_del ( &process->list );
  81. list_add_tail ( &process->list, &run_queue );
  82. ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
  83. DBGC2 ( process, "PROCESS %p (%p) executing\n",
  84. process, process->step );
  85. process->step ( process );
  86. DBGC2 ( process, "PROCESS %p (%p) finished executing\n",
  87. process, process->step );
  88. ref_put ( process->refcnt ); /* Allow destruction */
  89. }
  90. }
  91. /**
  92. * Initialise processes
  93. *
  94. */
  95. static void init_processes ( void ) {
  96. struct process *process;
  97. for_each_table_entry ( process, PERMANENT_PROCESSES )
  98. process_add ( process );
  99. }
  100. /** Process initialiser */
  101. struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
  102. .initialise = init_processes,
  103. };