forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.h
71 lines (60 loc) · 1.91 KB
/
mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef AWS_COMMON_MUTEX_H_
#define AWS_COMMON_MUTEX_H_
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/common.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif
struct aws_mutex {
struct aws_allocator *allocator;
#ifdef _WIN32
SRWLOCK mutex_handle;
#else
pthread_mutex_t mutex_handle;
#endif
};
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initializes a new platform instance of mutex.
*/
AWS_COMMON_API int aws_mutex_init(struct aws_mutex *mutex, struct aws_allocator *allocator);
/**
* Cleans up internal resources.
*/
AWS_COMMON_API void aws_mutex_clean_up(struct aws_mutex *mutex);
/**
* Blocks until it acquires the lock. While on some platforms such as Windows, this may behave as a reentrant mutex,
* you should not treat it like one. On platforms it is possible for it to be non-reentrant, it will be.
*/
AWS_COMMON_API int aws_mutex_lock(struct aws_mutex *mutex);
/**
* Attempts to acquire the lock but returns immediately if it can not.
* While on some platforms such as Windows, this may behave as a reentrant mutex,
* you should not treat it like one. On platforms it is possible for it to be non-reentrant, it will be.
*/
AWS_COMMON_API int aws_mutex_try_lock(struct aws_mutex *mutex);
/**
* Releases the lock.
*/
AWS_COMMON_API int aws_mutex_unlock(struct aws_mutex *mutex);
#ifdef __cplusplus
}
#endif
#endif /* AWS_COMMON_MUTEX_H_ */