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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * Get pointer to object containing process
  33. *
  34. * @v process Process
  35. * @ret object Containing object
  36. */
  37. void * process_object ( struct process *process ) {
  38. return ( ( ( void * ) process ) - process->desc->offset );
  39. }
  40. /**
  41. * Add process to process list
  42. *
  43. * @v process Process
  44. *
  45. * It is safe to call process_add() multiple times; further calls will
  46. * have no effect.
  47. */
  48. void process_add ( struct process *process ) {
  49. if ( ! process_running ( process ) ) {
  50. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  51. " starting\n", PROC_DBG ( process ) );
  52. ref_get ( process->refcnt );
  53. list_add_tail ( &process->list, &run_queue );
  54. } else {
  55. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  56. " already started\n", PROC_DBG ( process ) );
  57. }
  58. }
  59. /**
  60. * Remove process from process list
  61. *
  62. * @v process Process
  63. *
  64. * It is safe to call process_del() multiple times; further calls will
  65. * have no effect.
  66. */
  67. void process_del ( struct process *process ) {
  68. if ( process_running ( process ) ) {
  69. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  70. " stopping\n", PROC_DBG ( process ) );
  71. list_del ( &process->list );
  72. INIT_LIST_HEAD ( &process->list );
  73. ref_put ( process->refcnt );
  74. } else {
  75. DBGC ( PROC_COL ( process ), "PROCESS " PROC_FMT
  76. " already stopped\n", PROC_DBG ( process ) );
  77. }
  78. }
  79. /**
  80. * Single-step a single process
  81. *
  82. * This executes a single step of the first process in the run queue,
  83. * and moves the process to the end of the run queue.
  84. */
  85. void step ( void ) {
  86. struct process *process;
  87. struct process_descriptor *desc;
  88. void *object;
  89. if ( ( process = list_first_entry ( &run_queue, struct process,
  90. list ) ) ) {
  91. ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
  92. desc = process->desc;
  93. object = process_object ( process );
  94. if ( desc->reschedule ) {
  95. list_del ( &process->list );
  96. list_add_tail ( &process->list, &run_queue );
  97. } else {
  98. process_del ( process );
  99. }
  100. DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
  101. " executing\n", PROC_DBG ( process ) );
  102. desc->step ( object );
  103. DBGC2 ( PROC_COL ( process ), "PROCESS " PROC_FMT
  104. " finished executing\n", PROC_DBG ( process ) );
  105. ref_put ( process->refcnt ); /* Allow destruction */
  106. }
  107. }
  108. /**
  109. * Initialise processes
  110. *
  111. */
  112. static void init_processes ( void ) {
  113. struct process *process;
  114. for_each_table_entry ( process, PERMANENT_PROCESSES )
  115. process_add ( process );
  116. }
  117. /** Process initialiser */
  118. struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
  119. .initialise = init_processes,
  120. };