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

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