MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / lwp_tid_get

Function lwp_tid_get

components/lwp/lwp_tid.c:64–116  ·  view source on GitHub ↗

* @brief Allocates a thread ID (TID) from available resources * * @return int The allocated thread ID, or 0 if no TID available (with warning log) * * @note This function performs thread-safe allocation of a TID by: * 1. First checking the free list of available TIDs * 2. If none available, allocating from the TID array if space remains * 3. Performing a two-phase search f

Source from the content-addressed store, hash-verified

62 * 4. Inserting the allocated TID into the AVL tree for tracking
63 */
64int lwp_tid_get(void)
65{
66 struct lwp_avl_struct *p;
67 int tid = 0;
68
69 lwp_mutex_take_safe(&tid_lock, RT_WAITING_FOREVER, 0);
70 p = lwp_tid_free_head;
71 if (p)
72 {
73 lwp_tid_free_head = (struct lwp_avl_struct *)p->avl_right;
74 }
75 else if (lwp_tid_ary_alloced < LWP_TID_MAX_NR)
76 {
77 p = lwp_tid_ary + lwp_tid_ary_alloced;
78 lwp_tid_ary_alloced++;
79 }
80 if (p)
81 {
82 int found_noused = 0;
83
84 RT_ASSERT(p->data == RT_NULL);
85 for (tid = current_tid + 1; tid < TID_MAX; tid++)
86 {
87 if (!lwp_avl_find(tid, lwp_tid_root))
88 {
89 found_noused = 1;
90 break;
91 }
92 }
93 if (!found_noused)
94 {
95 for (tid = 1; tid <= current_tid; tid++)
96 {
97 if (!lwp_avl_find(tid, lwp_tid_root))
98 {
99 found_noused = 1;
100 break;
101 }
102 }
103 }
104 p->avl_key = tid;
105 lwp_avl_insert(p, &lwp_tid_root);
106 current_tid = tid;
107 }
108 lwp_mutex_release_safe(&tid_lock);
109
110 if (tid <= 0)
111 {
112 LOG_W("resource TID exhausted.");
113 }
114
115 return tid;
116}
117
118/**
119 * @brief Releases a thread ID (TID) and associated resources

Callers 4

lwp_execveFunction · 0.85
sys_thread_createFunction · 0.85
_sys_cloneFunction · 0.85
_sys_forkFunction · 0.85

Calls 4

lwp_mutex_take_safeFunction · 0.85
lwp_avl_findFunction · 0.85
lwp_avl_insertFunction · 0.85
lwp_mutex_release_safeFunction · 0.85

Tested by

no test coverage detected