更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
pthread_mutex.c
浏览该文件的文档.
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
35****************************************************************************/
36/*!
37* @file pthread_mutex.c
38* @brief 对鸿蒙轻内核互斥锁的封装
39* @verbatim
40 当 pthread_mutex_lock() 返回时,该互斥锁已被锁定。调用线程是该互斥锁的属主。
41 如果该互斥锁已被另一个线程锁定和拥有,则调用线程将阻塞,直到该互斥锁变为可用为止。
42
43 如果互斥锁类型为 PTHREAD_MUTEX_NORMAL,则不提供死锁检测。尝试重新锁定互斥锁会导致死锁。
44 如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或未锁定,则将产生不确定的行为。
45
46 如果互斥锁类型为 PTHREAD_MUTEX_ERRORCHECK,则会提供错误检查。如果某个线程尝试重新锁定的互斥锁已经由该线程锁定,
47 则将返回错误。如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或者未锁定,则将返回错误。
48
49 如果互斥锁类型为 PTHREAD_MUTEX_RECURSIVE,则该互斥锁会保留锁定计数这一概念。线程首次成功获取互斥锁时,
50 锁定计数会设置为 1。线程每重新锁定该互斥锁一次,锁定计数就增加 1。线程每解除锁定该互斥锁一次,锁定计数就减小 1。
51 锁定计数达到 0 时,该互斥锁即可供其他线程获取。如果某个线程尝试解除锁定的互斥锁不是由该线程锁定或者未锁定,则将返回错误。
52
53 如果互斥锁类型是 PTHREAD_MUTEX_DEFAULT,则尝试以递归方式锁定该互斥锁将产生不确定的行为。
54 对于不是由调用线程锁定的互斥锁,如果尝试解除对它的锁定,则会产生不确定的行为。如果尝试解除锁定尚未锁定的互斥锁,
55 则会产生不确定的行为。
56 参考链接: https://docs.oracle.com/cd/E19253-01/819-7051/sync-12/index.html
57* @endverbatim
58*/
59
60/**
61 * @brief 初始化互斥锁属性
62 * @param attr
63 * @return int
64 */
65int pthread_mutexattr_init(pthread_mutexattr_t *attr)
66{//如果互斥锁已初始化,则它会处于未锁定状态。互斥锁可以位于进程之间共享的内存中或者某个进程的专用内存中。
67 unsigned int ret = LOS_MuxAttrInit(attr);
68 if (ret != LOS_OK) {
69 return (int)ret;
70 }
71///当其他线程正在使用某个互斥锁时,请勿重新初始化或销毁该互斥锁。如果任一操作没有正确完成,将会导致程序失败。如果要重新初始化或销毁某个互斥锁,则应用程序必须确保当前未使用该互斥锁。
72#if defined POSIX_MUTEX_DEFAULT_INHERIT
73 attr->protocol = PTHREAD_PRIO_INHERIT;
74#elif defined POSIX_MUTEX_DEFAULT_PROTECT
75 attr->protocol = PTHREAD_PRIO_PROTECT;
76#else
77 attr->protocol = PTHREAD_PRIO_NONE;
78#endif
79 attr->type = PTHREAD_MUTEX_NORMAL;
80 return LOS_OK;
81}
82
83int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
84{
85 return LOS_MuxAttrDestroy(attr);
86}
87
88int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
89{
90 return LOS_MuxAttrSetProtocol(attr, protocol);
91}
92
93int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
94{
95 return LOS_MuxAttrGetProtocol(attr, protocol);
96}
97
98int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
99{
100 return LOS_MuxAttrSetPrioceiling(attr, prioceiling);
101}
102
103int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
104{
105 return LOS_MuxAttrGetPrioceiling(attr, prioceiling);
106}
107
108int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *oldPrioceiling)
109{
110 return LOS_MuxSetPrioceiling(mutex, prioceiling, oldPrioceiling);
111}
112
113int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)
114{
115 return LOS_MuxGetPrioceiling(mutex, prioceiling);
116}
117
118int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *outType)
119{
120 return LOS_MuxAttrGetType(attr, outType);
121}
122
123int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
124{
125 return LOS_MuxAttrSetType(attr, type);
126}
127/* Initialize mutex. If mutexAttr is NULL, use default attributes. */
128/**
129 * @brief 初始化互斥锁。 如果 mutexAttr 为 NULL,则使用默认属性。
130 * @param mutex
131 * @param mutexAttr
132 * @return int
133 */
134int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
135{
136 unsigned int ret = LOS_MuxInit(mutex, mutexAttr);
137 if ((ret == LOS_OK) && (mutexAttr == NULL)) {
138#if defined POSIX_MUTEX_DEFAULT_INHERIT
139 mutex->attr.protocol = PTHREAD_PRIO_INHERIT;
140#elif defined POSIX_MUTEX_DEFAULT_PROTECT
141 mutex->attr.protocol = PTHREAD_PRIO_PROTECT;
142#else
143 mutex->attr.protocol = PTHREAD_PRIO_NONE;
144#endif
145 mutex->attr.type = PTHREAD_MUTEX_NORMAL;
146 }
147
148 return (int)ret;
149}
150///销毁互斥锁
151int pthread_mutex_destroy(pthread_mutex_t *mutex)
152{
153 return LOS_MuxDestroy(mutex);
154}
155///互斥锁加锁操作
156/* Lock mutex, waiting for it if necessary. */
157int pthread_mutex_lock(pthread_mutex_t *mutex)
158{
159 return LOS_MuxLock(mutex, LOS_WAIT_FOREVER);
160}
161///互斥锁尝试加锁操作
162int pthread_mutex_trylock(pthread_mutex_t *mutex)
163{
164 return LOS_MuxTrylock(mutex);
165}
166///解锁互斥锁
167int pthread_mutex_unlock(pthread_mutex_t *mutex)
168{
169 return LOS_MuxUnlock(mutex);
170}
171
LITE_OS_SEC_TEXT UINT32 LOS_MuxTrylock(LosMux *mutex)
尝试要锁,没拿到也不等,直接返回,不纠结
Definition: los_mux.c:464
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
LITE_OS_SEC_TEXT UINT32 LOS_MuxUnlock(LosMux *mutex)
释放锁
Definition: los_mux.c:559
LITE_OS_SEC_TEXT UINT32 LOS_MuxDestroy(LosMux *mutex)
销毁互斥锁
Definition: los_mux.c:289
LITE_OS_SEC_TEXT UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout)
拿互斥锁,
Definition: los_mux.c:437
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrGetProtocol(const LosMuxAttr *attr, INT32 *protocol)
获取互斥锁的类型属性
Definition: los_mux.c:146
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrSetType(LosMuxAttr *attr, INT32 type)
设置互斥锁的类型属性
Definition: los_mux.c:136
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrGetPrioceiling(const LosMuxAttr *attr, INT32 *prioceiling)
获取互斥锁属性优先级
Definition: los_mux.c:174
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrDestroy(LosMuxAttr *attr)
????? 销毁互斥属 ,这里啥也没干呀
Definition: los_mux.c:109
LITE_OS_SEC_TEXT UINT32 LOS_MuxSetPrioceiling(LosMux *mutex, INT32 prioceiling, INT32 *oldPrioceiling)
设置互斥锁的优先级的上限,老优先级由oldPrioceiling带走
Definition: los_mux.c:200
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrSetProtocol(LosMuxAttr *attr, INT32 protocol)
设置互斥锁属性的协议
Definition: los_mux.c:157
LITE_OS_SEC_TEXT UINT32 LOS_MuxGetPrioceiling(const LosMux *mutex, INT32 *prioceiling)
获取互斥锁的优先级的上限
Definition: los_mux.c:229
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrSetPrioceiling(LosMuxAttr *attr, INT32 prioceiling)
设置互斥锁属性的优先级的上限
Definition: los_mux.c:187
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrGetType(const LosMuxAttr *attr, INT32 *outType)
获取互斥锁的类型属性,由outType接走,不送!
Definition: los_mux.c:118
LITE_OS_SEC_TEXT UINT32 LOS_MuxAttrInit(LosMuxAttr *attr)
互斥属性初始化
Definition: los_mux.c:97
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)
Definition: pthread_mutex.c:93
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
Definition: pthread_mutex.c:83
int pthread_mutex_trylock(pthread_mutex_t *mutex)
互斥锁尝试加锁操作
int pthread_mutex_destroy(pthread_mutex_t *mutex)
销毁互斥锁
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *oldPrioceiling)
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
初始化互斥锁。 如果 mutexAttr 为 NULL,则使用默认属性。
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
Definition: pthread_mutex.c:98
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *outType)
int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)
int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
Definition: pthread_mutex.c:88
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
初始化互斥锁属性
Definition: pthread_mutex.c:65