您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

process.c 3.6KB

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