Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
platform/pthread/threads_pthread_wrapper.c@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
JMF | 0:082731ede69f | 5 | * You may not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 6 | * A copy of the License is located at |
JMF | 0:082731ede69f | 7 | * |
JMF | 0:082731ede69f | 8 | * http://aws.amazon.com/apache2.0 |
JMF | 0:082731ede69f | 9 | * |
JMF | 0:082731ede69f | 10 | * or in the "license" file accompanying this file. This file is distributed |
JMF | 0:082731ede69f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
JMF | 0:082731ede69f | 12 | * express or implied. See the License for the specific language governing |
JMF | 0:082731ede69f | 13 | * permissions and limitations under the License. |
JMF | 0:082731ede69f | 14 | */ |
JMF | 0:082731ede69f | 15 | |
JMF | 0:082731ede69f | 16 | #include "threads_platform.h" |
JMF | 0:082731ede69f | 17 | #ifdef _ENABLE_THREAD_SUPPORT_ |
JMF | 0:082731ede69f | 18 | |
JMF | 0:082731ede69f | 19 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 20 | extern "C" { |
JMF | 0:082731ede69f | 21 | #endif |
JMF | 0:082731ede69f | 22 | |
JMF | 0:082731ede69f | 23 | /** |
JMF | 0:082731ede69f | 24 | * @brief Initialize the provided mutex |
JMF | 0:082731ede69f | 25 | * |
JMF | 0:082731ede69f | 26 | * Call this function to initialize the mutex |
JMF | 0:082731ede69f | 27 | * |
JMF | 0:082731ede69f | 28 | * @param IoT_Mutex_t - pointer to the mutex to be initialized |
JMF | 0:082731ede69f | 29 | * @return IoT_Error_t - error code indicating result of operation |
JMF | 0:082731ede69f | 30 | */ |
JMF | 0:082731ede69f | 31 | IoT_Error_t aws_iot_thread_mutex_init(IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 32 | if(0 != pthread_mutex_init(&(pMutex->lock), NULL)) { |
JMF | 0:082731ede69f | 33 | return MUTEX_INIT_ERROR; |
JMF | 0:082731ede69f | 34 | } |
JMF | 0:082731ede69f | 35 | |
JMF | 0:082731ede69f | 36 | return SUCCESS; |
JMF | 0:082731ede69f | 37 | } |
JMF | 0:082731ede69f | 38 | |
JMF | 0:082731ede69f | 39 | /** |
JMF | 0:082731ede69f | 40 | * @brief Lock the provided mutex |
JMF | 0:082731ede69f | 41 | * |
JMF | 0:082731ede69f | 42 | * Call this function to lock the mutex before performing a state change |
JMF | 0:082731ede69f | 43 | * Blocking, thread will block until lock request fails |
JMF | 0:082731ede69f | 44 | * |
JMF | 0:082731ede69f | 45 | * @param IoT_Mutex_t - pointer to the mutex to be locked |
JMF | 0:082731ede69f | 46 | * @return IoT_Error_t - error code indicating result of operation |
JMF | 0:082731ede69f | 47 | */ |
JMF | 0:082731ede69f | 48 | IoT_Error_t aws_iot_thread_mutex_lock(IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 49 | int rc = pthread_mutex_lock(&(pMutex->lock)); |
JMF | 0:082731ede69f | 50 | if(0 != rc) { |
JMF | 0:082731ede69f | 51 | return MUTEX_LOCK_ERROR; |
JMF | 0:082731ede69f | 52 | } |
JMF | 0:082731ede69f | 53 | |
JMF | 0:082731ede69f | 54 | return SUCCESS; |
JMF | 0:082731ede69f | 55 | } |
JMF | 0:082731ede69f | 56 | |
JMF | 0:082731ede69f | 57 | /** |
JMF | 0:082731ede69f | 58 | * @brief Try to lock the provided mutex |
JMF | 0:082731ede69f | 59 | * |
JMF | 0:082731ede69f | 60 | * Call this function to attempt to lock the mutex before performing a state change |
JMF | 0:082731ede69f | 61 | * Non-Blocking, immediately returns with failure if lock attempt fails |
JMF | 0:082731ede69f | 62 | * |
JMF | 0:082731ede69f | 63 | * @param IoT_Mutex_t - pointer to the mutex to be locked |
JMF | 0:082731ede69f | 64 | * @return IoT_Error_t - error code indicating result of operation |
JMF | 0:082731ede69f | 65 | */ |
JMF | 0:082731ede69f | 66 | IoT_Error_t aws_iot_thread_mutex_trylock(IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 67 | int rc = pthread_mutex_trylock(&(pMutex->lock)); |
JMF | 0:082731ede69f | 68 | if(0 != rc) { |
JMF | 0:082731ede69f | 69 | return MUTEX_LOCK_ERROR; |
JMF | 0:082731ede69f | 70 | } |
JMF | 0:082731ede69f | 71 | |
JMF | 0:082731ede69f | 72 | return SUCCESS; |
JMF | 0:082731ede69f | 73 | } |
JMF | 0:082731ede69f | 74 | |
JMF | 0:082731ede69f | 75 | /** |
JMF | 0:082731ede69f | 76 | * @brief Unlock the provided mutex |
JMF | 0:082731ede69f | 77 | * |
JMF | 0:082731ede69f | 78 | * Call this function to unlock the mutex before performing a state change |
JMF | 0:082731ede69f | 79 | * |
JMF | 0:082731ede69f | 80 | * @param IoT_Mutex_t - pointer to the mutex to be unlocked |
JMF | 0:082731ede69f | 81 | * @return IoT_Error_t - error code indicating result of operation |
JMF | 0:082731ede69f | 82 | */ |
JMF | 0:082731ede69f | 83 | IoT_Error_t aws_iot_thread_mutex_unlock(IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 84 | if(0 != pthread_mutex_unlock(&(pMutex->lock))) { |
JMF | 0:082731ede69f | 85 | return MUTEX_UNLOCK_ERROR; |
JMF | 0:082731ede69f | 86 | } |
JMF | 0:082731ede69f | 87 | |
JMF | 0:082731ede69f | 88 | return SUCCESS; |
JMF | 0:082731ede69f | 89 | } |
JMF | 0:082731ede69f | 90 | |
JMF | 0:082731ede69f | 91 | /** |
JMF | 0:082731ede69f | 92 | * @brief Destroy the provided mutex |
JMF | 0:082731ede69f | 93 | * |
JMF | 0:082731ede69f | 94 | * Call this function to destroy the mutex |
JMF | 0:082731ede69f | 95 | * |
JMF | 0:082731ede69f | 96 | * @param IoT_Mutex_t - pointer to the mutex to be destroyed |
JMF | 0:082731ede69f | 97 | * @return IoT_Error_t - error code indicating result of operation |
JMF | 0:082731ede69f | 98 | */ |
JMF | 0:082731ede69f | 99 | IoT_Error_t aws_iot_thread_mutex_destroy(IoT_Mutex_t *pMutex) { |
JMF | 0:082731ede69f | 100 | if(0 != pthread_mutex_destroy(&(pMutex->lock))) { |
JMF | 0:082731ede69f | 101 | return MUTEX_DESTROY_ERROR; |
JMF | 0:082731ede69f | 102 | } |
JMF | 0:082731ede69f | 103 | |
JMF | 0:082731ede69f | 104 | return SUCCESS; |
JMF | 0:082731ede69f | 105 | } |
JMF | 0:082731ede69f | 106 | |
JMF | 0:082731ede69f | 107 | #ifdef __cplusplus |
JMF | 0:082731ede69f | 108 | } |
JMF | 0:082731ede69f | 109 | #endif |
JMF | 0:082731ede69f | 110 | |
JMF | 0:082731ede69f | 111 | #endif /* _ENABLE_THREAD_SUPPORT_ */ |
JMF | 0:082731ede69f | 112 |