Nigel Rantor / azure_c_shared_utility

Fork of azure_c_shared_utility by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lock_rtx_mbed.cpp Source File

lock_rtx_mbed.cpp

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include "azure_c_shared_utility/lock.h"
00005 #include "azure_c_shared_utility/xlogging.h"
00006 #include "rtos.h"
00007 
00008 
00009 LOCK_HANDLE Lock_Init(void)
00010 {
00011     /* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */
00012     /* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL] */
00013     Mutex* result = new Mutex();
00014     if (result == NULL)
00015     {
00016         LogError("Failed to instantiate a new Mutex object.");
00017     }
00018 
00019     return (LOCK_HANDLE)result;
00020 }
00021 
00022 LOCK_RESULT Lock(LOCK_HANDLE handle)
00023 {
00024     LOCK_RESULT result;
00025     if (handle == NULL)
00026     {
00027         /* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */
00028         LogError("Invalid argument; handle is NULL.");
00029         result = LOCK_ERROR;
00030     }
00031     else
00032     {
00033         Mutex* lock_mtx = (Mutex*)handle;
00034         if (lock_mtx->lock() == osOK)
00035         {
00036             /* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */
00037             result = LOCK_OK;
00038         }
00039         else
00040         {
00041             /* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */
00042             LogError("Mutex(%p)->lock() failed.", handle);
00043             result = LOCK_ERROR;
00044         }
00045     }
00046 
00047     return result;
00048 }
00049 
00050 LOCK_RESULT Unlock(LOCK_HANDLE handle)
00051 {
00052     LOCK_RESULT result;
00053     if (handle == NULL)
00054     {
00055         /* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */
00056         LogError("Invalid argument; handle is NULL.");
00057         result = LOCK_ERROR;
00058     }
00059     else
00060     {
00061         Mutex* lock_mtx = (Mutex*)handle;
00062         if (lock_mtx->unlock() == osOK)
00063         {
00064             /* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */
00065             result = LOCK_OK;
00066         }
00067         else
00068         {
00069             /* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */
00070             LogError("Mutex(%p)->unlock() failed.", handle);
00071             result = LOCK_ERROR;
00072         }
00073     }
00074 
00075     return result;
00076 }
00077 
00078 LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle)
00079 {
00080     LOCK_RESULT result;
00081     if (NULL == handle)
00082     {
00083         /* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */
00084         LogError("Invalid argument; handle is NULL.");
00085         result = LOCK_ERROR;
00086     }
00087     else
00088     {
00089         /* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */
00090         Mutex* lock_mtx = (Mutex*)handle;
00091         delete lock_mtx;
00092         result = LOCK_OK;
00093     }
00094     
00095     return result;
00096 }