|
33 | 33 |
|
34 | 34 | #include <stddef.h> |
35 | 35 |
|
36 | | -// Debug counter for tracking execution (avoids printf blocking issues) |
37 | | -volatile int ble_work_debug_step = 0; |
38 | 36 |
|
39 | 37 | // Flag to indicate we're in the bt_enable() init phase |
40 | 38 | // During this phase, mp_bluetooth_zephyr_init_work_get() will get work from k_sys_work_q |
@@ -222,11 +220,6 @@ int k_work_cancel(struct k_work *work) { |
222 | 220 | return 1; |
223 | 221 | } |
224 | 222 |
|
225 | | -int k_work_cancel_sync(struct k_work *work, void *sync) { |
226 | | - // sync parameter is for synchronization with other threads, not needed in MicroPython |
227 | | - (void)sync; |
228 | | - return k_work_cancel(work); |
229 | | -} |
230 | 223 |
|
231 | 224 | bool k_work_is_pending(const struct k_work *work) { |
232 | 225 | return work->pending; |
@@ -286,19 +279,13 @@ int k_work_schedule(struct k_work_delayable *dwork, k_timeout_t delay) { |
286 | 279 | return k_work_schedule_for_queue(&k_sys_work_q, dwork, delay); |
287 | 280 | } |
288 | 281 |
|
289 | | -int k_work_reschedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, k_timeout_t delay) { |
290 | | - DEBUG_WORK_printf("k_work_reschedule_for_queue(%p, %p, %u)\n", queue, dwork, delay.ticks); |
291 | | - |
292 | | - // Reschedule is the same as schedule in our implementation |
293 | | - return k_work_schedule_for_queue(queue, dwork, delay); |
294 | | -} |
295 | 282 |
|
296 | 283 | int k_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay) { |
297 | 284 | if (!k_sys_work_q.head && !k_sys_work_q.nextq) { |
298 | 285 | k_work_queue_init(&k_sys_work_q); |
299 | 286 | k_sys_work_q.name = "SYS WQ"; |
300 | 287 | } |
301 | | - return k_work_reschedule_for_queue(&k_sys_work_q, dwork, delay); |
| 288 | + return k_work_schedule_for_queue(&k_sys_work_q, dwork, delay); |
302 | 289 | } |
303 | 290 |
|
304 | 291 | int k_work_cancel_delayable(struct k_work_delayable *dwork) { |
@@ -441,48 +428,6 @@ void mp_bluetooth_zephyr_work_process(void) { |
441 | 428 | work_process_depth--; |
442 | 429 | } |
443 | 430 |
|
444 | | -// Called by mp_bluetooth_init() wait loop to process initialization work synchronously |
445 | | -void mp_bluetooth_zephyr_work_process_init(void) { |
446 | | - // Prevent recursion (though unlikely since init work should be synchronous) |
447 | | - if (init_work_processor_running) { |
448 | | - DEBUG_WORK_printf("init_work_process: already running, skipping\n"); |
449 | | - return; |
450 | | - } |
451 | | - |
452 | | - init_work_processor_running = true; |
453 | | - |
454 | | - // Process ONLY the initialization work queue |
455 | | - struct k_work_q *q = &k_init_work_q; |
456 | | - |
457 | | - while (q->head != NULL) { |
458 | | - // Dequeue work item |
459 | | - struct k_work *work = q->head; |
460 | | - q->head = work->next; |
461 | | - |
462 | | - if (q->head) { |
463 | | - q->head->prev = NULL; |
464 | | - } |
465 | | - |
466 | | - work->next = NULL; |
467 | | - work->prev = NULL; |
468 | | - work->pending = false; |
469 | | - |
470 | | - // Execute work handler |
471 | | - DEBUG_WORK_printf("init_work_execute(%p, handler=%p)\n", work, work->handler); |
472 | | - if (work->handler) { |
473 | | - work->handler(work); |
474 | | - } |
475 | | - DEBUG_WORK_printf("init_work_execute(%p) done\n", work); |
476 | | - |
477 | | - // Check if work was re-enqueued during execution |
478 | | - if (work->pending) { |
479 | | - DEBUG_WORK_printf(" --> init work re-enqueued, stopping queue processing\n"); |
480 | | - break; |
481 | | - } |
482 | | - } |
483 | | - |
484 | | - init_work_processor_running = false; |
485 | | -} |
486 | 431 |
|
487 | 432 | // --- Init Work Helper Functions --- |
488 | 433 |
|
@@ -543,74 +488,7 @@ struct k_work *mp_bluetooth_zephyr_init_work_get(void) { |
543 | 488 | return work; |
544 | 489 | } |
545 | 490 |
|
546 | | -// Debug function to report work processing statistics |
547 | | -void mp_bluetooth_zephyr_work_debug_stats(void) { |
548 | | - mp_printf(&mp_plat_print, "WORK STATS: process() called %d times, %d items processed\n", |
549 | | - work_process_call_count, work_items_processed); |
550 | | -} |
551 | | - |
552 | | -// Process all pending work in all queues (except during init phase). |
553 | | -// Used by the FreeRTOS work queue thread (added in zephyr_ble_freertos branch). |
554 | | -#if MICROPY_BLUETOOTH_ZEPHYR_USE_FREERTOS |
555 | | -static void ble_work_process_all(void) { |
556 | | - ble_work_debug_step = 10; // Entered ble_work_process_all |
557 | 491 |
|
558 | | - // During init phase, main loop handles all work processing |
559 | | - // to ensure proper sequencing of bt_enable() initialization |
560 | | - if (mp_bluetooth_zephyr_in_init_phase()) { |
561 | | - ble_work_debug_step = 11; // Skip due to init phase |
562 | | - return; |
563 | | - } |
564 | | - ble_work_debug_step = 12; // Processing work |
565 | | - |
566 | | - for (struct k_work_q *q = global_work_q; q != NULL; q = q->nextq) { |
567 | | - // Skip initialization work queue (processed separately) |
568 | | - if (q == &k_init_work_q) { |
569 | | - continue; |
570 | | - } |
571 | | - |
572 | | - // Process all work items in this queue |
573 | | - // Note: Must check q->head inside critical section to avoid race |
574 | | - for (;;) { |
575 | | - // Dequeue work item atomically |
576 | | - MICROPY_PY_BLUETOOTH_ENTER |
577 | | - struct k_work *work = q->head; |
578 | | - if (work == NULL) { |
579 | | - MICROPY_PY_BLUETOOTH_EXIT |
580 | | - break; // Queue empty |
581 | | - } |
582 | | - q->head = work->next; |
583 | | - if (q->head) { |
584 | | - q->head->prev = NULL; |
585 | | - } |
586 | | - work->next = NULL; |
587 | | - work->prev = NULL; |
588 | | - work->pending = false; |
589 | | - MICROPY_PY_BLUETOOTH_EXIT |
590 | | - |
591 | | - // Execute work handler outside critical section |
592 | | - work_items_processed++; |
593 | | - ble_work_debug_step = 20; // About to execute work handler |
594 | | - |
595 | | - if (work->handler) { |
596 | | - // Set context flag if processing system work queue |
597 | | - bool is_sys_wq = (q == &k_sys_work_q); |
598 | | - if (is_sys_wq) { |
599 | | - in_sys_work_q_context = true; |
600 | | - } |
601 | | - ble_work_debug_step = 21; // Calling handler |
602 | | - work->handler(work); |
603 | | - ble_work_debug_step = 22; // Handler returned |
604 | | - if (is_sys_wq) { |
605 | | - in_sys_work_q_context = false; |
606 | | - } |
607 | | - } |
608 | | - |
609 | | - ble_work_debug_step = 23; // Work done |
610 | | - } |
611 | | - } |
612 | | -} |
613 | | -#endif // MICROPY_BLUETOOTH_ZEPHYR_USE_FREERTOS |
614 | 492 |
|
615 | 493 | // Stub implementations — work processed via cooperative polling, no dedicated work thread. |
616 | 494 | void mp_bluetooth_zephyr_work_thread_start(void) { |
|
0 commit comments