Line data Source code
1 : /**
2 : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0.
4 : */
5 :
6 : #include <aws/common/mutex.h>
7 : #include <aws/common/posix/common.inl>
8 :
9 : #include <errno.h>
10 :
11 0 : void aws_mutex_clean_up(struct aws_mutex *mutex) {
12 0 : AWS_PRECONDITION(mutex);
13 0 : if (mutex->initialized) {
14 0 : pthread_mutex_destroy(&mutex->mutex_handle);
15 0 : }
16 0 : AWS_ZERO_STRUCT(*mutex);
17 0 : }
18 :
19 0 : int aws_mutex_init(struct aws_mutex *mutex) {
20 0 : AWS_PRECONDITION(mutex);
21 0 : pthread_mutexattr_t attr;
22 0 : int err_code = pthread_mutexattr_init(&attr);
23 0 : int return_code = AWS_OP_SUCCESS;
24 0 :
25 0 : if (!err_code) {
26 0 : if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) ||
27 0 : (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) {
28 0 :
29 0 : return_code = aws_private_convert_and_raise_error_code(err_code);
30 0 : }
31 0 : pthread_mutexattr_destroy(&attr);
32 0 : } else {
33 0 : return_code = aws_private_convert_and_raise_error_code(err_code);
34 0 : }
35 0 :
36 0 : mutex->initialized = (return_code == AWS_OP_SUCCESS);
37 0 : return return_code;
38 0 : }
39 :
40 0 : int aws_mutex_lock(struct aws_mutex *mutex) {
41 0 : AWS_PRECONDITION(mutex && mutex->initialized);
42 0 : return aws_private_convert_and_raise_error_code(pthread_mutex_lock(&mutex->mutex_handle));
43 0 : }
44 :
45 0 : int aws_mutex_try_lock(struct aws_mutex *mutex) {
46 0 : AWS_PRECONDITION(mutex && mutex->initialized);
47 0 : return aws_private_convert_and_raise_error_code(pthread_mutex_trylock(&mutex->mutex_handle));
48 0 : }
49 :
50 0 : int aws_mutex_unlock(struct aws_mutex *mutex) {
51 0 : AWS_PRECONDITION(mutex && mutex->initialized);
52 0 : return aws_private_convert_and_raise_error_code(pthread_mutex_unlock(&mutex->mutex_handle));
53 0 : }
|