Jim Flynn / Mbed OS aws-iot-device-sdk-mbed-c
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers threads_pthread_wrapper.c Source File

threads_pthread_wrapper.c

00001 /*
00002  * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License").
00005  * You may not use this file except in compliance with the License.
00006  * A copy of the License is located at
00007  *
00008  *  http://aws.amazon.com/apache2.0
00009  *
00010  * or in the "license" file accompanying this file. This file is distributed
00011  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
00012  * express or implied. See the License for the specific language governing
00013  * permissions and limitations under the License.
00014  */
00015 
00016 #include "threads_platform.h"
00017 #ifdef _ENABLE_THREAD_SUPPORT_
00018 
00019 #ifdef __cplusplus
00020 extern "C" {
00021 #endif
00022 
00023 /**
00024  * @brief Initialize the provided mutex
00025  *
00026  * Call this function to initialize the mutex
00027  *
00028  * @param IoT_Mutex_t - pointer to the mutex to be initialized
00029  * @return IoT_Error_t - error code indicating result of operation
00030  */
00031 IoT_Error_t aws_iot_thread_mutex_init(IoT_Mutex_t *pMutex) {
00032     if(0 != pthread_mutex_init(&(pMutex->lock), NULL)) {
00033         return MUTEX_INIT_ERROR;
00034     }
00035 
00036     return SUCCESS;
00037 }
00038 
00039 /**
00040  * @brief Lock the provided mutex
00041  *
00042  * Call this function to lock the mutex before performing a state change
00043  * Blocking, thread will block until lock request fails
00044  *
00045  * @param IoT_Mutex_t - pointer to the mutex to be locked
00046  * @return IoT_Error_t - error code indicating result of operation
00047  */
00048 IoT_Error_t aws_iot_thread_mutex_lock(IoT_Mutex_t *pMutex) {
00049 int rc = pthread_mutex_lock(&(pMutex->lock));
00050     if(0 != rc) {
00051         return MUTEX_LOCK_ERROR;
00052     }
00053 
00054     return SUCCESS;
00055 }
00056 
00057 /**
00058  * @brief Try to lock the provided mutex
00059  *
00060  * Call this function to attempt to lock the mutex before performing a state change
00061  * Non-Blocking, immediately returns with failure if lock attempt fails
00062  *
00063  * @param IoT_Mutex_t - pointer to the mutex to be locked
00064  * @return IoT_Error_t - error code indicating result of operation
00065  */
00066 IoT_Error_t aws_iot_thread_mutex_trylock(IoT_Mutex_t *pMutex) {
00067 int rc = pthread_mutex_trylock(&(pMutex->lock));
00068     if(0 != rc) {
00069         return MUTEX_LOCK_ERROR;
00070     }
00071 
00072     return SUCCESS;
00073 }
00074 
00075 /**
00076  * @brief Unlock the provided mutex
00077  *
00078  * Call this function to unlock the mutex before performing a state change
00079  *
00080  * @param IoT_Mutex_t - pointer to the mutex to be unlocked
00081  * @return IoT_Error_t - error code indicating result of operation
00082  */
00083 IoT_Error_t aws_iot_thread_mutex_unlock(IoT_Mutex_t *pMutex) {
00084     if(0 != pthread_mutex_unlock(&(pMutex->lock))) {
00085         return MUTEX_UNLOCK_ERROR;
00086     }
00087 
00088     return SUCCESS;
00089 }
00090 
00091 /**
00092  * @brief Destroy the provided mutex
00093  *
00094  * Call this function to destroy the mutex
00095  *
00096  * @param IoT_Mutex_t - pointer to the mutex to be destroyed
00097  * @return IoT_Error_t - error code indicating result of operation
00098  */
00099 IoT_Error_t aws_iot_thread_mutex_destroy(IoT_Mutex_t *pMutex) {
00100     if(0 != pthread_mutex_destroy(&(pMutex->lock))) {
00101         return MUTEX_DESTROY_ERROR;
00102     }
00103 
00104     return SUCCESS;
00105 }
00106 
00107 #ifdef __cplusplus
00108 }
00109 #endif
00110 
00111 #endif /* _ENABLE_THREAD_SUPPORT_ */
00112