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

Function _pthread_data_create

components/libc/posix/pthreads/pthread.c:109–142  ·  view source on GitHub ↗

* @brief Create and initialize a new thread data structure with index allocation * * This function allocates memory for a new thread data structure, initializes its default state, * and registers it in the global thread table. Uses spinlock synchronization to ensure safe * access to the global thread table in multi-threaded environments. * * @return Allocated index value of type pthread_t *

Source from the content-addressed store, hash-verified

107 * - Magic number validation (PTHREAD_MAGIC) for structure integrity
108 */
109pthread_t _pthread_data_create(void)
110{
111 int index;
112 _pthread_data_t *ptd = NULL;
113
114 ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
115 if (!ptd) return PTHREAD_NUM_MAX;
116
117 memset(ptd, 0x0, sizeof(_pthread_data_t));
118 ptd->canceled = 0;
119 ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
120 ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
121 ptd->magic = PTHREAD_MAGIC;
122
123 rt_hw_spin_lock(&pth_lock);
124 for (index = 0; index < PTHREAD_NUM_MAX; index ++)
125 {
126 if (pth_table[index] == NULL)
127 {
128 pth_table[index] = ptd;
129 break;
130 }
131 }
132 rt_hw_spin_unlock(&pth_lock);
133
134 /* full of pthreads, clean magic and release ptd */
135 if (index == PTHREAD_NUM_MAX)
136 {
137 ptd->magic = 0x0;
138 rt_free(ptd);
139 }
140
141 return index;
142}
143
144/**
145 * @brief Destroy thread local storage item at specified index

Callers 1

pthread_createFunction · 0.85

Calls 4

rt_mallocFunction · 0.85
rt_freeFunction · 0.85
rt_hw_spin_lockFunction · 0.50
rt_hw_spin_unlockFunction · 0.50

Tested by

no test coverage detected