task.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <stdint.h>
  20. #include <kern/atomic.h>
  21. #include <kern/bulletin.h>
  22. #include <kern/cspace_types.h>
  23. #include <kern/init.h>
  24. #include <kern/kuid.h>
  25. #include <kern/list.h>
  26. #include <kern/log.h>
  27. #include <kern/spinlock.h>
  28. #include <kern/stream.h>
  29. #include <kern/thread.h>
  30. #include <vm/map.h>
  31. // Task name buffer size.
  32. #define TASK_NAME_SIZE 32
  33. // Task structure.
  34. struct task
  35. {
  36. struct kuid_head kuid;
  37. struct spinlock lock;
  38. struct list node;
  39. struct list threads;
  40. struct vm_map *map;
  41. struct cspace caps;
  42. struct bulletin dead_subs;
  43. char name[TASK_NAME_SIZE];
  44. };
  45. // Task IPC message (TODO: Move to a specific header).
  46. struct task_ipc_msg
  47. {
  48. uint32_t size;
  49. int op;
  50. union
  51. {
  52. char name[TASK_NAME_SIZE];
  53. int id;
  54. };
  55. };
  56. // Task IPC operations.
  57. enum
  58. {
  59. TASK_IPC_GET_NAME,
  60. TASK_IPC_SET_NAME,
  61. TASK_IPC_GET_ID,
  62. };
  63. static inline struct task*
  64. task_get_kernel_task (void)
  65. {
  66. extern struct task task_kernel_task;
  67. return (&task_kernel_task);
  68. }
  69. static inline uint32_t
  70. task_get_kuid (const struct task *task)
  71. {
  72. return (task->kuid.id);
  73. }
  74. static inline void
  75. task_ref (struct task *task)
  76. {
  77. size_t nr_refs = atomic_add_rlx (&task->kuid.nr_refs, 1);
  78. assert (nr_refs != (size_t)-1);
  79. }
  80. void task_destroy (struct task *task);
  81. static inline void
  82. task_unref (struct task *task)
  83. {
  84. size_t nr_refs = atomic_sub_acq_rel (&task->kuid.nr_refs, 1);
  85. assert (nr_refs);
  86. if (nr_refs == 1)
  87. task_destroy (task);
  88. }
  89. static inline struct vm_map*
  90. task_get_vm_map (const struct task *task)
  91. {
  92. return (task->map);
  93. }
  94. static inline struct task*
  95. task_self (void)
  96. {
  97. return (thread_self()->xtask);
  98. }
  99. static inline int
  100. task_id (const struct task *task)
  101. {
  102. return ((int)task->kuid.id);
  103. }
  104. // Create a task.
  105. int task_create (struct task **taskp, const char *name);
  106. /*
  107. * Look up a task from its name.
  108. *
  109. * If a task is found, it gains a reference. Otherwise, NULL is returned.
  110. *
  111. * This function is meant for debugging only.
  112. */
  113. struct task* task_lookup (const char *name);
  114. // Add a thread to a task.
  115. void task_add_thread (struct task *task, struct thread *thread);
  116. // Remove a thread from a task.
  117. void task_remove_thread (struct task *task, struct thread *thread);
  118. /*
  119. * Look up a thread in a task from its name.
  120. *
  121. * If a thread is found, it gains a reference, Otherwise, NULL is returned.
  122. *
  123. * This function is meant for debugging only.
  124. */
  125. struct thread* task_lookup_thread (struct task *task, const char *name);
  126. // Look up a task by its KUID.
  127. static inline struct task*
  128. task_by_kuid (uint32_t kuid)
  129. {
  130. return (kuid_find_type (kuid, struct task, kuid, KUID_TASK));
  131. }
  132. /*
  133. * Display task information.
  134. *
  135. * If task is NULL, this function displays all tasks.
  136. */
  137. void task_info (struct task *task, struct stream *stream);
  138. // Handle an IPC message on a task capability.
  139. struct cap_iters;
  140. struct ipc_msg_data;
  141. ssize_t task_handle_msg (struct task *task, struct cap_iters *src,
  142. struct cap_iters *dst, struct ipc_msg_data *data);
  143. /*
  144. * This init operation provides :
  145. * - task creation
  146. * - module fully initialized
  147. */
  148. INIT_OP_DECLARE (task_setup);
  149. #endif