1 | /* |
---|---|
2 | * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
5 | * You may not use this file except in compliance with the License. |
6 | * A copy of the License is located at |
7 | * |
8 | * http://aws.amazon.com/apache2.0 |
9 | * |
10 | * or in the "license" file accompanying this file. This file is distributed |
11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
12 | * express or implied. See the License for the specific language governing |
13 | * permissions and limitations under the License. |
14 | */ |
15 | |
16 | #include <aws/common/mutex.h> |
17 | #include <aws/common/posix/common.inl> |
18 | |
19 | #include <errno.h> |
20 | |
21 | void aws_mutex_clean_up(struct aws_mutex *mutex) { |
22 | pthread_mutex_destroy(&mutex->mutex_handle); |
23 | } |
24 | |
25 | int aws_mutex_init(struct aws_mutex *mutex) { |
26 | pthread_mutexattr_t attr; |
27 | int err_code = pthread_mutexattr_init(&attr); |
28 | int return_code = AWS_OP_SUCCESS; |
29 | |
30 | if (!err_code) { |
31 | if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) || |
32 | (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) { |
33 | |
34 | return_code = aws_private_convert_and_raise_error_code(err_code); |
35 | } |
36 | pthread_mutexattr_destroy(&attr); |
37 | } else { |
38 | return_code = aws_private_convert_and_raise_error_code(err_code); |
39 | } |
40 | |
41 | return return_code; |
42 | } |
43 | |
44 | int aws_mutex_lock(struct aws_mutex *mutex) { |
45 | |
46 | return aws_private_convert_and_raise_error_code(pthread_mutex_lock(&mutex->mutex_handle)); |
47 | } |
48 | |
49 | int aws_mutex_try_lock(struct aws_mutex *mutex) { |
50 | |
51 | return aws_private_convert_and_raise_error_code(pthread_mutex_trylock(&mutex->mutex_handle)); |
52 | } |
53 | |
54 | int aws_mutex_unlock(struct aws_mutex *mutex) { |
55 | |
56 | return aws_private_convert_and_raise_error_code(pthread_mutex_unlock(&mutex->mutex_handle)); |
57 | } |
58 |