1 #ifndef AWS_COMMON_MUTEX_H 2 #define AWS_COMMON_MUTEX_H 3 4 /** 5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 6 * SPDX-License-Identifier: Apache-2.0. 7 */ 8 9 #include <aws/common/common.h> 10 #ifdef _WIN32 11 /* NOTE: Do not use this macro before including Windows.h */ 12 # define AWSMUTEX_TO_WINDOWS(pMutex) (PSRWLOCK) & (pMutex)->mutex_handle 13 #else 14 # include <pthread.h> 15 #endif 16 17 struct aws_mutex { 18 #ifdef _WIN32 19 void *mutex_handle; 20 #else 21 pthread_mutex_t mutex_handle; 22 #endif 23 bool initialized; 24 }; 25 26 #ifdef _WIN32 27 # define AWS_MUTEX_INIT \ 28 { .mutex_handle = NULL, .initialized = true } 29 #else 30 # define AWS_MUTEX_INIT \ 31 { .mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true } 32 #endif 33 34 AWS_EXTERN_C_BEGIN 35 36 /** 37 * Initializes a new platform instance of mutex. 38 */ 39 AWS_COMMON_API 40 int aws_mutex_init(struct aws_mutex *mutex); 41 42 /** 43 * Cleans up internal resources. 44 */ 45 AWS_COMMON_API 46 void aws_mutex_clean_up(struct aws_mutex *mutex); 47 48 /** 49 * Blocks until it acquires the lock. While on some platforms such as Windows, 50 * this may behave as a reentrant mutex, you should not treat it like one. On 51 * platforms it is possible for it to be non-reentrant, it will be. 52 */ 53 AWS_COMMON_API 54 int aws_mutex_lock(struct aws_mutex *mutex); 55 56 /** 57 * Attempts to acquire the lock but returns immediately if it can not. 58 * While on some platforms such as Windows, this may behave as a reentrant mutex, 59 * you should not treat it like one. On platforms it is possible for it to be non-reentrant, it will be. 60 */ 61 AWS_COMMON_API 62 int aws_mutex_try_lock(struct aws_mutex *mutex); 63 64 /** 65 * Releases the lock. 66 */ 67 AWS_COMMON_API 68 int aws_mutex_unlock(struct aws_mutex *mutex); 69 70 AWS_EXTERN_C_END 71 72 #endif /* AWS_COMMON_MUTEX_H */ 73