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

Function sem_init

components/libc/posix/ipc/semaphore.c:275–306  ·  view source on GitHub ↗

* @brief Initializes a POSIX semaphore. * @param sem Pointer to the semaphore structure to be initialized. * @param pshared Flag indicating whether the semaphore is to be shared between processes. * @param value Initial value of the semaphore. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error. * * @note This function initi

Source from the content-addressed store, hash-verified

273 * After successful initialization, the semaphore structure is inserted into the linked list of POSIX semaphores.
274 */
275int sem_init(sem_t *sem, int pshared, unsigned int value)
276{
277 char name[RT_NAME_MAX];
278 static rt_uint16_t psem_number = 0;
279
280 if (sem == RT_NULL)
281 {
282 rt_set_errno(EINVAL);
283
284 return -1;
285 }
286
287 rt_snprintf(name, sizeof(name), "psem%02d", psem_number++);
288 sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
289 if (sem->sem == RT_NULL)
290 {
291 rt_set_errno(ENOMEM);
292
293 return -1;
294 }
295
296 /* initialize posix semaphore */
297 sem->refcount = 1;
298 sem->unlinked = 0;
299 sem->unamed = 1;
300 /* lock posix semaphore list */
301 rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
302 posix_sem_insert(sem);
303 rt_sem_release(&posix_sem_lock);
304
305 return 0;
306}
307RTM_EXPORT(sem_init);
308
309/**

Callers 4

thread_createFunction · 0.85
initFunction · 0.85
test_threadFunction · 0.85

Calls 6

rt_set_errnoFunction · 0.85
rt_snprintfFunction · 0.85
rt_sem_createFunction · 0.85
rt_sem_takeFunction · 0.85
posix_sem_insertFunction · 0.85
rt_sem_releaseFunction · 0.85

Tested by 1

test_threadFunction · 0.68