|
| 1 | +/************************************************************************************//** |
| 2 | +* \file port/linux/critutil.c |
| 3 | +* \brief Critical section utility source file. |
| 4 | +* \ingroup Utility |
| 5 | +* \internal |
| 6 | +*---------------------------------------------------------------------------------------- |
| 7 | +* C O P Y R I G H T |
| 8 | +*---------------------------------------------------------------------------------------- |
| 9 | +* Copyright (c) 2017 by Feaser http://www.feaser.com All rights reserved |
| 10 | +* |
| 11 | +*---------------------------------------------------------------------------------------- |
| 12 | +* L I C E N S E |
| 13 | +*---------------------------------------------------------------------------------------- |
| 14 | +* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or |
| 15 | +* modify it under the terms of the GNU General Public License as published by the Free |
| 16 | +* Software Foundation, either version 3 of the License, or (at your option) any later |
| 17 | +* version. |
| 18 | +* |
| 19 | +* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 20 | +* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | +* PURPOSE. See the GNU General Public License for more details. |
| 22 | +* |
| 23 | +* You have received a copy of the GNU General Public License along with OpenBLT. It |
| 24 | +* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy. |
| 25 | +* |
| 26 | +* \endinternal |
| 27 | +****************************************************************************************/ |
| 28 | + |
| 29 | +/**************************************************************************************** |
| 30 | +* Include files |
| 31 | +****************************************************************************************/ |
| 32 | +#include <assert.h> /* for assertions */ |
| 33 | +#include <stdint.h> /* for standard integer types */ |
| 34 | +#include <stdbool.h> /* for boolean type */ |
| 35 | +#include <pthread.h> /* for posix threads */ |
| 36 | +#include "util.h" /* Utility module */ |
| 37 | + |
| 38 | + |
| 39 | +/**************************************************************************************** |
| 40 | +* Local data declarations |
| 41 | +****************************************************************************************/ |
| 42 | +/** \brief Flag to determine if the critical section object was already initialized. */ |
| 43 | +static volatile bool criticalSectionInitialized = false; |
| 44 | + |
| 45 | +/** \brief Critical section object. */ |
| 46 | +static volatile pthread_mutex_t mtxCritSect; |
| 47 | + |
| 48 | + |
| 49 | +/************************************************************************************//** |
| 50 | +** \brief Initializes the critical section module. Should be called before the |
| 51 | +** Enter/Exit functions are used. It is okay to call this initialization |
| 52 | +** multiple times from different modules. |
| 53 | +** |
| 54 | +****************************************************************************************/ |
| 55 | +void UtilCriticalSectionInit(void) |
| 56 | +{ |
| 57 | + /* Only initialize if not yet done so previously. */ |
| 58 | + if (!criticalSectionInitialized) |
| 59 | + { |
| 60 | + /* Initialize the critical section object. */ |
| 61 | + (void)pthread_mutex_init((pthread_mutex_t *)&mtxCritSect, NULL); |
| 62 | + /* Set initialized flag. */ |
| 63 | + criticalSectionInitialized = true; |
| 64 | + } |
| 65 | +} /*** end of UtilCriticalSectionInit ***/ |
| 66 | + |
| 67 | + |
| 68 | +/************************************************************************************//** |
| 69 | +** \brief Terminates the critical section module. Should be called once critical |
| 70 | +** sections are no longer needed. Typically called from another module's |
| 71 | +** termination function that also initialized it. It is okay to call this |
| 72 | +** termination multiple times from different modules. |
| 73 | +** |
| 74 | +****************************************************************************************/ |
| 75 | +void UtilCriticalSectionTerminate(void) |
| 76 | +{ |
| 77 | + /* Only terminate if it was initialized. */ |
| 78 | + if (criticalSectionInitialized) |
| 79 | + { |
| 80 | + /* Reset the initialized flag. */ |
| 81 | + criticalSectionInitialized = false; |
| 82 | + /* Delete the critical section object. */ |
| 83 | + (void)pthread_mutex_destroy((pthread_mutex_t *)&mtxCritSect); |
| 84 | + } |
| 85 | +} /*** end of UtilCriticalSectionTerminate ***/ |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +/************************************************************************************//** |
| 90 | +** \brief Enters a critical section. The functions UtilCriticalSectionEnter and |
| 91 | +** UtilCriticalSectionExit should always be used in a pair. |
| 92 | +** |
| 93 | +****************************************************************************************/ |
| 94 | +void UtilCriticalSectionEnter(void) |
| 95 | +{ |
| 96 | + /* Check initialization. */ |
| 97 | + assert(criticalSectionInitialized); |
| 98 | + |
| 99 | + /* Only continue if actually initialized. */ |
| 100 | + if (criticalSectionInitialized) |
| 101 | + { |
| 102 | + (void)pthread_mutex_lock((pthread_mutex_t *)&mtxCritSect); |
| 103 | + } |
| 104 | +} /*** end of UtilCriticalSectionEnter ***/ /*lint !e456 !e454 */ |
| 105 | + |
| 106 | + |
| 107 | +/************************************************************************************//** |
| 108 | +** \brief Leaves a critical section. The functions UtilCriticalSectionEnter and |
| 109 | +** UtilCriticalSectionExit should always be used in a pair. |
| 110 | +** |
| 111 | +****************************************************************************************/ |
| 112 | +void UtilCriticalSectionExit(void) |
| 113 | +{ |
| 114 | + /* Check initialization. */ |
| 115 | + assert(criticalSectionInitialized); |
| 116 | + |
| 117 | + /* Only continue if actually initialized. */ |
| 118 | + if (criticalSectionInitialized) |
| 119 | + { |
| 120 | + (void)pthread_mutex_unlock((pthread_mutex_t *)&mtxCritSect); /*lint !e455 */ |
| 121 | + } |
| 122 | +} /*** end of UtilCriticalSectionExit ***/ |
| 123 | + |
| 124 | + |
| 125 | +/*********************************** end of critutil.c *********************************/ |
| 126 | + |
0 commit comments