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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef _IPXE_PROCESS_H
  2. #define _IPXE_PROCESS_H
  3. /** @file
  4. *
  5. * Processes
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <ipxe/list.h>
  10. #include <ipxe/refcnt.h>
  11. #include <ipxe/tables.h>
  12. /** A process */
  13. struct process {
  14. /** List of processes */
  15. struct list_head list;
  16. /** Process descriptor */
  17. struct process_descriptor *desc;
  18. /** Reference counter
  19. *
  20. * If this process is not part of a reference-counted object,
  21. * this field may be NULL.
  22. */
  23. struct refcnt *refcnt;
  24. };
  25. /** A process descriptor */
  26. struct process_descriptor {
  27. /** Offset of process within containing object */
  28. size_t offset;
  29. /**
  30. * Single-step the process
  31. *
  32. * This method should execute a single step of the process.
  33. * Returning from this method is isomorphic to yielding the
  34. * CPU to another process.
  35. */
  36. void ( * step ) ( void *object );
  37. /** Automatically reschedule the process */
  38. int reschedule;
  39. };
  40. /**
  41. * Define a process step() method
  42. *
  43. * @v object_type Implementing method's expected object type
  44. * @v step Implementing method
  45. * @ret step Process step method
  46. */
  47. #define PROC_STEP( object_type, step ) \
  48. ( ( ( ( typeof ( step ) * ) NULL ) == \
  49. ( ( void ( * ) ( object_type *object ) ) NULL ) ) ? \
  50. ( void ( * ) ( void *object ) ) step : \
  51. ( void ( * ) ( void *object ) ) step )
  52. /**
  53. * Calculate offset of process within containing object
  54. *
  55. * @v object_type Containing object data type
  56. * @v name Process name (i.e. field within object data type)
  57. * @ret offset Offset of process within containing object
  58. */
  59. #define process_offset( object_type, name ) \
  60. ( ( ( ( typeof ( ( ( object_type * ) NULL )->name ) * ) NULL ) \
  61. == ( ( struct process * ) NULL ) ) \
  62. ? offsetof ( object_type, name ) \
  63. : offsetof ( object_type, name ) )
  64. /**
  65. * Define a process descriptor
  66. *
  67. * @v object_type Containing object data type
  68. * @v process Process name (i.e. field within object data type)
  69. * @v step Process' step() method
  70. * @ret desc Object interface descriptor
  71. */
  72. #define PROC_DESC( object_type, process, _step ) { \
  73. .offset = process_offset ( object_type, process ), \
  74. .step = PROC_STEP ( object_type, _step ), \
  75. .reschedule = 1, \
  76. }
  77. /**
  78. * Define a process descriptor for a process that runs only once
  79. *
  80. * @v object_type Containing object data type
  81. * @v process Process name (i.e. field within object data type)
  82. * @v step Process' step() method
  83. * @ret desc Object interface descriptor
  84. */
  85. #define PROC_DESC_ONCE( object_type, process, _step ) { \
  86. .offset = process_offset ( object_type, process ), \
  87. .step = PROC_STEP ( object_type, _step ), \
  88. .reschedule = 0, \
  89. }
  90. /**
  91. * Define a process descriptor for a pure process
  92. *
  93. * A pure process is a process that does not have a containing object.
  94. *
  95. * @v step Process' step() method
  96. * @ret desc Object interface descriptor
  97. */
  98. #define PROC_DESC_PURE( _step ) { \
  99. .offset = 0, \
  100. .step = PROC_STEP ( struct process, _step ), \
  101. .reschedule = 1, \
  102. }
  103. extern void * __attribute__ (( pure ))
  104. process_object ( struct process *process );
  105. extern void process_add ( struct process *process );
  106. extern void process_del ( struct process *process );
  107. extern void step ( void );
  108. /**
  109. * Initialise process without adding to process list
  110. *
  111. * @v process Process
  112. * @v desc Process descriptor
  113. * @v refcnt Containing object reference count, or NULL
  114. */
  115. static inline __attribute__ (( always_inline )) void
  116. process_init_stopped ( struct process *process,
  117. struct process_descriptor *desc,
  118. struct refcnt *refcnt ) {
  119. INIT_LIST_HEAD ( &process->list );
  120. process->desc = desc;
  121. process->refcnt = refcnt;
  122. }
  123. /**
  124. * Initialise process and add to process list
  125. *
  126. * @v process Process
  127. * @v desc Process descriptor
  128. * @v refcnt Containing object reference count, or NULL
  129. */
  130. static inline __attribute__ (( always_inline )) void
  131. process_init ( struct process *process,
  132. struct process_descriptor *desc,
  133. struct refcnt *refcnt ) {
  134. process_init_stopped ( process, desc, refcnt );
  135. process_add ( process );
  136. }
  137. /**
  138. * Check if process is running
  139. *
  140. * @v process Process
  141. * @ret running Process is running
  142. */
  143. static inline __attribute__ (( always_inline )) int
  144. process_running ( struct process *process ) {
  145. return ( ! list_empty ( &process->list ) );
  146. }
  147. /** Permanent process table */
  148. #define PERMANENT_PROCESSES __table ( struct process, "processes" )
  149. /**
  150. * Declare a permanent process
  151. *
  152. * Permanent processes will be automatically added to the process list
  153. * at initialisation time.
  154. */
  155. #define __permanent_process __table_entry ( PERMANENT_PROCESSES, 01 )
  156. /** Define a permanent process
  157. *
  158. */
  159. #define PERMANENT_PROCESS( name, step ) \
  160. static struct process_descriptor name ## _desc = PROC_DESC_PURE ( step ); \
  161. struct process name __permanent_process = { \
  162. .list = LIST_HEAD_INIT ( name.list ), \
  163. .desc = & name ## _desc, \
  164. .refcnt = NULL, \
  165. };
  166. /**
  167. * Find debugging colourisation for a process
  168. *
  169. * @v process Process
  170. * @ret col Debugging colourisation
  171. *
  172. * Use as the first argument to DBGC() or equivalent macro.
  173. */
  174. #define PROC_COL( process ) process_object ( process )
  175. /** printf() format string for PROC_DBG() */
  176. #define PROC_FMT "%p+%zx"
  177. /**
  178. * printf() arguments for representing a process
  179. *
  180. * @v process Process
  181. * @ret args printf() argument list corresponding to PROC_FMT
  182. */
  183. #define PROC_DBG( process ) process_object ( process ), (process)->desc->offset
  184. #endif /* _IPXE_PROCESS_H */