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

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