@@ -99,7 +99,7 @@ static int esp_gcov_process_data(struct esp32_apptrace_cmd_ctx *ctx,
99
99
unsigned int core_id ,
100
100
uint8_t * data ,
101
101
uint32_t data_len );
102
- static void * esp32_apptrace_data_processor (void * arg );
102
+ static int esp32_apptrace_data_processor (void * priv );
103
103
static int esp32_apptrace_get_data_info (struct esp32_apptrace_cmd_ctx * ctx ,
104
104
struct esp32_apptrace_target_state * target_state ,
105
105
uint32_t * fired_target_num );
@@ -367,76 +367,50 @@ struct esp32_apptrace_block *esp32_apptrace_free_block_get(struct esp32_apptrace
367
367
{
368
368
struct esp32_apptrace_block * block = NULL ;
369
369
370
- int res = pthread_mutex_lock (& ctx -> trax_blocks_mux );
371
- if (res == 0 ) {
372
- if (!hlist_empty (& ctx -> free_trace_blocks )) {
373
- /*get first */
374
- block = hlist_entry (ctx -> free_trace_blocks .first , struct esp32_apptrace_block , node );
375
- hlist_del (& block -> node );
376
- }
377
- res = pthread_mutex_unlock (& ctx -> trax_blocks_mux );
378
- if (res )
379
- LOG_ERROR ("Failed to unlock blocks pool (%d)!" , res );
370
+ if (!hlist_empty (& ctx -> free_trace_blocks )) {
371
+ /*get first */
372
+ block = hlist_entry (ctx -> free_trace_blocks .first , struct esp32_apptrace_block , node );
373
+ hlist_del (& block -> node );
380
374
}
381
375
382
376
return block ;
383
377
}
384
378
385
379
static int esp32_apptrace_ready_block_put (struct esp32_apptrace_cmd_ctx * ctx , struct esp32_apptrace_block * block )
386
380
{
387
- int res = pthread_mutex_lock (& ctx -> trax_blocks_mux );
388
- if (res == 0 ) {
389
- LOG_DEBUG ("esp32_apptrace_ready_block_put" );
390
- /* add to ready blocks list */
391
- INIT_HLIST_NODE (& block -> node );
392
- hlist_add_head (& block -> node , & ctx -> ready_trace_blocks );
393
- res = pthread_mutex_unlock (& ctx -> trax_blocks_mux );
394
- if (res )
395
- LOG_ERROR ("Failed to unlock blocks pool (%d)!" , res );
396
- } else {
397
- LOG_ERROR ("Failed to lock blocks pool (%d)!" , res );
398
- }
381
+ LOG_DEBUG ("esp32_apptrace_ready_block_put" );
382
+ /* add to ready blocks list */
383
+ INIT_HLIST_NODE (& block -> node );
384
+ hlist_add_head (& block -> node , & ctx -> ready_trace_blocks );
399
385
400
- return res == 0 ? ERROR_OK : ERROR_FAIL ;
386
+ return ERROR_OK ;
401
387
}
402
388
403
389
static struct esp32_apptrace_block * esp32_apptrace_ready_block_get (struct esp32_apptrace_cmd_ctx * ctx )
404
390
{
405
391
struct esp32_apptrace_block * block = NULL ;
406
- if (pthread_mutex_trylock (& ctx -> trax_blocks_mux ) == 0 ) {
407
- if (!hlist_empty (& ctx -> ready_trace_blocks )) {
408
- struct hlist_head * head = & ctx -> ready_trace_blocks ;
409
- struct hlist_node * pos = head -> first ;
410
- while (pos ) {
411
- block = hlist_entry (pos , struct esp32_apptrace_block , node );
412
- pos = pos -> next ;
413
- }
414
- /* remove it from ready list */
415
- hlist_del (& block -> node );
392
+
393
+ if (!hlist_empty (& ctx -> ready_trace_blocks )) {
394
+ struct hlist_head * head = & ctx -> ready_trace_blocks ;
395
+ struct hlist_node * pos = head -> first ;
396
+ while (pos ) {
397
+ block = hlist_entry (pos , struct esp32_apptrace_block , node );
398
+ pos = pos -> next ;
416
399
}
417
- int res = pthread_mutex_unlock (& ctx -> trax_blocks_mux );
418
- if (res )
419
- LOG_ERROR ("Failed to unlock blocks pool (%d)!" , res );
420
- /* TODO; return NULL and free block?? */
400
+ /* remove it from ready list */
401
+ hlist_del (& block -> node );
421
402
}
403
+
422
404
return block ;
423
405
}
424
406
425
407
static int esp32_apptrace_block_free (struct esp32_apptrace_cmd_ctx * ctx , struct esp32_apptrace_block * block )
426
408
{
427
- int res = pthread_mutex_lock (& ctx -> trax_blocks_mux );
428
- if (res == 0 ) {
429
- /* add to free blocks list */
430
- INIT_HLIST_NODE (& block -> node );
431
- hlist_add_head (& block -> node , & ctx -> free_trace_blocks );
432
- res = pthread_mutex_unlock (& ctx -> trax_blocks_mux );
433
- if (res )
434
- LOG_ERROR ("Failed to unlock blocks pool (%d)!" , res );
435
- } else {
436
- LOG_ERROR ("Failed to lock blocks pool (%d)!" , res );
437
- }
409
+ /* add to free blocks list */
410
+ INIT_HLIST_NODE (& block -> node );
411
+ hlist_add_head (& block -> node , & ctx -> free_trace_blocks );
438
412
439
- return res == 0 ? ERROR_OK : ERROR_FAIL ;
413
+ return ERROR_OK ;
440
414
}
441
415
442
416
static int esp32_apptrace_wait_tracing_finished (struct esp32_apptrace_cmd_ctx * ctx )
@@ -449,19 +423,9 @@ static int esp32_apptrace_wait_tracing_finished(struct esp32_apptrace_cmd_ctx *c
449
423
return ERROR_FAIL ;
450
424
}
451
425
}
452
- /* signal thread to stop */
426
+ /* signal timer callback to stop */
453
427
ctx -> running = 0 ;
454
- /* wait for the processor thread to finish */
455
- if (ctx -> data_processor != (pthread_t )- 1 ) {
456
- int * thr_res = NULL ;
457
- int res = pthread_join (ctx -> data_processor , (void * * )& thr_res );
458
- if (res )
459
- LOG_ERROR ("Failed to join trace data processor thread (%d)!" , res );
460
- else
461
- LOG_INFO ("Trace data processor thread exited with %d" , * thr_res );
462
- free (thr_res );
463
- }
464
-
428
+ target_unregister_timer_callback (esp32_apptrace_data_processor , ctx );
465
429
return ERROR_OK ;
466
430
}
467
431
@@ -473,7 +437,6 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
473
437
{
474
438
memset (cmd_ctx , 0 , sizeof (struct esp32_apptrace_cmd_ctx ));
475
439
476
- cmd_ctx -> data_processor = (pthread_t )- 1 ;
477
440
cmd_ctx -> mode = mode ;
478
441
cmd_ctx -> target_state = target -> state ;
479
442
@@ -547,19 +510,11 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
547
510
}
548
511
549
512
cmd_ctx -> running = 1 ;
550
-
551
- int res = pthread_mutex_init (& cmd_ctx -> trax_blocks_mux , NULL );
552
- if (res ) {
553
- LOG_ERROR ("Failed to blocks pool mux (%d)!" , res );
554
- esp32_apptrace_blocks_pool_cleanup (cmd_ctx );
555
- return ERROR_FAIL ;
556
- }
557
513
if (cmd_ctx -> mode != ESP_APPTRACE_CMD_MODE_SYNC ) {
558
- res = pthread_create (& cmd_ctx -> data_processor , NULL , esp32_apptrace_data_processor , cmd_ctx );
559
- if (res ) {
560
- LOG_ERROR ("Failed to start trace data processor thread (%d)!" , res );
561
- cmd_ctx -> data_processor = (pthread_t )- 1 ;
562
- pthread_mutex_destroy (& cmd_ctx -> trax_blocks_mux );
514
+ int res = target_register_timer_callback (esp32_apptrace_data_processor , 0 , TARGET_TIMER_TYPE_PERIODIC ,
515
+ cmd_ctx );
516
+ if (res != ERROR_OK ) {
517
+ LOG_ERROR ("Failed to start trace data timer callback (%d)!" , res );
563
518
esp32_apptrace_blocks_pool_cleanup (cmd_ctx );
564
519
return ERROR_FAIL ;
565
520
}
@@ -580,7 +535,6 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
580
535
581
536
int esp32_apptrace_cmd_ctx_cleanup (struct esp32_apptrace_cmd_ctx * cmd_ctx )
582
537
{
583
- pthread_mutex_destroy (& cmd_ctx -> trax_blocks_mux );
584
538
esp32_apptrace_blocks_pool_cleanup (cmd_ctx );
585
539
return ERROR_OK ;
586
540
}
@@ -1036,34 +990,31 @@ static int esp32_apptrace_handle_trace_block(struct esp32_apptrace_cmd_ctx *ctx,
1036
990
return ERROR_OK ;
1037
991
}
1038
992
1039
- static void * esp32_apptrace_data_processor (void * arg )
993
+ static int esp32_apptrace_data_processor (void * priv )
1040
994
{
1041
- int * res = malloc (sizeof (int ));
1042
- assert (res );
995
+ struct esp32_apptrace_cmd_ctx * ctx = (struct esp32_apptrace_cmd_ctx * )priv ;
1043
996
1044
- struct esp32_apptrace_cmd_ctx * ctx = (struct esp32_apptrace_cmd_ctx * )arg ;
997
+ if (!ctx -> running )
998
+ return ERROR_OK ;
1045
999
1046
- * res = ERROR_OK ;
1000
+ struct esp32_apptrace_block * block = esp32_apptrace_ready_block_get (ctx );
1001
+ if (!block )
1002
+ return ERROR_OK ;
1047
1003
1048
- while (ctx -> running ) {
1049
- struct esp32_apptrace_block * block = esp32_apptrace_ready_block_get (ctx );
1050
- if (!block )
1051
- continue ;
1052
- * res = esp32_apptrace_handle_trace_block (ctx , block );
1053
- if (* res != ERROR_OK ) {
1054
- ctx -> running = 0 ;
1055
- LOG_ERROR ("Failed to process trace block %" PRId32 " bytes!" , block -> data_len );
1056
- break ;
1057
- }
1058
- * res = esp32_apptrace_block_free (ctx , block );
1059
- if (* res != ERROR_OK ) {
1060
- ctx -> running = 0 ;
1061
- LOG_ERROR ("Failed to free ready block!" );
1062
- break ;
1063
- }
1004
+ int res = esp32_apptrace_handle_trace_block (ctx , block );
1005
+ if (res != ERROR_OK ) {
1006
+ ctx -> running = 0 ;
1007
+ LOG_ERROR ("Failed to process trace block %" PRId32 " bytes!" , block -> data_len );
1008
+ return res ;
1009
+ }
1010
+ res = esp32_apptrace_block_free (ctx , block );
1011
+ if (res != ERROR_OK ) {
1012
+ ctx -> running = 0 ;
1013
+ LOG_ERROR ("Failed to free ready block!" );
1014
+ return res ;
1064
1015
}
1065
1016
1066
- return ( void * ) res ;
1017
+ return ERROR_OK ;
1067
1018
}
1068
1019
1069
1020
static int esp32_apptrace_check_connection (struct esp32_apptrace_cmd_ctx * ctx )
0 commit comments