1#ifndef AWS_COMMON_RW_LOCK_H
2#define AWS_COMMON_RW_LOCK_H
3
4/*
5 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/common.h>
20#ifdef _WIN32
21/* NOTE: Do not use this macro before including Windows.h */
22# define AWSSRW_TO_WINDOWS(pCV) (PSRWLOCK) pCV
23#else
24# include <pthread.h>
25#endif
26
27struct aws_rw_lock {
28#ifdef _WIN32
29 void *lock_handle;
30#else
31 pthread_rwlock_t lock_handle;
32#endif
33};
34
35#ifdef _WIN32
36# define AWS_RW_LOCK_INIT \
37 { .lock_handle = NULL }
38#else
39# define AWS_RW_LOCK_INIT \
40 { .lock_handle = PTHREAD_RWLOCK_INITIALIZER }
41#endif
42
43AWS_EXTERN_C_BEGIN
44
45/**
46 * Initializes a new platform instance of mutex.
47 */
48AWS_COMMON_API int aws_rw_lock_init(struct aws_rw_lock *lock);
49
50/**
51 * Cleans up internal resources.
52 */
53AWS_COMMON_API void aws_rw_lock_clean_up(struct aws_rw_lock *lock);
54
55/**
56 * Blocks until it acquires the lock. While on some platforms such as Windows,
57 * this may behave as a reentrant mutex, you should not treat it like one. On
58 * platforms it is possible for it to be non-reentrant, it will be.
59 */
60AWS_COMMON_API int aws_rw_lock_rlock(struct aws_rw_lock *lock);
61AWS_COMMON_API int aws_rw_lock_wlock(struct aws_rw_lock *lock);
62
63/**
64 * Attempts to acquire the lock but returns immediately if it can not.
65 * While on some platforms such as Windows, this may behave as a reentrant mutex,
66 * you should not treat it like one. On platforms it is possible for it to be non-reentrant, it will be.
67 */
68AWS_COMMON_API int aws_rw_lock_try_rlock(struct aws_rw_lock *lock);
69AWS_COMMON_API int aws_rw_lock_try_wlock(struct aws_rw_lock *lock);
70
71/**
72 * Releases the lock.
73 */
74AWS_COMMON_API int aws_rw_lock_runlock(struct aws_rw_lock *lock);
75AWS_COMMON_API int aws_rw_lock_wunlock(struct aws_rw_lock *lock);
76
77AWS_EXTERN_C_END
78
79#endif /* AWS_COMMON_RW_LOCK_H */
80