| 1 | /*************************************************************************** |
|---|
| 2 | * (c)2004-2009 Broadcom Corporation |
|---|
| 3 | * |
|---|
| 4 | * This program is the proprietary software of Broadcom Corporation and/or its licensors, |
|---|
| 5 | * and may only be used, duplicated, modified or distributed pursuant to the terms and |
|---|
| 6 | * conditions of a separate, written license agreement executed between you and Broadcom |
|---|
| 7 | * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants |
|---|
| 8 | * no license (express or implied), right to use, or waiver of any kind with respect to the |
|---|
| 9 | * Software, and Broadcom expressly reserves all rights in and to the Software and all |
|---|
| 10 | * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU |
|---|
| 11 | * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY |
|---|
| 12 | * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. |
|---|
| 13 | * |
|---|
| 14 | * Except as expressly set forth in the Authorized License, |
|---|
| 15 | * |
|---|
| 16 | * 1. This program, including its structure, sequence and organization, constitutes the valuable trade |
|---|
| 17 | * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, |
|---|
| 18 | * and to use this information only in connection with your use of Broadcom integrated circuit products. |
|---|
| 19 | * |
|---|
| 20 | * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
|---|
| 21 | * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR |
|---|
| 22 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO |
|---|
| 23 | * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES |
|---|
| 24 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, |
|---|
| 25 | * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION |
|---|
| 26 | * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF |
|---|
| 27 | * USE OR PERFORMANCE OF THE SOFTWARE. |
|---|
| 28 | * |
|---|
| 29 | * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS |
|---|
| 30 | * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR |
|---|
| 31 | * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR |
|---|
| 32 | * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF |
|---|
| 33 | * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT |
|---|
| 34 | * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE |
|---|
| 35 | * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF |
|---|
| 36 | * ANY LIMITED REMEDY. |
|---|
| 37 | * |
|---|
| 38 | * $brcm_Workfile: b_os_lib.h $ |
|---|
| 39 | * $brcm_Revision: 5 $ |
|---|
| 40 | * $brcm_Date: 7/6/09 1:14p $ |
|---|
| 41 | * |
|---|
| 42 | * Description: |
|---|
| 43 | * API name: OS |
|---|
| 44 | * Library routines for OS abstraction |
|---|
| 45 | * |
|---|
| 46 | * Revision History: |
|---|
| 47 | * |
|---|
| 48 | * $brcm_Log: /nexus/lib/os/include/b_os_lib.h $ |
|---|
| 49 | * |
|---|
| 50 | * 5 7/6/09 1:14p jgarrett |
|---|
| 51 | * PR 55572: Adding option for thread termination |
|---|
| 52 | * |
|---|
| 53 | * 4 6/4/08 11:20a rjlewis |
|---|
| 54 | * PR40352: can't be void when instantiated. |
|---|
| 55 | * |
|---|
| 56 | * 3 3/31/08 1:13p jgarrett |
|---|
| 57 | * PR 41043: Adding time routines |
|---|
| 58 | * |
|---|
| 59 | * 2 3/10/08 8:54p jgarrett |
|---|
| 60 | * PR 40306: Adding mutex lock assertion |
|---|
| 61 | * |
|---|
| 62 | * 1 3/10/08 1:59p jgarrett |
|---|
| 63 | * PR 40306: Adding osLib |
|---|
| 64 | * |
|---|
| 65 | ***************************************************************************/ |
|---|
| 66 | #ifndef B_OS_LIB_H__ |
|---|
| 67 | #define B_OS_LIB_H__ |
|---|
| 68 | |
|---|
| 69 | #ifdef __cplusplus |
|---|
| 70 | extern "C" { |
|---|
| 71 | #endif |
|---|
| 72 | |
|---|
| 73 | #include "bstd.h" |
|---|
| 74 | #include "b_os_time.h" |
|---|
| 75 | |
|---|
| 76 | /*************************************************************************** |
|---|
| 77 | Summary: |
|---|
| 78 | Generic Error Codes |
|---|
| 79 | |
|---|
| 80 | Description: |
|---|
| 81 | These error codes will match those returned by nexus (NEXUS_Error) and |
|---|
| 82 | magnum (BERR_Code). These may be used throughout application libraries |
|---|
| 83 | for consistency. |
|---|
| 84 | ***************************************************************************/ |
|---|
| 85 | typedef unsigned B_Error; |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | Summary: |
|---|
| 89 | Standard Nexus error codes. |
|---|
| 90 | **/ |
|---|
| 91 | #define B_ERROR_SUCCESS 0 /* success (always zero) */ |
|---|
| 92 | #define B_ERROR_NOT_INITIALIZED 1 /* parameter not initialized */ |
|---|
| 93 | #define B_ERROR_INVALID_PARAMETER 2 /* parameter is invalid */ |
|---|
| 94 | #define B_ERROR_OUT_OF_MEMORY 3 /* out of heap memory */ |
|---|
| 95 | #define B_ERROR_TIMEOUT 5 /* reached timeout limit */ |
|---|
| 96 | #define B_ERROR_OS_ERROR 6 /* generic OS error */ |
|---|
| 97 | #define B_ERROR_LEAKED_RESOURCE 7 /* resource being freed has attached resources that haven't been freed */ |
|---|
| 98 | #define B_ERROR_NOT_SUPPORTED 8 /* requested feature is not supported */ |
|---|
| 99 | #define B_ERROR_UNKNOWN 9 /* unknown */ |
|---|
| 100 | |
|---|
| 101 | /*************************************************************************** |
|---|
| 102 | Summary: |
|---|
| 103 | Initialize the OS Library |
|---|
| 104 | |
|---|
| 105 | Description: |
|---|
| 106 | It is permissible to call this routine multiple times. Internally, a |
|---|
| 107 | reference count will be maintained. |
|---|
| 108 | ***************************************************************************/ |
|---|
| 109 | B_Error B_Os_Init(void); |
|---|
| 110 | |
|---|
| 111 | /*************************************************************************** |
|---|
| 112 | Summary: |
|---|
| 113 | Un-Initialize the OS Library |
|---|
| 114 | |
|---|
| 115 | Description: |
|---|
| 116 | This routine must be called once per call to B_Os_Init |
|---|
| 117 | ***************************************************************************/ |
|---|
| 118 | B_Error B_Os_Uninit(void); |
|---|
| 119 | |
|---|
| 120 | /*************************************************************************** |
|---|
| 121 | Summary: |
|---|
| 122 | Mutex Handle |
|---|
| 123 | ***************************************************************************/ |
|---|
| 124 | typedef struct B_Mutex *B_MutexHandle; |
|---|
| 125 | |
|---|
| 126 | /*************************************************************************** |
|---|
| 127 | Summary: |
|---|
| 128 | Event Handle |
|---|
| 129 | ***************************************************************************/ |
|---|
| 130 | typedef void *B_EventHandle; |
|---|
| 131 | |
|---|
| 132 | /*************************************************************************** |
|---|
| 133 | Summary: |
|---|
| 134 | Event Group Handle |
|---|
| 135 | ***************************************************************************/ |
|---|
| 136 | typedef void *B_EventGroupHandle; |
|---|
| 137 | |
|---|
| 138 | /*************************************************************************** |
|---|
| 139 | Summary: |
|---|
| 140 | Message Queue Handle |
|---|
| 141 | ***************************************************************************/ |
|---|
| 142 | typedef struct B_MessageQueue *B_MessageQueueHandle; |
|---|
| 143 | |
|---|
| 144 | /*************************************************************************** |
|---|
| 145 | Summary: |
|---|
| 146 | Thread Handle |
|---|
| 147 | ***************************************************************************/ |
|---|
| 148 | typedef struct B_Thread *B_ThreadHandle; |
|---|
| 149 | |
|---|
| 150 | /*************************************************************************** |
|---|
| 151 | Summary: |
|---|
| 152 | Thread Main Loop Function |
|---|
| 153 | ***************************************************************************/ |
|---|
| 154 | typedef void (*B_ThreadFunc)(void *pParam); |
|---|
| 155 | |
|---|
| 156 | /*************************************************************************** |
|---|
| 157 | Summary: |
|---|
| 158 | Scheduler Handle |
|---|
| 159 | |
|---|
| 160 | Description: |
|---|
| 161 | A Scheduler is a higher-level OS construct. A scheduler is responsible for |
|---|
| 162 | dispatching synchronized timer and event callbacks using a single thread. |
|---|
| 163 | ***************************************************************************/ |
|---|
| 164 | typedef struct B_Scheduler *B_SchedulerHandle; |
|---|
| 165 | |
|---|
| 166 | /*************************************************************************** |
|---|
| 167 | Summary: |
|---|
| 168 | Scheduler Event Callback Prototype |
|---|
| 169 | ***************************************************************************/ |
|---|
| 170 | typedef void (*B_EventCallback)(void *pContext); |
|---|
| 171 | |
|---|
| 172 | /*************************************************************************** |
|---|
| 173 | Summary: |
|---|
| 174 | Scheduler Event Callback Handle |
|---|
| 175 | ***************************************************************************/ |
|---|
| 176 | typedef struct B_SchedulerEvent *B_SchedulerEventId; |
|---|
| 177 | |
|---|
| 178 | /*************************************************************************** |
|---|
| 179 | Summary: |
|---|
| 180 | Scheduler Timer Callback Prototype |
|---|
| 181 | ***************************************************************************/ |
|---|
| 182 | typedef void (*B_TimerCallback)(void *pContext); |
|---|
| 183 | |
|---|
| 184 | /*************************************************************************** |
|---|
| 185 | Summary: |
|---|
| 186 | Scheduler Timer Handle |
|---|
| 187 | ***************************************************************************/ |
|---|
| 188 | typedef struct B_SchedulerTimer *B_SchedulerTimerId; |
|---|
| 189 | |
|---|
| 190 | /*************************************************************************** |
|---|
| 191 | Summary: |
|---|
| 192 | Scheduler Callback Handle |
|---|
| 193 | ***************************************************************************/ |
|---|
| 194 | typedef struct B_SchedulerCallback *B_SchedulerCallbackHandle; |
|---|
| 195 | |
|---|
| 196 | /*************************************************************************** |
|---|
| 197 | Summary: |
|---|
| 198 | Scheduler Callback Function Prototype |
|---|
| 199 | ***************************************************************************/ |
|---|
| 200 | typedef void (*B_SchedulerCallbackFunction)(void *pParam1, int param2); |
|---|
| 201 | |
|---|
| 202 | /*************************************************************************** |
|---|
| 203 | Shortcuts for event timeouts |
|---|
| 204 | ***************************************************************************/ |
|---|
| 205 | #define B_WAIT_NONE (0) /* Avoid blocking while waiting for events */ |
|---|
| 206 | #define B_WAIT_FOREVER (-1) /* Block forever waiting for events */ |
|---|
| 207 | |
|---|
| 208 | /*************************************************************************** |
|---|
| 209 | Summary: |
|---|
| 210 | Allocate Memory (malloc equivalent) |
|---|
| 211 | ***************************************************************************/ |
|---|
| 212 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 213 | #define B_Os_Malloc(numBytes) B_Os_Malloc_tagged((numBytes), __FUNCTION__, __LINE__) |
|---|
| 214 | #else |
|---|
| 215 | #define B_Os_Malloc(numBytes) B_Os_Malloc_tagged((numBytes), NULL, 0) |
|---|
| 216 | #endif |
|---|
| 217 | void *B_Os_Malloc_tagged( |
|---|
| 218 | size_t numBytes, |
|---|
| 219 | const char *pFunction, |
|---|
| 220 | int line |
|---|
| 221 | ); |
|---|
| 222 | |
|---|
| 223 | /*************************************************************************** |
|---|
| 224 | Summary: |
|---|
| 225 | Free Memory (free equivalent) |
|---|
| 226 | ***************************************************************************/ |
|---|
| 227 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 228 | #define B_Os_Free(pMemory) B_Os_Free_tagged((pMemory), __FUNCTION__, __LINE__) |
|---|
| 229 | #else |
|---|
| 230 | #define B_Os_Free(pMemory) B_Os_Free_tagged((pMemory), NULL, 0) |
|---|
| 231 | #endif |
|---|
| 232 | void B_Os_Free_tagged( |
|---|
| 233 | void *pMemory, |
|---|
| 234 | const char *pFunction, |
|---|
| 235 | int line |
|---|
| 236 | ); |
|---|
| 237 | |
|---|
| 238 | /*************************************************************************** |
|---|
| 239 | Summary: |
|---|
| 240 | Allocate and Clear memory (calloc equivalent) |
|---|
| 241 | ***************************************************************************/ |
|---|
| 242 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 243 | #define B_Os_Calloc(numMembers, numBytes) B_Os_Calloc_tagged((numMembers), (numBytes), __FUNCTION__, __LINE__) |
|---|
| 244 | #else |
|---|
| 245 | #define B_Os_Calloc(numMembers, numBytes) B_Os_Calloc_tagged((numMembers), (numBytes), NULL, 0) |
|---|
| 246 | #endif |
|---|
| 247 | void *B_Os_Calloc_tagged( |
|---|
| 248 | size_t numMembers, |
|---|
| 249 | size_t memberSize, |
|---|
| 250 | const char *pFunction, |
|---|
| 251 | int line |
|---|
| 252 | ); |
|---|
| 253 | |
|---|
| 254 | /*************************************************************************** |
|---|
| 255 | Summary: |
|---|
| 256 | Re-Allocate memory (realloc equivalent) |
|---|
| 257 | ***************************************************************************/ |
|---|
| 258 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 259 | #define B_Os_Realloc(pMemory, numBytes) B_Os_Realloc_tagged((pMemory), (numBytes), __FUNCTION__, __LINE__) |
|---|
| 260 | #else |
|---|
| 261 | #define B_Os_Realloc(pMemory, numBytes) B_Os_Realloc_tagged((pMemory), (numBytes), NULL, 0) |
|---|
| 262 | #endif |
|---|
| 263 | void *B_Os_Realloc_tagged( |
|---|
| 264 | void *pMemory, |
|---|
| 265 | size_t numBytes, |
|---|
| 266 | const char *pFunction, |
|---|
| 267 | int line |
|---|
| 268 | ); |
|---|
| 269 | |
|---|
| 270 | /*************************************************************************** |
|---|
| 271 | Summary: |
|---|
| 272 | Mutex settings. Currently a placeholder. |
|---|
| 273 | ***************************************************************************/ |
|---|
| 274 | typedef struct { int notused; } B_MutexSettings; |
|---|
| 275 | |
|---|
| 276 | /*************************************************************************** |
|---|
| 277 | Summary: |
|---|
| 278 | Get default mutex settings. |
|---|
| 279 | ***************************************************************************/ |
|---|
| 280 | void B_Mutex_GetDefaultSettings( |
|---|
| 281 | B_MutexSettings *pSettings /* [out] */ |
|---|
| 282 | ); |
|---|
| 283 | |
|---|
| 284 | /*************************************************************************** |
|---|
| 285 | Summary: |
|---|
| 286 | Create a mutex using the specified settings. |
|---|
| 287 | ***************************************************************************/ |
|---|
| 288 | B_MutexHandle B_Mutex_Create( |
|---|
| 289 | const B_MutexSettings *pSettings /* Pass NULL for defaults */ |
|---|
| 290 | ); |
|---|
| 291 | |
|---|
| 292 | /*************************************************************************** |
|---|
| 293 | Summary: |
|---|
| 294 | Destroy a mutex |
|---|
| 295 | ***************************************************************************/ |
|---|
| 296 | void B_Mutex_Destroy( |
|---|
| 297 | B_MutexHandle mutex |
|---|
| 298 | ); |
|---|
| 299 | |
|---|
| 300 | /*************************************************************************** |
|---|
| 301 | Summary: |
|---|
| 302 | Lock a mutex |
|---|
| 303 | ***************************************************************************/ |
|---|
| 304 | void B_Mutex_Lock( |
|---|
| 305 | B_MutexHandle mutex |
|---|
| 306 | ); |
|---|
| 307 | |
|---|
| 308 | /*************************************************************************** |
|---|
| 309 | Summary: |
|---|
| 310 | Try to lock a mutex. |
|---|
| 311 | |
|---|
| 312 | Description: |
|---|
| 313 | This will attempt to lock the mutex without blocking. If the mutex is |
|---|
| 314 | available, it will be locked and B_Error_eSuccess will be returned. If |
|---|
| 315 | the mutex is unavailable, B_Error_eTimeout will be returned. |
|---|
| 316 | ***************************************************************************/ |
|---|
| 317 | B_Error B_Mutex_TryLock( |
|---|
| 318 | B_MutexHandle mutex |
|---|
| 319 | ); |
|---|
| 320 | |
|---|
| 321 | /*************************************************************************** |
|---|
| 322 | Summary: |
|---|
| 323 | Unlock a mutex. |
|---|
| 324 | |
|---|
| 325 | Description: |
|---|
| 326 | Mutexes must be unlocked by the same thread that successfully called |
|---|
| 327 | B_Mutex_Lock or B_Mutex_TryLock. |
|---|
| 328 | ***************************************************************************/ |
|---|
| 329 | void B_Mutex_Unlock( |
|---|
| 330 | B_MutexHandle mutex |
|---|
| 331 | ); |
|---|
| 332 | |
|---|
| 333 | /*************************************************************************** |
|---|
| 334 | Summary: |
|---|
| 335 | Verify that a mutex is locked by the calling thread |
|---|
| 336 | |
|---|
| 337 | Description: |
|---|
| 338 | This can be used to facilitate debugging |
|---|
| 339 | ***************************************************************************/ |
|---|
| 340 | #define B_MUTEX_ASSERT_LOCKED(mutex) BDBG_ASSERT(true == B_Mutex_P_IsLocked((mutex))) |
|---|
| 341 | bool B_Mutex_P_IsLocked( |
|---|
| 342 | B_MutexHandle mutex |
|---|
| 343 | ); |
|---|
| 344 | |
|---|
| 345 | /*************************************************************************** |
|---|
| 346 | Summary: |
|---|
| 347 | Event settings. Currently a placeholder. |
|---|
| 348 | ***************************************************************************/ |
|---|
| 349 | typedef struct { int notused; } B_EventSettings; |
|---|
| 350 | |
|---|
| 351 | /*************************************************************************** |
|---|
| 352 | Summary: |
|---|
| 353 | Get default event settings. |
|---|
| 354 | ***************************************************************************/ |
|---|
| 355 | void B_Event_GetDefaultSettings( |
|---|
| 356 | B_EventSettings *pSettings /* [out] */ |
|---|
| 357 | ); |
|---|
| 358 | |
|---|
| 359 | /*************************************************************************** |
|---|
| 360 | Summary: |
|---|
| 361 | Create an event |
|---|
| 362 | ***************************************************************************/ |
|---|
| 363 | B_EventHandle B_Event_Create( |
|---|
| 364 | const B_EventSettings *pSettings /* Pass NULL for defaults */ |
|---|
| 365 | ); |
|---|
| 366 | |
|---|
| 367 | /*************************************************************************** |
|---|
| 368 | Summary: |
|---|
| 369 | Destroy an event |
|---|
| 370 | ***************************************************************************/ |
|---|
| 371 | void B_Event_Destroy( |
|---|
| 372 | B_EventHandle event |
|---|
| 373 | ); |
|---|
| 374 | |
|---|
| 375 | /*************************************************************************** |
|---|
| 376 | Summary: |
|---|
| 377 | Set an event |
|---|
| 378 | ***************************************************************************/ |
|---|
| 379 | void B_Event_Set( |
|---|
| 380 | B_EventHandle event |
|---|
| 381 | ); |
|---|
| 382 | |
|---|
| 383 | /*************************************************************************** |
|---|
| 384 | Summary: |
|---|
| 385 | Wait for an event to be set |
|---|
| 386 | |
|---|
| 387 | Description: |
|---|
| 388 | This routine will wait for the specified event to be set. If the event |
|---|
| 389 | was set, B_Error_eSuccess will be returned. If the event was not set, |
|---|
| 390 | B_Error_eTimeout will be returned. |
|---|
| 391 | ***************************************************************************/ |
|---|
| 392 | B_Error B_Event_Wait( |
|---|
| 393 | B_EventHandle event, |
|---|
| 394 | int timeoutMsec /* Timeout in milliseconds. Pass B_WAIT_FOREVER to wait |
|---|
| 395 | forever or B_WAIT_NONE to avoid blocking */ |
|---|
| 396 | ); |
|---|
| 397 | |
|---|
| 398 | /*************************************************************************** |
|---|
| 399 | Summary: |
|---|
| 400 | Clear an event if it has been set |
|---|
| 401 | ***************************************************************************/ |
|---|
| 402 | void B_Event_Reset( |
|---|
| 403 | B_EventHandle event |
|---|
| 404 | ); |
|---|
| 405 | |
|---|
| 406 | /*************************************************************************** |
|---|
| 407 | Summary: |
|---|
| 408 | Event Group Settings. Currently a placeholder. |
|---|
| 409 | ***************************************************************************/ |
|---|
| 410 | typedef void B_EventGroupSettings; |
|---|
| 411 | |
|---|
| 412 | /*************************************************************************** |
|---|
| 413 | Summary: |
|---|
| 414 | Get default event group settings |
|---|
| 415 | ***************************************************************************/ |
|---|
| 416 | void B_EventGroup_GetDefaultSettings( |
|---|
| 417 | B_EventGroupSettings *pSettings /* [out] */ |
|---|
| 418 | ); |
|---|
| 419 | |
|---|
| 420 | /*************************************************************************** |
|---|
| 421 | Summary: |
|---|
| 422 | Create an event group |
|---|
| 423 | ***************************************************************************/ |
|---|
| 424 | B_EventGroupHandle B_EventGroup_Create( |
|---|
| 425 | const B_EventGroupSettings *pSettings |
|---|
| 426 | ); |
|---|
| 427 | |
|---|
| 428 | /*************************************************************************** |
|---|
| 429 | Summary: |
|---|
| 430 | Destroy an event group |
|---|
| 431 | ***************************************************************************/ |
|---|
| 432 | void B_EventGroup_Destroy( |
|---|
| 433 | B_EventGroupHandle group |
|---|
| 434 | ); |
|---|
| 435 | |
|---|
| 436 | /*************************************************************************** |
|---|
| 437 | Summary: |
|---|
| 438 | Add an event to the group |
|---|
| 439 | ***************************************************************************/ |
|---|
| 440 | B_Error B_EventGroup_AddEvent( |
|---|
| 441 | B_EventGroupHandle group, |
|---|
| 442 | B_EventHandle event |
|---|
| 443 | ); |
|---|
| 444 | |
|---|
| 445 | /*************************************************************************** |
|---|
| 446 | Summary: |
|---|
| 447 | Remove an event from the group |
|---|
| 448 | ***************************************************************************/ |
|---|
| 449 | B_Error B_EventGroup_RemoveEvent( |
|---|
| 450 | B_EventGroupHandle group, |
|---|
| 451 | B_EventHandle event |
|---|
| 452 | ); |
|---|
| 453 | |
|---|
| 454 | /*************************************************************************** |
|---|
| 455 | Summary: |
|---|
| 456 | Wait for any event in the group to be set. |
|---|
| 457 | |
|---|
| 458 | Description: |
|---|
| 459 | This routine will wait for the specified event to be set. If the event |
|---|
| 460 | was set, B_Error_eSuccess will be returned. If the event was not set, |
|---|
| 461 | B_Error_eTimeout will be returned. |
|---|
| 462 | ***************************************************************************/ |
|---|
| 463 | B_Error B_EventGroup_Wait( |
|---|
| 464 | B_EventGroupHandle group, |
|---|
| 465 | int timeoutMsec, /* Timeout in milliseconds. Pass B_WAIT_FOREVER to wait |
|---|
| 466 | forever or B_WAIT_NONE to avoid blocking */ |
|---|
| 467 | B_EventHandle *pTriggeredEvents, /* Triggered events handles will be copied into this array */ |
|---|
| 468 | unsigned maxTriggeredEvents, /* Size of triggered event array */ |
|---|
| 469 | unsigned *pNumTriggeredEvents /* [out] Number of triggered events */ |
|---|
| 470 | ); |
|---|
| 471 | |
|---|
| 472 | /*************************************************************************** |
|---|
| 473 | Summary: |
|---|
| 474 | Delay a thread for the specified time |
|---|
| 475 | |
|---|
| 476 | Description: |
|---|
| 477 | This will busy-wait a thread for the specified time without yielding. |
|---|
| 478 | ***************************************************************************/ |
|---|
| 479 | void B_Thread_Delay( |
|---|
| 480 | int microseconds |
|---|
| 481 | ); |
|---|
| 482 | |
|---|
| 483 | /*************************************************************************** |
|---|
| 484 | Summary: |
|---|
| 485 | Suspend a thread for the specified time |
|---|
| 486 | |
|---|
| 487 | Description: |
|---|
| 488 | This will yield the CPU for the specified time. May round up to the nearest |
|---|
| 489 | OS scheduler tick value. |
|---|
| 490 | ***************************************************************************/ |
|---|
| 491 | void B_Thread_Sleep( |
|---|
| 492 | int milliseconds |
|---|
| 493 | ); |
|---|
| 494 | |
|---|
| 495 | /*************************************************************************** |
|---|
| 496 | Summary: |
|---|
| 497 | Thread Settings |
|---|
| 498 | ***************************************************************************/ |
|---|
| 499 | typedef struct B_ThreadSettings |
|---|
| 500 | { |
|---|
| 501 | int priority; /* 0 = maximum, 100=minimum */ |
|---|
| 502 | int stackSize; /* In bytes, 0 will use an OS default */ |
|---|
| 503 | bool terminate; /* If true, B_Thread_Destroy will terminate the thread. |
|---|
| 504 | Otherwise, B_Thread_Destroy will wait for the thread |
|---|
| 505 | to exit from some external signal. */ |
|---|
| 506 | } B_ThreadSettings; |
|---|
| 507 | |
|---|
| 508 | /*************************************************************************** |
|---|
| 509 | Summary: |
|---|
| 510 | Get Default Thread Settings |
|---|
| 511 | ***************************************************************************/ |
|---|
| 512 | void B_Thread_GetDefaultSettings( |
|---|
| 513 | B_ThreadSettings *pSettings /* [out] */ |
|---|
| 514 | ); |
|---|
| 515 | |
|---|
| 516 | /*************************************************************************** |
|---|
| 517 | Summary: |
|---|
| 518 | Create a thread using the specified settings |
|---|
| 519 | ***************************************************************************/ |
|---|
| 520 | B_ThreadHandle B_Thread_Create( |
|---|
| 521 | const char *pName, /* Thread Name - Optional */ |
|---|
| 522 | B_ThreadFunc threadFunction, /* Thread Main Routine */ |
|---|
| 523 | void *pThreadParam, /* Parameter provided to threadFunction */ |
|---|
| 524 | const B_ThreadSettings *pSettings /* Pass NULL for defaults */ |
|---|
| 525 | ); |
|---|
| 526 | |
|---|
| 527 | /*************************************************************************** |
|---|
| 528 | Summary: |
|---|
| 529 | Destroy a thread. |
|---|
| 530 | |
|---|
| 531 | Description: |
|---|
| 532 | This will signal the thread to exit and will wait for the thread to |
|---|
| 533 | terminate before returning. |
|---|
| 534 | ***************************************************************************/ |
|---|
| 535 | void B_Thread_Destroy( |
|---|
| 536 | B_ThreadHandle thread |
|---|
| 537 | ); |
|---|
| 538 | |
|---|
| 539 | /*************************************************************************** |
|---|
| 540 | Summary: |
|---|
| 541 | Get a handle to the currently executing thread |
|---|
| 542 | |
|---|
| 543 | Description: |
|---|
| 544 | This will return NULL if the thread was not created with B_Thread_Create |
|---|
| 545 | ***************************************************************************/ |
|---|
| 546 | B_ThreadHandle B_Thread_GetSelf(void); |
|---|
| 547 | |
|---|
| 548 | /*************************************************************************** |
|---|
| 549 | Summary: |
|---|
| 550 | Get a handle to the currently executing thread |
|---|
| 551 | |
|---|
| 552 | Description: |
|---|
| 553 | This will return a thread ID for any thread, not just those created by |
|---|
| 554 | B_Thread_Create. This is useful for bprofile. |
|---|
| 555 | ***************************************************************************/ |
|---|
| 556 | B_Error B_Thread_GetId( |
|---|
| 557 | B_ThreadHandle thread, /* Pass NULL for the current thread */ |
|---|
| 558 | int *pId |
|---|
| 559 | ); |
|---|
| 560 | |
|---|
| 561 | /*************************************************************************** |
|---|
| 562 | Summary: |
|---|
| 563 | Get the name of a specified thread for debugging. |
|---|
| 564 | |
|---|
| 565 | Description: |
|---|
| 566 | This will return the name of a thread specified, and will return |
|---|
| 567 | an error for those threads not created with B_ThreadCreate. |
|---|
| 568 | ***************************************************************************/ |
|---|
| 569 | B_Error B_Thread_GetName( |
|---|
| 570 | B_ThreadHandle thread, /* Pass NULL for current thread */ |
|---|
| 571 | char *pName, /* Pointer to array for name */ |
|---|
| 572 | int maxNameLen /* Size of name array */ |
|---|
| 573 | ); |
|---|
| 574 | |
|---|
| 575 | /*************************************************************************** |
|---|
| 576 | Summary: |
|---|
| 577 | Get the name of a specified thread for debugging. |
|---|
| 578 | |
|---|
| 579 | Description: |
|---|
| 580 | This will return the name of a thread specified, and will return |
|---|
| 581 | an error for those threads not created with B_ThreadCreate. This is useful |
|---|
| 582 | for bprofile. |
|---|
| 583 | ***************************************************************************/ |
|---|
| 584 | B_Error B_Thread_GetNameFromId( |
|---|
| 585 | int id, /* ID returned by B_Thread_GetId */ |
|---|
| 586 | char *pName, /* Pointer to array for name */ |
|---|
| 587 | int maxNameLen /* Size of name array */ |
|---|
| 588 | ); |
|---|
| 589 | |
|---|
| 590 | /*************************************************************************** |
|---|
| 591 | Summary: |
|---|
| 592 | Scheduler settings. Currently a placeholder. |
|---|
| 593 | ***************************************************************************/ |
|---|
| 594 | typedef void B_SchedulerSettings; |
|---|
| 595 | |
|---|
| 596 | /*************************************************************************** |
|---|
| 597 | Summary: |
|---|
| 598 | Get Default Scheduler Settings |
|---|
| 599 | ***************************************************************************/ |
|---|
| 600 | void B_Scheduler_GetDefaultSettings( |
|---|
| 601 | B_SchedulerSettings *pSettings /* [out] */ |
|---|
| 602 | ); |
|---|
| 603 | |
|---|
| 604 | /*************************************************************************** |
|---|
| 605 | Summary: |
|---|
| 606 | Craete a scheduler |
|---|
| 607 | ***************************************************************************/ |
|---|
| 608 | B_SchedulerHandle B_Scheduler_Create( |
|---|
| 609 | const B_SchedulerSettings *pSettings |
|---|
| 610 | ); |
|---|
| 611 | |
|---|
| 612 | /*************************************************************************** |
|---|
| 613 | Summary: |
|---|
| 614 | Destroy a scheduler |
|---|
| 615 | ***************************************************************************/ |
|---|
| 616 | void B_Scheduler_Destroy( |
|---|
| 617 | B_SchedulerHandle scheduler |
|---|
| 618 | ); |
|---|
| 619 | |
|---|
| 620 | /*************************************************************************** |
|---|
| 621 | Summary: |
|---|
| 622 | Register an event with a scheduler |
|---|
| 623 | |
|---|
| 624 | Description: |
|---|
| 625 | This will cause the scheduler to call the sepecified EventCallback function |
|---|
| 626 | when the specified event has been set. Prior to calling the callback, |
|---|
| 627 | the specified mutex will be automatically locked. After the callback |
|---|
| 628 | returns, the mutex will be unlocked. |
|---|
| 629 | ***************************************************************************/ |
|---|
| 630 | B_SchedulerEventId B_Scheduler_RegisterEvent( |
|---|
| 631 | B_SchedulerHandle scheduler, |
|---|
| 632 | B_MutexHandle mutex, /* Mutex to lock prior to calling callback */ |
|---|
| 633 | B_EventHandle event, /* Event that will trigger the callback */ |
|---|
| 634 | B_EventCallback callback, /* Callback routine to execute when event is set */ |
|---|
| 635 | void *pContext /* Value passed to callback routine */ |
|---|
| 636 | ); |
|---|
| 637 | |
|---|
| 638 | /*************************************************************************** |
|---|
| 639 | Summary: |
|---|
| 640 | Un-Register an event from the scheduler |
|---|
| 641 | ***************************************************************************/ |
|---|
| 642 | void B_Scheduler_UnregisterEvent( |
|---|
| 643 | B_SchedulerHandle scheduler, |
|---|
| 644 | B_SchedulerEventId eventId |
|---|
| 645 | ); |
|---|
| 646 | |
|---|
| 647 | /*************************************************************************** |
|---|
| 648 | Summary: |
|---|
| 649 | Schedule a timer |
|---|
| 650 | |
|---|
| 651 | Description: |
|---|
| 652 | This will start a timer that will lock the specified mutex and call the |
|---|
| 653 | specified callback routine when the timer expires. Timers are one-shot, |
|---|
| 654 | and they must be rescheduled to repeat. Re-scheduling the timer from the |
|---|
| 655 | callback itself is allowed. After the timer expires, the TimerId value |
|---|
| 656 | returned becomes invalid and should be discarded. |
|---|
| 657 | ***************************************************************************/ |
|---|
| 658 | B_SchedulerTimerId B_Scheduler_StartTimer( |
|---|
| 659 | B_SchedulerHandle scheduler, |
|---|
| 660 | B_MutexHandle mutex, /* Mutex to lock prior to calling callback */ |
|---|
| 661 | int timeoutMsec, /* Timer expiration time in msec */ |
|---|
| 662 | B_TimerCallback callback, /* Callback to call when timer expires */ |
|---|
| 663 | void *pContext /* Value passed to callback routine */ |
|---|
| 664 | ); |
|---|
| 665 | |
|---|
| 666 | /*************************************************************************** |
|---|
| 667 | Summary: |
|---|
| 668 | Cancel a timer |
|---|
| 669 | |
|---|
| 670 | Description: |
|---|
| 671 | This will cancel a timer that was previously started. If the timer has |
|---|
| 672 | already expired, this call is safe. If the callback has actually been |
|---|
| 673 | called, this will generate a warning message. If the timer has expired and |
|---|
| 674 | the callback is pending on the mutex, the timer will be discarded and no |
|---|
| 675 | warning will occur. |
|---|
| 676 | ***************************************************************************/ |
|---|
| 677 | void B_Scheduler_CancelTimer( |
|---|
| 678 | B_SchedulerHandle scheduler, |
|---|
| 679 | B_SchedulerTimerId timerId |
|---|
| 680 | ); |
|---|
| 681 | |
|---|
| 682 | /*************************************************************************** |
|---|
| 683 | Summary: |
|---|
| 684 | Run a scheduler |
|---|
| 685 | |
|---|
| 686 | Description: |
|---|
| 687 | This routine will drive the scheduler execution. It will not return until |
|---|
| 688 | B_Scheduler_Stop is called. |
|---|
| 689 | ***************************************************************************/ |
|---|
| 690 | void B_Scheduler_Run( |
|---|
| 691 | B_SchedulerHandle scheduler |
|---|
| 692 | ); |
|---|
| 693 | |
|---|
| 694 | /*************************************************************************** |
|---|
| 695 | Summary: |
|---|
| 696 | Stop a scheduler |
|---|
| 697 | |
|---|
| 698 | Description: |
|---|
| 699 | This routine will stop the scheduler execution. After this is called, |
|---|
| 700 | B_Scheduler_Run will return. This routine can be called by any thread, |
|---|
| 701 | including the scheduler thread itself. It is not synchronized, so this call |
|---|
| 702 | may return before B_Scheduler_Run returns. |
|---|
| 703 | ***************************************************************************/ |
|---|
| 704 | void B_Scheduler_Stop( |
|---|
| 705 | B_SchedulerHandle scheduler |
|---|
| 706 | ); |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | /*************************************************************************** |
|---|
| 710 | Summary: |
|---|
| 711 | Settings for a scheduler callback instance |
|---|
| 712 | ***************************************************************************/ |
|---|
| 713 | typedef void B_SchedulerCallbackSettings; /* Placeholder */ |
|---|
| 714 | |
|---|
| 715 | /*************************************************************************** |
|---|
| 716 | Summary: |
|---|
| 717 | Get default settings for a scheduler callback instance |
|---|
| 718 | ***************************************************************************/ |
|---|
| 719 | void B_SchedulerCallback_GetDefaultSettings( |
|---|
| 720 | B_SchedulerCallbackSettings *pSettings /* [out] */ |
|---|
| 721 | ); |
|---|
| 722 | |
|---|
| 723 | /*************************************************************************** |
|---|
| 724 | Summary: |
|---|
| 725 | Register an asynchronous callback |
|---|
| 726 | |
|---|
| 727 | Description: |
|---|
| 728 | This will cancel a timer that was previously started. If the timer has |
|---|
| 729 | already expired, this call is safe. If the callback has actually been |
|---|
| 730 | called, this will generate a warning message. If the timer has expired and |
|---|
| 731 | the callback is pending on the mutex, the timer will be discarded and no |
|---|
| 732 | warning will occur. |
|---|
| 733 | ***************************************************************************/ |
|---|
| 734 | B_SchedulerCallbackHandle B_SchedulerCallback_Create( |
|---|
| 735 | B_SchedulerHandle scheduler, |
|---|
| 736 | void *pInstanceHandle, /* Pass the handle to the application object's instance here */ |
|---|
| 737 | const B_SchedulerCallbackSettings *pSettings /* Pass NULL for defaults */ |
|---|
| 738 | ); |
|---|
| 739 | |
|---|
| 740 | /*************************************************************************** |
|---|
| 741 | Summary: |
|---|
| 742 | Set the function to be used in an asynchronous callback |
|---|
| 743 | ***************************************************************************/ |
|---|
| 744 | B_Error B_SchedulerCallback_SetFunction( |
|---|
| 745 | B_SchedulerCallbackHandle callbackHandle, |
|---|
| 746 | B_SchedulerCallbackFunction callbackFunction, /* Function to be called */ |
|---|
| 747 | void *pParam1, /* First function parameter */ |
|---|
| 748 | int param2 /* Second function parameter */ |
|---|
| 749 | ); |
|---|
| 750 | |
|---|
| 751 | /*************************************************************************** |
|---|
| 752 | Summary: |
|---|
| 753 | Fire an asynchronous callback |
|---|
| 754 | |
|---|
| 755 | Description: |
|---|
| 756 | This will schedule the callback to be executed when the scheduler becomes |
|---|
| 757 | idle. If the same callback is fired multiple times before it is able to |
|---|
| 758 | be called, only a single application callback will be made. |
|---|
| 759 | ***************************************************************************/ |
|---|
| 760 | void B_SchedulerCallback_Fire( |
|---|
| 761 | B_SchedulerCallbackHandle callback |
|---|
| 762 | ); |
|---|
| 763 | |
|---|
| 764 | /*************************************************************************** |
|---|
| 765 | Summary: |
|---|
| 766 | Destroying an asynchronous callback |
|---|
| 767 | ***************************************************************************/ |
|---|
| 768 | void B_SchedulerCallback_Destroy( |
|---|
| 769 | B_SchedulerCallbackHandle callback |
|---|
| 770 | ); |
|---|
| 771 | |
|---|
| 772 | /*************************************************************************** |
|---|
| 773 | Summary: |
|---|
| 774 | Cancel all pending callbacks for this object instance |
|---|
| 775 | |
|---|
| 776 | Description: |
|---|
| 777 | Because of the asynchronous nature of scheduler callbacks, race conditions |
|---|
| 778 | can occur when closing objects that have pending callbacks. In order to |
|---|
| 779 | guarantee that all pending callbacks have been stopped for an instance, |
|---|
| 780 | this function should be called as part of closing the object instance. |
|---|
| 781 | |
|---|
| 782 | NOTE: It is important to call this routine before acquiring your instance's |
|---|
| 783 | mutex to avoid a race condition where a callback into your module may still |
|---|
| 784 | be pending due to contention on the module's mutex. As an example: |
|---|
| 785 | |
|---|
| 786 | void B_Object_Close(B_ObjectHandle handle) |
|---|
| 787 | { |
|---|
| 788 | BDBG_ASSERT(NULL != handle); |
|---|
| 789 | |
|---|
| 790 | B_Scheduler_StopCallbacks(handle->scheduler); |
|---|
| 791 | |
|---|
| 792 | // Now it's guaranteed that no callbacks are pending. |
|---|
| 793 | |
|---|
| 794 | B_Mutex_Lock(handle->mutex); |
|---|
| 795 | |
|---|
| 796 | // Actually clean up now |
|---|
| 797 | } |
|---|
| 798 | ***************************************************************************/ |
|---|
| 799 | void B_Scheduler_StopCallbacks( |
|---|
| 800 | B_SchedulerHandle scheduler, |
|---|
| 801 | void *pInstanceHandle |
|---|
| 802 | ); |
|---|
| 803 | |
|---|
| 804 | /*************************************************************************** |
|---|
| 805 | Summary: |
|---|
| 806 | Get current system time |
|---|
| 807 | |
|---|
| 808 | Description: |
|---|
| 809 | The value returned in this call is only useful in the B_Time_Add and |
|---|
| 810 | B_Time_Diff routines, it is not meant for direct access to a system time |
|---|
| 811 | value. |
|---|
| 812 | ***************************************************************************/ |
|---|
| 813 | void B_Time_Get( |
|---|
| 814 | B_Time *pTime |
|---|
| 815 | ); |
|---|
| 816 | |
|---|
| 817 | /*************************************************************************** |
|---|
| 818 | Summary: |
|---|
| 819 | Return the delta between two time values in milliseconds |
|---|
| 820 | ***************************************************************************/ |
|---|
| 821 | long B_Time_Diff( |
|---|
| 822 | const B_Time *pFuture, |
|---|
| 823 | const B_Time *pPast |
|---|
| 824 | ); |
|---|
| 825 | |
|---|
| 826 | /*************************************************************************** |
|---|
| 827 | Summary: |
|---|
| 828 | Add a specified number of milliseconds to a time value |
|---|
| 829 | ***************************************************************************/ |
|---|
| 830 | void B_Time_Add( |
|---|
| 831 | B_Time *pTime, |
|---|
| 832 | long deltaMsec |
|---|
| 833 | ); |
|---|
| 834 | |
|---|
| 835 | /*************************************************************************** |
|---|
| 836 | Summary: |
|---|
| 837 | Message Queue Settings |
|---|
| 838 | ***************************************************************************/ |
|---|
| 839 | typedef struct B_MessageQueueSettings |
|---|
| 840 | { |
|---|
| 841 | size_t maxMessageSize; /* Maximum size of an individual message */ |
|---|
| 842 | size_t maxMessages; /* Maximum messages in the queue at a time */ |
|---|
| 843 | } B_MessageQueueSettings; |
|---|
| 844 | |
|---|
| 845 | /*************************************************************************** |
|---|
| 846 | Summary: |
|---|
| 847 | Get Default Message Queue Settings |
|---|
| 848 | ***************************************************************************/ |
|---|
| 849 | void B_MessageQueue_GetDefaultSettings( |
|---|
| 850 | B_MessageQueueSettings *pSettings /* [out] */ |
|---|
| 851 | ); |
|---|
| 852 | |
|---|
| 853 | /*************************************************************************** |
|---|
| 854 | Summary: |
|---|
| 855 | Create a message queue |
|---|
| 856 | ***************************************************************************/ |
|---|
| 857 | B_MessageQueueHandle B_MessageQueue_Create( |
|---|
| 858 | const B_MessageQueueSettings *pSettings |
|---|
| 859 | ); |
|---|
| 860 | |
|---|
| 861 | /*************************************************************************** |
|---|
| 862 | Summary: |
|---|
| 863 | Destroy a message queue |
|---|
| 864 | ***************************************************************************/ |
|---|
| 865 | void B_MessageQueue_Destroy( |
|---|
| 866 | B_MessageQueueHandle handle |
|---|
| 867 | ); |
|---|
| 868 | |
|---|
| 869 | /*************************************************************************** |
|---|
| 870 | Summary: |
|---|
| 871 | Post (Send) a message to a queue |
|---|
| 872 | |
|---|
| 873 | Description: |
|---|
| 874 | This function will return B_ERROR_SUCCESS if the message was sent |
|---|
| 875 | successfully or B_ERROR_TIMEOUT if the message queue is full. |
|---|
| 876 | ***************************************************************************/ |
|---|
| 877 | B_Error B_MessageQueue_Post( |
|---|
| 878 | B_MessageQueueHandle handle, |
|---|
| 879 | const void *pMessageData, |
|---|
| 880 | size_t messageSize |
|---|
| 881 | ); |
|---|
| 882 | |
|---|
| 883 | /*************************************************************************** |
|---|
| 884 | Summary: |
|---|
| 885 | Receive a message from a queue |
|---|
| 886 | |
|---|
| 887 | Description: |
|---|
| 888 | This function will return B_ERROR_SUCCESS if a message was successfully |
|---|
| 889 | received or B_ERROR_TIMEOUT if the specified timeout value was reached. |
|---|
| 890 | ***************************************************************************/ |
|---|
| 891 | B_Error B_MessageQueue_Wait( |
|---|
| 892 | B_MessageQueueHandle handle, |
|---|
| 893 | void *pMessageBuffer, |
|---|
| 894 | size_t messageBufferSize, |
|---|
| 895 | size_t *pMessageSizeReceived, |
|---|
| 896 | int timeoutMsec /* Values B_WAIT_NONE and B_WAIT_FOREVER may be used */ |
|---|
| 897 | ); |
|---|
| 898 | |
|---|
| 899 | #ifdef __cplusplus |
|---|
| 900 | } |
|---|
| 901 | #endif |
|---|
| 902 | |
|---|
| 903 | #endif /* #ifndef B_OS_LIB_H__ */ |
|---|
| 904 | |
|---|