awtk/tests/waitable_action_queue_test.cpp

86 lines
1.8 KiB
C++
Raw Normal View History

2020-02-07 11:19:46 +08:00
#include "tkc/utils.h"
#include "tkc/thread.h"
#include "tkc/waitable_action_queue.h"
#define NR 100000
static uint32_t exec_times = 0;
static waitable_action_queue_t* q;
static ret_t qaction_exec_dummy(qaction_t* req) {
exec_times++;
return RET_OK;
}
static void* consumer(void* args) {
uint32_t n = 0;
2020-02-13 10:19:58 +08:00
qaction_t* action = NULL;
2020-02-07 11:19:46 +08:00
log_debug("consumer start\n");
while (waitable_action_queue_recv(q, &action, 3000) == RET_OK) {
n++;
2020-02-13 10:19:58 +08:00
qaction_exec(action);
qaction_destroy(action);
2020-02-07 11:19:46 +08:00
}
log_debug("consumer done\n");
return NULL;
}
static void* producer(void* args) {
uint32_t i = 0;
uint32_t id = tk_pointer_to_int(args);
log_debug("p=%u start\n", id);
for (i = 0; i < NR; i++) {
2020-02-13 10:19:58 +08:00
qaction_t* a = qaction_create(qaction_exec_dummy, NULL, 0);
2020-02-07 11:19:46 +08:00
if (waitable_action_queue_send(q, a, 3000) != RET_OK) {
log_debug("send timeout\n");
break;
}
}
log_debug("p=%u done\n", id);
return NULL;
}
void test() {
tk_thread_t* c = tk_thread_create(consumer, NULL);
tk_thread_t* p1 = tk_thread_create(producer, tk_pointer_from_int(1));
tk_thread_t* p2 = tk_thread_create(producer, tk_pointer_from_int(2));
tk_thread_t* p3 = tk_thread_create(producer, tk_pointer_from_int(3));
tk_thread_t* p4 = tk_thread_create(producer, tk_pointer_from_int(4));
q = waitable_action_queue_create(9);
tk_thread_start(c);
tk_thread_start(p1);
tk_thread_start(p2);
tk_thread_start(p3);
tk_thread_start(p4);
tk_thread_join(c);
tk_thread_join(p1);
tk_thread_join(p2);
tk_thread_join(p3);
tk_thread_join(p4);
tk_thread_destroy(c);
tk_thread_destroy(p1);
tk_thread_destroy(p2);
tk_thread_destroy(p3);
tk_thread_destroy(p4);
waitable_action_queue_destroy(q);
2020-02-13 10:19:58 +08:00
log_debug("exec_times=%u \n", exec_times);
2020-02-07 11:19:46 +08:00
}
#include "tkc/platform.h"
int main(int argc, char* argv[]) {
platform_prepare();
test();
return 0;
}