Skip to content

Commit cc534a6

Browse files
committed
esp/apptrace: remove pthread dependency
1 parent 7b08625 commit cc534a6

File tree

5 files changed

+49
-104
lines changed

5 files changed

+49
-104
lines changed

.gitlab/ci/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ build_windows:
237237
- make
238238
- MAKEFLAGS= make install-strip
239239
- popd
240-
- cp /usr/${CONF_HOST}/lib/libwinpthread-1.dll $DIST_INSTALLED_DIR/bin/
241240
- cp ${ZLIB_DIR}/zlib1.dll $DIST_INSTALLED_DIR/bin/
242241
- *dist_archive
243242

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ AC_CHECK_HEADERS([fcntl.h])
6060
AC_CHECK_HEADERS([malloc.h])
6161
AC_CHECK_HEADERS([netdb.h])
6262
AC_CHECK_HEADERS([poll.h])
63-
AC_CHECK_HEADERS([pthread.h])
6463
AC_CHECK_HEADERS([strings.h])
6564
AC_CHECK_HEADERS([sys/ioctl.h])
6665
AC_CHECK_HEADERS([sys/param.h])

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ else
2020
%C%_openocd_LDADD += -ljim
2121
endif
2222

23-
%C%_openocd_LDADD += -lpthread
2423
if BUILD_ESP_COMPRESSION
2524
%C%_openocd_LDADD += -lz
2625
endif

src/target/espressif/esp32_apptrace.c

Lines changed: 49 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int esp_gcov_process_data(struct esp32_apptrace_cmd_ctx *ctx,
9999
unsigned int core_id,
100100
uint8_t *data,
101101
uint32_t data_len);
102-
static void *esp32_apptrace_data_processor(void *arg);
102+
static int esp32_apptrace_data_processor(void *priv);
103103
static int esp32_apptrace_get_data_info(struct esp32_apptrace_cmd_ctx *ctx,
104104
struct esp32_apptrace_target_state *target_state,
105105
uint32_t *fired_target_num);
@@ -367,76 +367,50 @@ struct esp32_apptrace_block *esp32_apptrace_free_block_get(struct esp32_apptrace
367367
{
368368
struct esp32_apptrace_block *block = NULL;
369369

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);
380374
}
381375

382376
return block;
383377
}
384378

385379
static int esp32_apptrace_ready_block_put(struct esp32_apptrace_cmd_ctx *ctx, struct esp32_apptrace_block *block)
386380
{
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);
399385

400-
return res == 0 ? ERROR_OK : ERROR_FAIL;
386+
return ERROR_OK;
401387
}
402388

403389
static struct esp32_apptrace_block *esp32_apptrace_ready_block_get(struct esp32_apptrace_cmd_ctx *ctx)
404390
{
405391
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;
416399
}
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);
421402
}
403+
422404
return block;
423405
}
424406

425407
static int esp32_apptrace_block_free(struct esp32_apptrace_cmd_ctx *ctx, struct esp32_apptrace_block *block)
426408
{
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);
438412

439-
return res == 0 ? ERROR_OK : ERROR_FAIL;
413+
return ERROR_OK;
440414
}
441415

442416
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
449423
return ERROR_FAIL;
450424
}
451425
}
452-
/* signal thread to stop */
426+
/* signal timer callback to stop */
453427
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);
465429
return ERROR_OK;
466430
}
467431

@@ -473,7 +437,6 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
473437
{
474438
memset(cmd_ctx, 0, sizeof(struct esp32_apptrace_cmd_ctx));
475439

476-
cmd_ctx->data_processor = (pthread_t)-1;
477440
cmd_ctx->mode = mode;
478441
cmd_ctx->target_state = target->state;
479442

@@ -547,19 +510,11 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
547510
}
548511

549512
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-
}
557513
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);
563518
esp32_apptrace_blocks_pool_cleanup(cmd_ctx);
564519
return ERROR_FAIL;
565520
}
@@ -580,7 +535,6 @@ int esp32_apptrace_cmd_ctx_init(struct target *target, struct esp32_apptrace_cmd
580535

581536
int esp32_apptrace_cmd_ctx_cleanup(struct esp32_apptrace_cmd_ctx *cmd_ctx)
582537
{
583-
pthread_mutex_destroy(&cmd_ctx->trax_blocks_mux);
584538
esp32_apptrace_blocks_pool_cleanup(cmd_ctx);
585539
return ERROR_OK;
586540
}
@@ -1036,34 +990,31 @@ static int esp32_apptrace_handle_trace_block(struct esp32_apptrace_cmd_ctx *ctx,
1036990
return ERROR_OK;
1037991
}
1038992

1039-
static void *esp32_apptrace_data_processor(void *arg)
993+
static int esp32_apptrace_data_processor(void *priv)
1040994
{
1041-
int *res = malloc(sizeof(int));
1042-
assert(res);
995+
struct esp32_apptrace_cmd_ctx *ctx = (struct esp32_apptrace_cmd_ctx *)priv;
1043996

1044-
struct esp32_apptrace_cmd_ctx *ctx = (struct esp32_apptrace_cmd_ctx *)arg;
997+
if (!ctx->running)
998+
return ERROR_OK;
1045999

1046-
*res = ERROR_OK;
1000+
struct esp32_apptrace_block *block = esp32_apptrace_ready_block_get(ctx);
1001+
if (!block)
1002+
return ERROR_OK;
10471003

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;
10641015
}
10651016

1066-
return (void *)res;
1017+
return ERROR_OK;
10671018
}
10681019

10691020
static int esp32_apptrace_check_connection(struct esp32_apptrace_cmd_ctx *ctx)

src/target/espressif/esp32_apptrace.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#ifndef OPENOCD_TARGET_ESP32_APPTRACE_H
99
#define OPENOCD_TARGET_ESP32_APPTRACE_H
1010

11-
#include <pthread.h>
1211
#include <helper/command.h>
1312
#include <helper/time_support.h>
1413
#include <target/target.h>
@@ -85,11 +84,9 @@ struct esp32_apptrace_cmd_ctx {
8584
const struct algorithm_hw *algo_hw;
8685
enum target_state target_state;
8786
uint32_t last_blk_id;
88-
pthread_mutex_t trax_blocks_mux;
8987
struct hlist_head free_trace_blocks;
9088
struct hlist_head ready_trace_blocks;
9189
uint32_t max_trace_block_sz;
92-
pthread_t data_processor;
9390
struct esp32_apptrace_format trace_format;
9491
int (*process_data)(struct esp32_apptrace_cmd_ctx *ctx, unsigned int core_id, uint8_t *data, uint32_t data_len);
9592
void (*auto_clean)(struct esp32_apptrace_cmd_ctx *ctx);

0 commit comments

Comments
 (0)