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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <gpxe/list.h>
  20. #include <gpxe/init.h>
  21. #include <gpxe/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 ( list_empty ( &process->list ) ) {
  41. DBGC ( process, "PROCESS %p starting\n", process );
  42. ref_get ( process->refcnt );
  43. list_add_tail ( &process->list, &run_queue );
  44. } else {
  45. DBGC ( process, "PROCESS %p already started\n", process );
  46. }
  47. }
  48. /**
  49. * Remove process from process list
  50. *
  51. * @v process Process
  52. *
  53. * It is safe to call process_del() multiple times; further calls will
  54. * have no effect.
  55. */
  56. void process_del ( struct process *process ) {
  57. if ( ! list_empty ( &process->list ) ) {
  58. DBGC ( process, "PROCESS %p stopping\n", process );
  59. list_del ( &process->list );
  60. INIT_LIST_HEAD ( &process->list );
  61. ref_put ( process->refcnt );
  62. } else {
  63. DBGC ( process, "PROCESS %p already stopped\n", process );
  64. }
  65. }
  66. /**
  67. * Single-step a single process
  68. *
  69. * This executes a single step of the first process in the run queue,
  70. * and moves the process to the end of the run queue.
  71. */
  72. void step ( void ) {
  73. struct process *process;
  74. list_for_each_entry ( process, &run_queue, list ) {
  75. list_del ( &process->list );
  76. list_add_tail ( &process->list, &run_queue );
  77. DBGC2 ( process, "PROCESS %p executing\n", process );
  78. process->step ( process );
  79. DBGC2 ( process, "PROCESS %p finished executing\n", process );
  80. break;
  81. }
  82. }
  83. /**
  84. * Initialise processes
  85. *
  86. */
  87. static void init_processes ( void ) {
  88. struct process *process;
  89. for_each_table_entry ( process, PERMANENT_PROCESSES )
  90. process_add ( process );
  91. }
  92. /** Process initialiser */
  93. struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
  94. .initialise = init_processes,
  95. };