| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2009, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bkni_multi.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/20 $ |
|---|
| 12 | * $brcm_Date: 4/2/09 11:29a $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/basemodules/kni/generic/bkni_multi.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/20 4/2/09 11:29a erickson |
|---|
| 21 | * PR53778: extend BKNI_TRACK_MALLOCS to events and mutexes |
|---|
| 22 | * |
|---|
| 23 | * Hydra_Software_Devel/19 4/8/03 4:47p erickson |
|---|
| 24 | * BKNI_ResetEvent now returns void |
|---|
| 25 | * Split BKNI_AcquireMutex into Acquire and TryAcquire |
|---|
| 26 | * |
|---|
| 27 | * Hydra_Software_Devel/18 4/4/03 12:24p erickson |
|---|
| 28 | * trying to fix list item |
|---|
| 29 | * |
|---|
| 30 | * Hydra_Software_Devel/17 4/4/03 12:22p erickson |
|---|
| 31 | * updated docs |
|---|
| 32 | * |
|---|
| 33 | * Hydra_Software_Devel/16 4/4/03 11:27a erickson |
|---|
| 34 | * updated documentation |
|---|
| 35 | * |
|---|
| 36 | * Hydra_Software_Devel/15 4/3/03 6:13p erickson |
|---|
| 37 | * some pre 0.9 api rework |
|---|
| 38 | * |
|---|
| 39 | * Hydra_Software_Devel/14 4/1/03 7:02p erickson |
|---|
| 40 | * updated comments |
|---|
| 41 | * |
|---|
| 42 | * Hydra_Software_Devel/13 4/1/03 3:16p erickson |
|---|
| 43 | * update AcquireMutex notes |
|---|
| 44 | * |
|---|
| 45 | * Hydra_Software_Devel/12 3/25/03 11:44a erickson |
|---|
| 46 | * Updated mutex commnets |
|---|
| 47 | * |
|---|
| 48 | * Hydra_Software_Devel/11 3/24/03 11:38a erickson |
|---|
| 49 | * reworked docs |
|---|
| 50 | * |
|---|
| 51 | * Hydra_Software_Devel/10 3/12/03 12:04p erickson |
|---|
| 52 | * updated return codes, comments |
|---|
| 53 | * |
|---|
| 54 | * Hydra_Software_Devel/9 3/11/03 6:59p erickson |
|---|
| 55 | * changed kernelinterface from using object ptrs to handles |
|---|
| 56 | * |
|---|
| 57 | * Hydra_Software_Devel/8 3/10/03 8:28p vsilyaev |
|---|
| 58 | * Added support for tagged kernel interface. |
|---|
| 59 | * |
|---|
| 60 | * Hydra_Software_Devel/7 3/10/03 6:37p vsilyaev |
|---|
| 61 | * Uses extern "C" { } brackets. |
|---|
| 62 | * |
|---|
| 63 | * Hydra_Software_Devel/6 3/10/03 2:35p vsilyaev |
|---|
| 64 | * Fixed the BKNI_TaskPriority enum. |
|---|
| 65 | * |
|---|
| 66 | * Hydra_Software_Devel/5 3/10/03 2:34p erickson |
|---|
| 67 | * expanded comments |
|---|
| 68 | * |
|---|
| 69 | * Hydra_Software_Devel/5 3/10/03 2:29p erickson |
|---|
| 70 | * updated docs |
|---|
| 71 | * |
|---|
| 72 | * Hydra_Software_Devel/5 3/10/03 12:36p erickson |
|---|
| 73 | * linuxkernel tasks initial implementationd |
|---|
| 74 | * |
|---|
| 75 | * Hydra_Software_Devel/3 3/7/03 5:20p erickson |
|---|
| 76 | * linux kernel interface work |
|---|
| 77 | * |
|---|
| 78 | * Hydra_Software_Devel/2 3/6/03 6:27p erickson |
|---|
| 79 | * rework KNI api |
|---|
| 80 | * |
|---|
| 81 | * Hydra_Software_Devel/1 3/5/03 5:16p erickson |
|---|
| 82 | * Initial kernelinterface work |
|---|
| 83 | * |
|---|
| 84 | ***************************************************************************/ |
|---|
| 85 | #ifndef BKNI_MULTI_H__ |
|---|
| 86 | #define BKNI_MULTI_H__ |
|---|
| 87 | |
|---|
| 88 | #include "bkni.h" |
|---|
| 89 | |
|---|
| 90 | #ifdef __cplusplus |
|---|
| 91 | extern "C" { |
|---|
| 92 | #endif |
|---|
| 93 | |
|---|
| 94 | /*=************************************************************************* |
|---|
| 95 | The multi-tasking kernel interface can only be used by multi-tasking |
|---|
| 96 | modules. In the Magnum architecture, this can only be SysLib modules. |
|---|
| 97 | |
|---|
| 98 | See bkni.h for a kernel interface overview. |
|---|
| 99 | ****************************************************************************/ |
|---|
| 100 | |
|---|
| 101 | /*************************************************************************** |
|---|
| 102 | Summary: |
|---|
| 103 | BKNI_INFINITE can be passed as a timeout value into BKNI_WaitForEvent |
|---|
| 104 | for multi-threaded modules. |
|---|
| 105 | ****************************************************************************/ |
|---|
| 106 | #define BKNI_INFINITE -1 |
|---|
| 107 | |
|---|
| 108 | /*************************************************************************** |
|---|
| 109 | Summary: |
|---|
| 110 | Mutex object initialized by BKNI_CreateMutex. |
|---|
| 111 | ****************************************************************************/ |
|---|
| 112 | typedef struct BKNI_MutexObj *BKNI_MutexHandle; |
|---|
| 113 | |
|---|
| 114 | /*************************************************************************** |
|---|
| 115 | Summary: |
|---|
| 116 | Create a mutex in an unacquired state. |
|---|
| 117 | |
|---|
| 118 | Description: |
|---|
| 119 | The mutex is returned in an unacquired state, which means that the first call |
|---|
| 120 | to BKNI_AcquireMutex or BKNI_TryAcquireMutex for that mutex is guaranteed to succeed immediately. |
|---|
| 121 | |
|---|
| 122 | Note that there is no name parameter in BKNI_CreateMutex. We do not support named |
|---|
| 123 | mutexes because they are essentially global variables that can lead to deadlock. |
|---|
| 124 | Passing in a name for debugging purposes might lead to someone to think we |
|---|
| 125 | support named mutexes. |
|---|
| 126 | |
|---|
| 127 | Returns: |
|---|
| 128 | BERR_SUCCESS if the mutex is created. |
|---|
| 129 | BERR_OS_ERROR if mutex was not created. |
|---|
| 130 | ****************************************************************************/ |
|---|
| 131 | #if BKNI_TRACK_MALLOCS |
|---|
| 132 | #define BKNI_CreateMutex(mutex) BKNI_CreateMutex_tagged(mutex, __FILE__, __LINE__) |
|---|
| 133 | |
|---|
| 134 | BERR_Code BKNI_CreateMutex_tagged( |
|---|
| 135 | BKNI_MutexHandle *mutex, |
|---|
| 136 | const char *file, |
|---|
| 137 | int line |
|---|
| 138 | ); |
|---|
| 139 | #else |
|---|
| 140 | BERR_Code BKNI_CreateMutex(BKNI_MutexHandle *mutex); |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | /*************************************************************************** |
|---|
| 144 | Summary: |
|---|
| 145 | Destroy a mutex. |
|---|
| 146 | |
|---|
| 147 | Description: |
|---|
| 148 | Destroying a mutex in an acquired state is not allowed and leads to undefined behavior. |
|---|
| 149 | ****************************************************************************/ |
|---|
| 150 | #if BKNI_TRACK_MALLOCS |
|---|
| 151 | #define BKNI_DestroyMutex(mutex) BKNI_DestroyMutex_tagged(mutex, __FILE__, __LINE__) |
|---|
| 152 | |
|---|
| 153 | void BKNI_DestroyMutex_tagged( |
|---|
| 154 | BKNI_MutexHandle mutex, |
|---|
| 155 | const char *file, |
|---|
| 156 | int line |
|---|
| 157 | ); |
|---|
| 158 | #else |
|---|
| 159 | void BKNI_DestroyMutex(BKNI_MutexHandle mutex); |
|---|
| 160 | #endif |
|---|
| 161 | |
|---|
| 162 | /*************************************************************************** |
|---|
| 163 | Summary: |
|---|
| 164 | Try acquiring exclusive ownership of a mutex, but do not block. |
|---|
| 165 | |
|---|
| 166 | Description: |
|---|
| 167 | If no other task currently owns the mutex, BKNI_TryAcquireMutex will return |
|---|
| 168 | with BERR_SUCCESS and the caller retains exclusive ownership until |
|---|
| 169 | BKNI_ReleaseMutex is called for that mutex. |
|---|
| 170 | |
|---|
| 171 | If another task currently owns the mutex, BKNI_TryAcquireMutex will not block |
|---|
| 172 | but will return immediately with BERR_TIMEOUT. |
|---|
| 173 | |
|---|
| 174 | Magnum code cannot nest calls to BKNI_TryAcquireMutex or BKNI_AcquireMutex. |
|---|
| 175 | If a mutex has already been acquired in a task, that same task cannot acquire it |
|---|
| 176 | a second time. It leads to undefined behavior. Some possible results include: |
|---|
| 177 | |
|---|
| 178 | * Deadlock. The function will never return and the task is hung forever. |
|---|
| 179 | * Systemhalts. |
|---|
| 180 | * Error returned and mutex is only acquired once. If the error code is not handled (which is a violation of Magnum rules), race conditions will be introduced. |
|---|
| 181 | * No error returned and mutex is acquired twice (two releases are required). This may allow bad code to be hidden until another platform uncovers the problem. |
|---|
| 182 | |
|---|
| 183 | A platform's implementation may chose any of the above results, although we strongly |
|---|
| 184 | recommend that the function fail and return an error code if possible. |
|---|
| 185 | |
|---|
| 186 | Returns: |
|---|
| 187 | BERR_SUCCESS - Mutex was acquired successfully. |
|---|
| 188 | BERR_TIMEOUT - The mutex is already acquired by another task and so the mutex was not acquired. |
|---|
| 189 | BERR_OS_ERROR - The system failed and the mutex was not acquired. |
|---|
| 190 | |
|---|
| 191 | See Also: |
|---|
| 192 | BKNI_AcquireMutex, BKNI_ReleaseMutex, Magnum ThreadSafe rules |
|---|
| 193 | ****************************************************************************/ |
|---|
| 194 | BERR_Code BKNI_TryAcquireMutex( |
|---|
| 195 | BKNI_MutexHandle mutex |
|---|
| 196 | ); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | /*************************************************************************** |
|---|
| 200 | Summary: |
|---|
| 201 | Acquire exclusive ownership of a mutex, possibly blocking. |
|---|
| 202 | |
|---|
| 203 | Description: |
|---|
| 204 | Acquire exclusive ownership of a mutex. If another task currently |
|---|
| 205 | owns the mutex, BKNI_AcquireMutex will block until the mutex can be acquired. |
|---|
| 206 | After acquiring the mutex, the caller retains exclusive ownership until |
|---|
| 207 | BKNI_ReleaseMutex is called for that mutex. |
|---|
| 208 | |
|---|
| 209 | Magnum code cannot nest calls to BKNI_AcquireMutex or BKNI_TryAcquireMutex. |
|---|
| 210 | If a mutex has already been acquired in a task, that same task cannot acquire it |
|---|
| 211 | a second time. It leads to undefined behavior. Some possible results include: |
|---|
| 212 | |
|---|
| 213 | * Deadlock. The function will never return and the task is hung forever. |
|---|
| 214 | * Systemhalts. |
|---|
| 215 | * Error returned and mutex is only acquired once. If the error code is not handled (which is a violation of Magnum rules), race conditions will be introduced. |
|---|
| 216 | * No error returned and mutex is acquired twice (two releases are required). This may allow bad code to be hidden until another platform uncovers the problem. |
|---|
| 217 | |
|---|
| 218 | A platform's implementation may chose any of the above results, although we strongly |
|---|
| 219 | recommend that the function fail and return an error code if possible. |
|---|
| 220 | |
|---|
| 221 | Returns: |
|---|
| 222 | BERR_SUCCESS - Mutex was acquired successfully. |
|---|
| 223 | BERR_OS_ERROR - The system failed or was interrupted, and the mutex was not acquired. |
|---|
| 224 | |
|---|
| 225 | See Also: |
|---|
| 226 | BKNI_TryAcquireMutex, BKNI_ReleaseMutex, Magnum ThreadSafe rules |
|---|
| 227 | ****************************************************************************/ |
|---|
| 228 | BERR_Code BKNI_AcquireMutex( |
|---|
| 229 | BKNI_MutexHandle mutex |
|---|
| 230 | ); |
|---|
| 231 | |
|---|
| 232 | /*************************************************************************** |
|---|
| 233 | Summary: |
|---|
| 234 | Release exclusive ownership of a mutex. |
|---|
| 235 | |
|---|
| 236 | Description: |
|---|
| 237 | If you successfully acquired the mutex using BKNI_AcquireMutex or BKNI_TryAcquireMutex, |
|---|
| 238 | BKNI_ReleaseMutex will release the mutex. |
|---|
| 239 | |
|---|
| 240 | In Magnum code, releasing a mutex which was acquired in a different task is not |
|---|
| 241 | allowed and leads to undefined behavior. |
|---|
| 242 | Also, releasing a mutex which is not in an acquired state is not allowed |
|---|
| 243 | and leads to undefined behavior. |
|---|
| 244 | |
|---|
| 245 | A platform implementation does not have to enforce the usage rules noted |
|---|
| 246 | above. This is because it may not be possible in all cases, and the platform |
|---|
| 247 | may want to actually allow this behavior in platform-specific (non-Magnum) code. |
|---|
| 248 | |
|---|
| 249 | See Also: |
|---|
| 250 | BKNI_AcquireMutex, BKNI_TryAcquireMutex, Magnum ThreadSafe rules |
|---|
| 251 | ****************************************************************************/ |
|---|
| 252 | void BKNI_ReleaseMutex(BKNI_MutexHandle mutex); |
|---|
| 253 | |
|---|
| 254 | #ifdef __cplusplus |
|---|
| 255 | } |
|---|
| 256 | #endif |
|---|
| 257 | |
|---|
| 258 | #endif /* BKNI_MULTI_H__ */ |
|---|