1#ifndef AWS_COMMON_MUTEX_H
2#define AWS_COMMON_MUTEX_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 AWSMUTEX_TO_WINDOWS(pCV) (PSRWLOCK) pCV
23#else
24# include <pthread.h>
25#endif
26
27struct aws_mutex {
28#ifdef _WIN32
29 void *mutex_handle;
30#else
31 pthread_mutex_t mutex_handle;
32#endif
33};
34
35#ifdef _WIN32
36# define AWS_MUTEX_INIT \
37 { NULL }
38#else
39# define AWS_MUTEX_INIT \
40 { .mutex_handle = PTHREAD_MUTEX_INITIALIZER }
41#endif
42
43AWS_EXTERN_C_BEGIN
44
45/**
46 * Initializes a new platform instance of mutex.
47 */
48AWS_COMMON_API
49int aws_mutex_init(struct aws_mutex *mutex);
50
51/**
52 * Cleans up internal resources.
53 */
54AWS_COMMON_API
55void aws_mutex_clean_up(struct aws_mutex *mutex);
56
57/**
58 * Blocks until it acquires the lock. While on some platforms such as Windows,
59 * this may behave as a reentrant mutex, you should not treat it like one. On
60 * platforms it is possible for it to be non-reentrant, it will be.
61 */
62AWS_COMMON_API
63int aws_mutex_lock(struct aws_mutex *mutex);
64
65/**
66 * Attempts to acquire the lock but returns immediately if it can not.
67 * While on some platforms such as Windows, this may behave as a reentrant mutex,
68 * you should not treat it like one. On platforms it is possible for it to be non-reentrant, it will be.
69 */
70AWS_COMMON_API
71int aws_mutex_try_lock(struct aws_mutex *mutex);
72
73/**
74 * Releases the lock.
75 */
76AWS_COMMON_API
77int aws_mutex_unlock(struct aws_mutex *mutex);
78
79AWS_EXTERN_C_END
80
81#endif /* AWS_COMMON_MUTEX_H */
82