1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "pthread.h"
33 
34 
pthread_mutexattr_init(pthread_mutexattr_t *attr)35 int pthread_mutexattr_init(pthread_mutexattr_t *attr)
36 {
37     unsigned int ret = LOS_MuxAttrInit(attr);
38     if (ret != LOS_OK) {
39         return (int)ret;
40     }
41 
42 #if defined POSIX_MUTEX_DEFAULT_INHERIT
43     attr->protocol    = PTHREAD_PRIO_INHERIT;
44 #elif defined POSIX_MUTEX_DEFAULT_PROTECT
45     attr->protocol    = PTHREAD_PRIO_PROTECT;
46 #else
47     attr->protocol    = PTHREAD_PRIO_NONE;
48 #endif
49     attr->type = PTHREAD_MUTEX_NORMAL;
50     return LOS_OK;
51 }
52 
pthread_mutexattr_destroy(pthread_mutexattr_t *attr)53 int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
54 {
55     return LOS_MuxAttrDestroy(attr);
56 }
57 
pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)58 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
59 {
60     return LOS_MuxAttrSetProtocol(attr, protocol);
61 }
62 
pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)63 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
64 {
65     return LOS_MuxAttrGetProtocol(attr, protocol);
66 }
67 
pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)68 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
69 {
70     return LOS_MuxAttrSetPrioceiling(attr, prioceiling);
71 }
72 
pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)73 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
74 {
75     return LOS_MuxAttrGetPrioceiling(attr, prioceiling);
76 }
77 
pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *oldPrioceiling)78 int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *oldPrioceiling)
79 {
80     return LOS_MuxSetPrioceiling(mutex, prioceiling, oldPrioceiling);
81 }
82 
pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)83 int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)
84 {
85     return LOS_MuxGetPrioceiling(mutex, prioceiling);
86 }
87 
pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *outType)88 int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *outType)
89 {
90     return LOS_MuxAttrGetType(attr, outType);
91 }
92 
pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)93 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
94 {
95     return LOS_MuxAttrSetType(attr, type);
96 }
97 
98 /* Initialize mutex. If mutexAttr is NULL, use default attributes. */
pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)99 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
100 {
101     unsigned int ret = LOS_MuxInit(mutex, mutexAttr);
102     if ((ret == LOS_OK) && (mutexAttr == NULL)) {
103 #if defined POSIX_MUTEX_DEFAULT_INHERIT
104         mutex->attr.protocol    = PTHREAD_PRIO_INHERIT;
105 #elif defined POSIX_MUTEX_DEFAULT_PROTECT
106         mutex->attr.protocol    = PTHREAD_PRIO_PROTECT;
107 #else
108         mutex->attr.protocol    = PTHREAD_PRIO_NONE;
109 #endif
110         mutex->attr.type = PTHREAD_MUTEX_NORMAL;
111     }
112 
113     return (int)ret;
114 }
115 
pthread_mutex_destroy(pthread_mutex_t *mutex)116 int pthread_mutex_destroy(pthread_mutex_t *mutex)
117 {
118     return LOS_MuxDestroy(mutex);
119 }
120 
121 /* Lock mutex, waiting for it if necessary. */
pthread_mutex_lock(pthread_mutex_t *mutex)122 int pthread_mutex_lock(pthread_mutex_t *mutex)
123 {
124     return LOS_MuxLock(mutex, LOS_WAIT_FOREVER);
125 }
126 
pthread_mutex_trylock(pthread_mutex_t *mutex)127 int pthread_mutex_trylock(pthread_mutex_t *mutex)
128 {
129     return LOS_MuxTrylock(mutex);
130 }
131 
pthread_mutex_unlock(pthread_mutex_t *mutex)132 int pthread_mutex_unlock(pthread_mutex_t *mutex)
133 {
134     return LOS_MuxUnlock(mutex);
135 }
136 
137