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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <gpxe/list.h>
  19. #include <gpxe/init.h>
  20. #include <gpxe/process.h>
  21. /** @file
  22. *
  23. * Processes
  24. *
  25. * We implement a trivial form of cooperative multitasking, in which
  26. * all processes share a single stack and address space.
  27. */
  28. /** Process run queue */
  29. static LIST_HEAD ( run_queue );
  30. /** Registered permanent processes */
  31. static struct process processes[0]
  32. __table_start ( struct process, processes );
  33. static struct process processes_end[0]
  34. __table_end ( struct process, processes );
  35. /**
  36. * Add process to process list
  37. *
  38. * @v process Process
  39. */
  40. void process_add ( struct process *process ) {
  41. DBGC ( process, "PROCESS %p starting\n", process );
  42. ref_get ( process->refcnt );
  43. list_add_tail ( &process->list, &run_queue );
  44. }
  45. /**
  46. * Remove process from process list
  47. *
  48. * @v process Process
  49. *
  50. * It is safe to call process_del() multiple times; further calls will
  51. * have no effect.
  52. */
  53. void process_del ( struct process *process ) {
  54. if ( ! list_empty ( &process->list ) ) {
  55. DBGC ( process, "PROCESS %p stopping\n", process );
  56. list_del ( &process->list );
  57. INIT_LIST_HEAD ( &process->list );
  58. ref_put ( process->refcnt );
  59. } else {
  60. DBGC ( process, "PROCESS %p already stopped\n", process );
  61. }
  62. }
  63. /**
  64. * Single-step a single process
  65. *
  66. * This executes a single step of the first process in the run queue,
  67. * and moves the process to the end of the run queue.
  68. */
  69. void step ( void ) {
  70. struct process *process;
  71. list_for_each_entry ( process, &run_queue, list ) {
  72. list_del ( &process->list );
  73. list_add_tail ( &process->list, &run_queue );
  74. DBGC2 ( process, "PROCESS %p executing\n", process );
  75. process->step ( process );
  76. DBGC2 ( process, "PROCESS %p finished executing\n", process );
  77. break;
  78. }
  79. }
  80. /**
  81. * Initialise processes
  82. *
  83. */
  84. static void init_processes ( void ) {
  85. struct process *process;
  86. for ( process = processes ; process < processes_end ; process++ ) {
  87. process_add ( process );
  88. }
  89. }
  90. /** Process initialiser */
  91. struct init_fn process_init_fn __init_fn ( INIT_NORMAL ) = {
  92. .initialise = init_processes,
  93. };