1
0
mirror of https://github.com/azure-rtos/threadx synced 2025-02-06 08:08:27 +08:00

136 lines
6.2 KiB
C
Raw Normal View History

2024-01-29 13:51:15 +08:00
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
2020-09-30 15:42:41 -07:00
2021-06-02 06:45:05 +00:00
/**************************************************************************/
/**************************************************************************/
2020-09-30 15:42:41 -07:00
/** */
2021-06-02 06:45:05 +00:00
/** POSIX wrapper for THREADX */
/** */
/** */
/** */
/**************************************************************************/
/**************************************************************************/
2020-09-30 15:42:41 -07:00
2021-06-02 06:45:05 +00:00
/* Include necessary system files. */
2020-09-30 15:42:41 -07:00
2021-06-02 06:45:05 +00:00
#include "tx_api.h" /* Threadx API */
#include "pthread.h" /* Posix API */
#include "px_int.h" /* Posix helper functions */
2020-09-30 15:42:41 -07:00
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
2021-06-02 06:45:05 +00:00
/* pthread_mutex_init PORTABLE C */
2021-07-28 07:24:02 +00:00
/* 6.1.7 */
2020-09-30 15:42:41 -07:00
/* AUTHOR */
/* */
2021-06-02 06:45:05 +00:00
/* William E. Lamie, Microsoft Corporation */
2020-09-30 15:42:41 -07:00
/* */
/* DESCRIPTION */
/* */
2021-06-02 06:45:05 +00:00
/* This function shall init the mutex object referenced by mutex with */
/* attributes specified by attr. */
/* If attr is NULL, the default mutex attributes are used; the effect */
/* shall be the same as passing the address of a default mutex */
/* attributes object. Upon successful initialization,the state of the */
/* mutex becomes initialized and unlocked. */
/* */
2020-09-30 15:42:41 -07:00
/* INPUT */
2021-06-02 06:45:05 +00:00
/* */
/* mutex Pointer to a pthread mutex object */
/* attr Pointer to mutex attributes */
2020-09-30 15:42:41 -07:00
/* */
/* OUTPUT */
/* */
2021-06-02 06:45:05 +00:00
/* 0 If successful */
/* Value In case of any error */
2020-09-30 15:42:41 -07:00
/* */
/* CALLS */
/* */
2021-06-02 06:45:05 +00:00
/* posix_internal_error In case of some special errors */
/* posix_in_thread_context Check whether called from a thread */
/* tx_mutex_create Create a ThreadX Mutex object */
2020-09-30 15:42:41 -07:00
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
2021-07-28 07:24:02 +00:00
/* 06-02-2021 William E. Lamie Initial Version 6.1.7 */
2020-09-30 15:42:41 -07:00
/* */
/**************************************************************************/
2021-06-02 06:45:05 +00:00
INT pthread_mutex_init(pthread_mutex_t *mutex ,pthread_mutexattr_t *attr)
2020-09-30 15:42:41 -07:00
{
2021-06-02 06:45:05 +00:00
TX_INTERRUPT_SAVE_AREA
TX_MUTEX *mutex_ptr;
ULONG status,retval;
/* Make sure we're calling this routine from a thread context. */
if (!posix_in_thread_context())
{
/* return POSIX error. */
posix_internal_error(444);
}
/* Disable interrupts. */
TX_DISABLE
/* Check for any pthread_mutexattr_t suggested */
if (!attr)
{
/* no attributes passed so assume default attributes */
attr = &(posix_default_mutex_attr);
}
else
{
/* attributes passed , check for validity */
if (( (attr->in_use) == TX_FALSE)|| (attr->type!=PTHREAD_MUTEX_RECURSIVE))
{
/* attributes passed is not valid, return with an error */
/* Restore interrupts. */
TX_RESTORE
posix_errno = EINVAL;
posix_set_pthread_errno(EINVAL);
return(EINVAL);
}
}
mutex_ptr = (&(mutex->mutex_info)) ;
/* Now actually create the mutex */
status = tx_mutex_create(mutex_ptr, "PMTX", TX_INHERIT);
if ( status == TX_SUCCESS)
{
mutex->in_use = TX_TRUE;
retval = OK;
}
else
{
mutex->in_use = TX_FALSE;
posix_errno = EINVAL;
posix_set_pthread_errno(EINVAL);
retval = EINVAL;
}
TX_RESTORE
return(retval);
2020-09-30 15:42:41 -07:00
}