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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <ipxe/list.h>
  25. #include <ipxe/init.h>
  26. #include <ipxe/process.h>
  27. /** @file
  28. *
  29. * Processes
  30. *
  31. * We implement a trivial form of cooperative multitasking, in which
  32. * all processes share a single stack and address space.
  33. */
  34. /** Process run queue */
  35. static LIST_HEAD ( run_queue );
  36. /**
  37. * Get pointer to object containing process
  38. *
  39. * @v process Process
  40. * @ret object Containing object
  41. */
  42. void * process_object ( struct process *process ) {
  43. return ( ( ( void * ) process ) - process->desc->offset );
  44. }
  45. /**
  46. * Add process to process list
  47. *
  48. * @v process Process
  49. *
  50. * It is safe to call process_add() multiple times; further calls will
  51. * have no effect.
  52. */
  53. void process_add ( struct process *process ) {
  54. if ( ! process_running ( process ) ) {
  55. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  56. " starting\n", PROC_DBG ( process ) );
  57. ref_get ( process->refcnt );
  58. list_add_tail ( &process->list, &run_queue );
  59. } else {
  60. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  61. " already started\n", PROC_DBG ( process ) );
  62. }
  63. }
  64. /**
  65. * Remove process from process list
  66. *
  67. * @v process Process
  68. *
  69. * It is safe to call process_del() multiple times; further calls will
  70. * have no effect.
  71. */
  72. void process_del ( struct process *process ) {
  73. if ( process_running ( process ) ) {
  74. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  75. " stopping\n", PROC_DBG ( process ) );
  76. list_del ( &process->list );
  77. INIT_LIST_HEAD ( &process->list );
  78. ref_put ( process->refcnt );
  79. } else {
  80. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  81. " already stopped\n", PROC_DBG ( process ) );
  82. }
  83. }
  84. /**
  85. * Single-step a single process
  86. *
  87. * This executes a single step of the first process in the run queue,
  88. * and moves the process to the end of the run queue.
  89. */
  90. void step ( void ) {
  91. struct process *process;
  92. struct process_descriptor *desc;
  93. void *object;
  94. if ( ( process = list_first_entry ( &run_queue, struct process,
  95. list ) ) ) {
  96. ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
  97. desc = process->desc;
  98. object = process_object ( process );
  99. if ( desc->reschedule ) {
  100. list_del ( &process->list );
  101. list_add_tail ( &process->list, &run_queue );
  102. } else {
  103. process_del ( process );
  104. }
  105. DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
  106. " executing\n", PROC_DBG ( process ) );
  107. desc->step ( object );
  108. DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
  109. " finished executing\n", PROC_DBG ( process ) );
  110. ref_put ( process->refcnt ); /* Allow destruction */
  111. }
  112. }
  113. /**
  114. * Initialise processes
  115. *
  116. */
  117. static void init_processes ( void ) {
  118. struct process *process;
  119. for_each_table_entry ( process, PERMANENT_PROCESSES )
  120. process_add ( process );
  121. }
  122. /** Process initialiser */
  123. struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
  124. .initialise = init_processes,
  125. };