| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-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: bsettop_os.c $ |
|---|
| 11 | * $brcm_Revision: 8 $ |
|---|
| 12 | * $brcm_Date: 8/18/09 6:38p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/api/src/nexus/bsettop_os.c $ |
|---|
| 19 | * |
|---|
| 20 | * 8 8/18/09 6:38p katrep |
|---|
| 21 | * PR56109: Use calllbacks instead of using events for callbacks to the |
|---|
| 22 | * app. |
|---|
| 23 | * |
|---|
| 24 | * 7 7/6/09 1:21p jgarrett |
|---|
| 25 | * PR 55572: Switching thread termination mode |
|---|
| 26 | * |
|---|
| 27 | * 6 5/12/09 5:38p jrubio |
|---|
| 28 | * PR55063: Scheduler Destroy must be called before the Task Thread is |
|---|
| 29 | * Destroyed, Scheduler Destroy will wait for task to finish |
|---|
| 30 | * |
|---|
| 31 | * 5 4/14/09 3:23p jtna |
|---|
| 32 | * PR51960: fixing memleak due to scheduler not getting destroyed |
|---|
| 33 | * |
|---|
| 34 | * 4 6/9/08 5:18p rjlewis |
|---|
| 35 | * PR40352: warnings. |
|---|
| 36 | * |
|---|
| 37 | * 3 6/4/08 11:24a rjlewis |
|---|
| 38 | * PR40352: extra functions needed for VxWorks. |
|---|
| 39 | * |
|---|
| 40 | * 2 3/31/08 6:59p jgarrett |
|---|
| 41 | * PR 40606: Fixing thread pointer |
|---|
| 42 | * |
|---|
| 43 | * 1 3/31/08 6:04p jgarrett |
|---|
| 44 | * PR 40606: Adding oslib support |
|---|
| 45 | * |
|---|
| 46 | ***************************************************************************/ |
|---|
| 47 | #include "bsettop_impl.h" |
|---|
| 48 | |
|---|
| 49 | BDBG_MODULE(os); |
|---|
| 50 | |
|---|
| 51 | static B_ThreadHandle g_thread; |
|---|
| 52 | static B_MutexHandle g_lock, g_slow, g_idle; |
|---|
| 53 | static B_SchedulerHandle g_scheduler; |
|---|
| 54 | |
|---|
| 55 | static void b_os_p_thread_main(void *pParam) |
|---|
| 56 | { |
|---|
| 57 | B_SchedulerHandle scheduler=pParam; |
|---|
| 58 | |
|---|
| 59 | B_Scheduler_Run(scheduler); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | bresult b_os_init(void) |
|---|
| 63 | { |
|---|
| 64 | bresult rc; |
|---|
| 65 | B_Error errCode; |
|---|
| 66 | B_ThreadSettings settings; |
|---|
| 67 | |
|---|
| 68 | errCode = B_Os_Init(); |
|---|
| 69 | if ( errCode ) |
|---|
| 70 | { |
|---|
| 71 | return BSETTOP_ERROR(berr_external_error); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | g_lock = B_Mutex_Create(NULL); |
|---|
| 75 | if ( NULL == g_lock ) |
|---|
| 76 | { |
|---|
| 77 | rc = BSETTOP_ERROR(berr_external_error); |
|---|
| 78 | goto err_lock; |
|---|
| 79 | } |
|---|
| 80 | g_slow = B_Mutex_Create(NULL); |
|---|
| 81 | if ( NULL == g_slow ) |
|---|
| 82 | { |
|---|
| 83 | rc = BSETTOP_ERROR(berr_external_error); |
|---|
| 84 | goto err_slow; |
|---|
| 85 | } |
|---|
| 86 | g_idle = B_Mutex_Create(NULL); |
|---|
| 87 | if ( NULL == g_idle ) |
|---|
| 88 | { |
|---|
| 89 | rc = BSETTOP_ERROR(berr_external_error); |
|---|
| 90 | goto err_idle; |
|---|
| 91 | } |
|---|
| 92 | g_scheduler = B_Scheduler_Create(NULL); |
|---|
| 93 | if ( NULL == g_scheduler ) |
|---|
| 94 | { |
|---|
| 95 | rc = BSETTOP_ERROR(berr_external_error); |
|---|
| 96 | goto err_scheduler; |
|---|
| 97 | } |
|---|
| 98 | B_Thread_GetDefaultSettings(&settings); |
|---|
| 99 | settings.terminate = false; |
|---|
| 100 | g_thread = B_Thread_Create("b_event", b_os_p_thread_main, g_scheduler, &settings); |
|---|
| 101 | if ( NULL == g_thread ) |
|---|
| 102 | { |
|---|
| 103 | rc = BSETTOP_ERROR(berr_external_error); |
|---|
| 104 | goto err_thread; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /* Success */ |
|---|
| 108 | return b_ok; |
|---|
| 109 | |
|---|
| 110 | err_thread: |
|---|
| 111 | B_Scheduler_Destroy(g_scheduler); |
|---|
| 112 | err_scheduler: |
|---|
| 113 | B_Mutex_Destroy(g_idle); |
|---|
| 114 | err_idle: |
|---|
| 115 | B_Mutex_Destroy(g_slow); |
|---|
| 116 | err_slow: |
|---|
| 117 | B_Mutex_Destroy(g_lock); |
|---|
| 118 | err_lock: |
|---|
| 119 | B_Os_Uninit(); |
|---|
| 120 | return rc; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | bresult b_os_uninit(void) |
|---|
| 124 | { |
|---|
| 125 | |
|---|
| 126 | if ( g_scheduler ) |
|---|
| 127 | { |
|---|
| 128 | B_Scheduler_Stop(g_scheduler); |
|---|
| 129 | B_Scheduler_Destroy(g_scheduler); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if ( g_thread ) |
|---|
| 133 | { |
|---|
| 134 | B_Thread_Destroy(g_thread); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | B_Os_Uninit(); |
|---|
| 138 | |
|---|
| 139 | return b_ok; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /* this function is used to acquire global lock */ |
|---|
| 143 | void b_lock(void) |
|---|
| 144 | { |
|---|
| 145 | B_Mutex_Lock(g_lock); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /* this function is used to release global lock (lock must be held by the current task before function is called) */ |
|---|
| 149 | void b_unlock(void) |
|---|
| 150 | { |
|---|
| 151 | B_MUTEX_ASSERT_LOCKED(g_lock); |
|---|
| 152 | B_Mutex_Unlock(g_lock); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /* this function returns trus if current task is holds the lock, the sole purpose of this function is to be used with conjunction with BDBG_ASSERT, e.g. BDBG_ASSERT(b_lock_assert()); */ |
|---|
| 156 | bool b_lock_assert(void) |
|---|
| 157 | { |
|---|
| 158 | return B_Mutex_P_IsLocked(g_lock); |
|---|
| 159 | } |
|---|
| 160 | #define B_LOCK_ASSERT() BDBG_ASSERT(b_lock_assert()) |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | Request a timer callback to occur no sooner than the specified in milliseconds. |
|---|
| 164 | It may come any amount of time later. If this function called within SettopApi core |
|---|
| 165 | callback would be called with global mutex (b_lock) held. Otherwise no particular |
|---|
| 166 | mutex held and it's user responsibility to call necessary synchronization functions. |
|---|
| 167 | **/ |
|---|
| 168 | b_timer_t b_timer_schedule(unsigned delay /* ms */, b_timer_callback_t callback, void *cntx) |
|---|
| 169 | { |
|---|
| 170 | return B_Scheduler_StartTimer(g_scheduler, |
|---|
| 171 | g_lock, |
|---|
| 172 | delay, |
|---|
| 173 | callback, |
|---|
| 174 | cntx); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | Cancel a timer callback. |
|---|
| 179 | **/ |
|---|
| 180 | void b_timer_cancel(b_timer_t timer) |
|---|
| 181 | { |
|---|
| 182 | B_Scheduler_CancelTimer(g_scheduler, timer); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | Get the current time. |
|---|
| 188 | The value of b_time_t is platform dependent. However, you can obtain a platform-independent |
|---|
| 189 | time diff by using b_time_diff. |
|---|
| 190 | **/ |
|---|
| 191 | void b_time_get(b_time_t *time) |
|---|
| 192 | { |
|---|
| 193 | B_Time_Get(time); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /** |
|---|
| 197 | Returns difference in milliseconds between two timestamps obtained with b_time_get. |
|---|
| 198 | |
|---|
| 199 | It's recomended that code would use this function in a way that it could return |
|---|
| 200 | only positive values (where applicable, it helps in handling wraparound conditions) |
|---|
| 201 | |
|---|
| 202 | TODO: Regarding the above comment, we need to define specific behavior for wraparound. |
|---|
| 203 | This either handles wrap-around and always returns a positive value, or it does not. |
|---|
| 204 | **/ |
|---|
| 205 | long b_time_diff(const b_time_t *future, const b_time_t *past) |
|---|
| 206 | { |
|---|
| 207 | return B_Time_Diff(future, past); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | This function flushes and invalidates data cache in a specified memory region. |
|---|
| 212 | |
|---|
| 213 | This function is used before a DMA output (write) operation to be sure that the DMA uses the flushed data. |
|---|
| 214 | This function is also used before a DMA input (read) operation to be sure that when the data is read it |
|---|
| 215 | comes from the newly DMAed data (and not stale data found in the cache). |
|---|
| 216 | |
|---|
| 217 | Note: the implementation ensures that both a flush AND an invailidate operation is performed. |
|---|
| 218 | **/ |
|---|
| 219 | void b_cacheflush(const void *addr, size_t nbytes) |
|---|
| 220 | { |
|---|
| 221 | NEXUS_FlushCache(addr, nbytes); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /* |
|---|
| 225 | Summary: |
|---|
| 226 | The following two functions convert from b_lock state to idle state, and vice versa. |
|---|
| 227 | |
|---|
| 228 | Description: |
|---|
| 229 | Both function is place for a race conditione (they don't swap locks atomically) |
|---|
| 230 | so critical state might need to be reexamined after function call. |
|---|
| 231 | */ |
|---|
| 232 | void b_lock2idle(void) |
|---|
| 233 | { |
|---|
| 234 | b_unlock(); |
|---|
| 235 | B_Mutex_Lock(g_idle); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | void b_idle2lock(void) |
|---|
| 239 | { |
|---|
| 240 | B_MUTEX_ASSERT_LOCKED(g_idle); |
|---|
| 241 | B_Mutex_Unlock(g_idle); |
|---|
| 242 | b_lock(); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /* |
|---|
| 246 | Summary: |
|---|
| 247 | The following two functions convert from b_lock state to slow state, and vice versa. |
|---|
| 248 | |
|---|
| 249 | Description: |
|---|
| 250 | Both function is place for a race conditione (they don't swap locks atomically) |
|---|
| 251 | so critical state might need to be reexamined after function call. |
|---|
| 252 | */ |
|---|
| 253 | void b_lock2slow(void) |
|---|
| 254 | { |
|---|
| 255 | b_unlock(); |
|---|
| 256 | B_Mutex_Lock(g_slow); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void b_slow2lock(void) |
|---|
| 260 | { |
|---|
| 261 | B_MUTEX_ASSERT_LOCKED(g_slow); |
|---|
| 262 | B_Mutex_Unlock(g_slow); |
|---|
| 263 | b_lock(); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /* |
|---|
| 267 | The b_event interface provides for receiving a task-context callback function when a KNI event is fired. |
|---|
| 268 | |
|---|
| 269 | The callback function is called with b_lock already held. |
|---|
| 270 | |
|---|
| 271 | The caller should execute the callback with the absolute minimum amount of time. All events |
|---|
| 272 | are serialized. You should never convert from lock2idle or lock2slow in an event callback because it will |
|---|
| 273 | cause all other event processing to block. |
|---|
| 274 | |
|---|
| 275 | It is possible to call b_event_unregister from inside an event. |
|---|
| 276 | |
|---|
| 277 | It is required that all b_event functions are called with b_lock held. |
|---|
| 278 | */ |
|---|
| 279 | |
|---|
| 280 | b_event_id_t b_event_register(B_EventHandle event, b_event_callback_t callback, void *cntx) |
|---|
| 281 | { |
|---|
| 282 | return B_Scheduler_RegisterEvent(g_scheduler, |
|---|
| 283 | g_lock, |
|---|
| 284 | event, |
|---|
| 285 | callback, |
|---|
| 286 | cntx); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | void b_event_unregister(b_event_id_t id) |
|---|
| 290 | { |
|---|
| 291 | B_Scheduler_UnregisterEvent(g_scheduler, id); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | void b_task_params_init(b_task_params *params) |
|---|
| 296 | { |
|---|
| 297 | BKNI_Memset(params, 0, sizeof(*params)); |
|---|
| 298 | params->name = "b_task"; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | typedef struct b_task |
|---|
| 302 | { |
|---|
| 303 | B_ThreadHandle thread; |
|---|
| 304 | b_task_func func; |
|---|
| 305 | void *data; |
|---|
| 306 | } b_task; |
|---|
| 307 | |
|---|
| 308 | static void b_thread_launcher(void *param) |
|---|
| 309 | { |
|---|
| 310 | b_task *task = param; |
|---|
| 311 | |
|---|
| 312 | task->func(task->data); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | bresult b_start_task(b_task_t *handle, const b_task_params *params, b_task_func func, void *data) |
|---|
| 316 | { |
|---|
| 317 | B_ThreadSettings threadSettings; |
|---|
| 318 | |
|---|
| 319 | BDBG_ASSERT(NULL != handle); |
|---|
| 320 | BDBG_ASSERT(NULL != params); |
|---|
| 321 | BDBG_ASSERT(NULL != func); |
|---|
| 322 | |
|---|
| 323 | B_Thread_GetDefaultSettings(&threadSettings); |
|---|
| 324 | |
|---|
| 325 | threadSettings.priority = params->priority; |
|---|
| 326 | if ( params->stack_size ) |
|---|
| 327 | { |
|---|
| 328 | threadSettings.stackSize = params->stack_size; |
|---|
| 329 | } |
|---|
| 330 | else |
|---|
| 331 | { |
|---|
| 332 | threadSettings.stackSize = 16*1024; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | *handle = BKNI_Malloc(sizeof(b_task)); |
|---|
| 336 | if ( NULL == *handle ) |
|---|
| 337 | { |
|---|
| 338 | return BSETTOP_ERROR(berr_out_of_memory); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | (*handle)->func = func; |
|---|
| 342 | (*handle)->data = data; |
|---|
| 343 | threadSettings.terminate = false; |
|---|
| 344 | (*handle)->thread = B_Thread_Create(params->name, |
|---|
| 345 | b_thread_launcher, |
|---|
| 346 | *handle, |
|---|
| 347 | &threadSettings); |
|---|
| 348 | if ( (*handle)->thread ) |
|---|
| 349 | { |
|---|
| 350 | return b_ok; |
|---|
| 351 | } |
|---|
| 352 | else |
|---|
| 353 | { |
|---|
| 354 | BKNI_Free(handle); |
|---|
| 355 | return BSETTOP_ERROR(berr_external_error); |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | void b_stop_task(b_task_t handle) |
|---|
| 360 | { |
|---|
| 361 | B_Thread_Destroy(handle->thread); |
|---|
| 362 | BKNI_Free(handle); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | b_calllback_id_t b_callback_create(void *pInstanceHandle, b_callback_t function,void *cntx,int param) |
|---|
| 366 | { |
|---|
| 367 | B_SchedulerCallbackHandle hCallback = B_SchedulerCallback_Create(g_scheduler,pInstanceHandle,NULL); |
|---|
| 368 | if(!hCallback) |
|---|
| 369 | { |
|---|
| 370 | BDBG_ERR(("Failed to create callback")); |
|---|
| 371 | return NULL; |
|---|
| 372 | } |
|---|
| 373 | if(B_SchedulerCallback_SetFunction(hCallback,function,cntx,param)!=B_ERROR_SUCCESS) |
|---|
| 374 | { |
|---|
| 375 | BDBG_ERR(("Failed to set callback function")); |
|---|
| 376 | return NULL; |
|---|
| 377 | } |
|---|
| 378 | return (b_calllback_id_t)hCallback; |
|---|
| 379 | } |
|---|
| 380 | void b_callback_destroy(b_calllback_id_t callback) |
|---|
| 381 | { |
|---|
| 382 | B_SchedulerCallback_Destroy(callback); |
|---|
| 383 | } |
|---|
| 384 | void b_callback_fire(b_calllback_id_t callback) |
|---|
| 385 | { |
|---|
| 386 | B_SchedulerCallback_Fire(callback); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | void b_callback_stop(void *pInstanceHandle) |
|---|
| 390 | { |
|---|
| 391 | B_Scheduler_StopCallbacks(g_scheduler,pInstanceHandle); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | #ifdef __vxworks |
|---|
| 397 | |
|---|
| 398 | /*#include "nexus_file_posix.h" */ |
|---|
| 399 | /* We have to define these here because the file above is defined to be a private include. */ |
|---|
| 400 | off_t bfile_io_seek(int desc, off_t offset, int whence); |
|---|
| 401 | off_t bfile_io_size(int desc); |
|---|
| 402 | |
|---|
| 403 | off_t b_file_seek(int desc, off_t offset, int whence) |
|---|
| 404 | { |
|---|
| 405 | return bfile_io_seek(desc,offset,whence); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | off_t b_file_size(int desc) |
|---|
| 409 | { |
|---|
| 410 | return bfile_io_size(desc); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | #endif |
|---|
| 414 | |
|---|