lwp_pid.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-02-23 Jesven first version.
  9. */
  10. #ifndef LWP_PID_H__
  11. #define LWP_PID_H__
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include <rtthread.h>
  16. #define LWP_CREATE_FLAG_NONE 0x0000
  17. #define LWP_CREATE_FLAG_ALLOC_PID 0x0001 /* allocate pid on lwp object create */
  18. #define LWP_CREATE_FLAG_INIT_USPACE 0x0002 /* do user space initialization */
  19. #define LWP_CREATE_FLAG_NOTRACE_EXEC 0x0004 /* not trace if execve() after fork() */
  20. struct rt_lwp;
  21. struct lwp_avl_struct *lwp_get_pid_ary(void);
  22. int lwp_pid_init(void);
  23. int lwp_pid_wait_for_empty(int wait_flags, rt_tick_t to);
  24. int lwp_pid_for_each(int (*cb)(pid_t pid, void *data), void *data);
  25. void lwp_pid_put(struct rt_lwp *lwp);
  26. void lwp_pid_lock_take(void);
  27. void lwp_pid_lock_release(void);
  28. /**
  29. * @brief Create a new lwp object
  30. * This will initialize the member in the object and register to system.
  31. * Besides, a new pid is allocate with lwp
  32. *
  33. * @param flags control the property of the lwp object. Can be ORed with:
  34. * LWP_CREATE_FLAG_NONE: raw lwp object
  35. * LWP_CREATE_FLAG_ALLOC_PID: lwp object with specified pid
  36. *
  37. * @return struct rt_lwp* object
  38. */
  39. struct rt_lwp* lwp_create(rt_base_t flags);
  40. void lwp_free(struct rt_lwp* lwp);
  41. int lwp_ref_inc(struct rt_lwp *lwp);
  42. int lwp_ref_dec(struct rt_lwp *lwp);
  43. struct rt_lwp* lwp_from_pid_raw_locked(pid_t pid);
  44. struct rt_lwp* lwp_from_pid_locked(pid_t pid);
  45. pid_t lwp_to_pid(struct rt_lwp* lwp);
  46. pid_t lwp_name2pid(const char* name);
  47. char* lwp_pid2name(int32_t pid);
  48. int lwp_getpid(void);
  49. /**
  50. * @brief Resource usage statistics structure
  51. *
  52. * @note The structure tracks various system resource usage statistics for a process,
  53. * including CPU time, memory usage, I/O operations, and context switches.
  54. * It follows the traditional Unix rusage structure format.
  55. */
  56. struct rusage
  57. {
  58. struct timeval ru_utime; /**< User CPU time */
  59. struct timeval ru_stime; /**< System CPU time */
  60. long ru_maxrss; /**< Maximum resident set size */
  61. long ru_ixrss; /**< Integral shared memory size */
  62. long ru_idrss; /**< Integral unshared data size */
  63. long ru_isrss; /**< Integral unshared stack size */
  64. long ru_minflt; /**< Page reclaims (soft page faults) */
  65. long ru_majflt; /**< Page faults (hard page faults) */
  66. long ru_nswap; /**< Swaps */
  67. long ru_inblock; /**< Block input operations */
  68. long ru_oublock; /**< Block output operations */
  69. long ru_msgsnd; /**< IPC messages sent */
  70. long ru_msgrcv; /**< IPC messages received */
  71. long ru_nsignals; /**< Signals received */
  72. long ru_nvcsw; /**< voluntary context switches */
  73. long ru_nivcsw; /**< involuntary context switches */
  74. long reserved[16]; /**< Reserved for future use */
  75. };
  76. pid_t lwp_waitpid(const pid_t pid, int *status, int options, struct rusage *ru);
  77. rt_err_t lwp_waitpid_kick(struct rt_lwp *parent, struct rt_lwp *self_lwp);
  78. pid_t waitpid(pid_t pid, int *status, int options);
  79. long list_process(void);
  80. void lwp_user_object_lock_init(struct rt_lwp *lwp);
  81. void lwp_user_object_lock_destroy(struct rt_lwp *lwp);
  82. void lwp_user_object_lock(struct rt_lwp *lwp);
  83. void lwp_user_object_unlock(struct rt_lwp *lwp);
  84. int lwp_user_object_add(struct rt_lwp *lwp, rt_object_t object);
  85. rt_err_t lwp_user_object_delete(struct rt_lwp *lwp, rt_object_t object);
  86. void lwp_user_object_clear(struct rt_lwp *lwp);
  87. void lwp_user_object_dup(struct rt_lwp *dst_lwp, struct rt_lwp *src_lwp);
  88. rt_inline struct rt_lwp *lwp_from_pid_and_lock(pid_t pid)
  89. {
  90. struct rt_lwp *lwp;
  91. lwp_pid_lock_take();
  92. lwp = lwp_from_pid_locked(pid);
  93. if (lwp)
  94. lwp_ref_inc(lwp);
  95. lwp_pid_lock_release();
  96. return lwp;
  97. }
  98. rt_inline void lwp_from_pid_release_lock(struct rt_lwp *lwp)
  99. {
  100. if (lwp)
  101. lwp_ref_dec(lwp);
  102. }
  103. typedef rt_base_t lwp_status_t;
  104. void lwp_thread_exit(rt_thread_t thread, int status);
  105. void lwp_exit(struct rt_lwp *lwp, lwp_status_t status);
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif