task.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2012-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef KERN_TASK_H
  18. #define KERN_TASK_H
  19. #include <kern/atomic.h>
  20. #include <kern/init.h>
  21. #include <kern/list.h>
  22. #include <kern/log.h>
  23. #include <kern/spinlock.h>
  24. #include <kern/thread.h>
  25. #include <vm/vm_map.h>
  26. /*
  27. * Task name buffer size.
  28. */
  29. #define TASK_NAME_SIZE 32
  30. /*
  31. * Task structure.
  32. */
  33. struct task {
  34. unsigned long nr_refs;
  35. struct spinlock lock;
  36. struct list node;
  37. struct list threads;
  38. struct vm_map *map;
  39. char name[TASK_NAME_SIZE];
  40. };
  41. static inline struct task *
  42. task_get_kernel_task(void)
  43. {
  44. extern struct task task_kernel_task;
  45. return &task_kernel_task;
  46. }
  47. static inline void
  48. task_ref(struct task *task)
  49. {
  50. unsigned long nr_refs;
  51. nr_refs = atomic_fetch_add(&task->nr_refs, 1UL, ATOMIC_RELAXED);
  52. assert(nr_refs != (unsigned long)-1);
  53. }
  54. static inline void
  55. task_unref(struct task *task)
  56. {
  57. unsigned long nr_refs;
  58. nr_refs = atomic_fetch_sub(&task->nr_refs, 1UL, ATOMIC_ACQ_REL);
  59. assert(nr_refs != 0);
  60. if (nr_refs == 1) {
  61. /* TODO Task destruction */
  62. }
  63. }
  64. static inline struct vm_map *
  65. task_get_vm_map(const struct task *task)
  66. {
  67. return task->map;
  68. }
  69. /*
  70. * Create a task.
  71. */
  72. int task_create(struct task **taskp, const char *name);
  73. /*
  74. * Look up a task from its name.
  75. *
  76. * If a task is found, it gains a reference. Otherwise, NULL is returned.
  77. *
  78. * This function is meant for debugging only.
  79. */
  80. struct task * task_lookup(const char *name);
  81. /*
  82. * Add a thread to a task.
  83. */
  84. void task_add_thread(struct task *task, struct thread *thread);
  85. /*
  86. * Remove a thread from a task.
  87. */
  88. void task_remove_thread(struct task *task, struct thread *thread);
  89. /*
  90. * Look up a thread in a task from its name.
  91. *
  92. * If a thread is found, it gains a reference, Otherwise, NULL is returned.
  93. *
  94. * This function is meant for debugging only.
  95. */
  96. struct thread * task_lookup_thread(struct task *task, const char *name);
  97. /*
  98. * Display task information.
  99. *
  100. * If task is NULL, this function displays all tasks.
  101. */
  102. void task_info(struct task *task, log_print_fn_t print_fn);
  103. /*
  104. * This init operation provides :
  105. * - task creation
  106. * - module fully initialized
  107. */
  108. INIT_OP_DECLARE(task_setup);
  109. #endif /* KERN_TASK_H */