From aa97f7250048077445de34f658feb22dbbb5d588 Mon Sep 17 00:00:00 2001 From: ypc <15051963820@163.com> Date: Tue, 7 Jul 2026 15:01:20 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=E6=97=A7=E7=9A=84?= =?UTF-8?q?=20myTask.cpp=EF=BC=88=E5=B7=B2=E6=9B=BF=E6=8D=A2=E4=B8=BA=20my?= =?UTF-8?q?Task.c=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/public/libtask/src/myTask.cpp | 302 ------------------------------ 1 file changed, 302 deletions(-) delete mode 100644 src/public/libtask/src/myTask.cpp diff --git a/src/public/libtask/src/myTask.cpp b/src/public/libtask/src/myTask.cpp deleted file mode 100644 index 81e0be9..0000000 --- a/src/public/libtask/src/myTask.cpp +++ /dev/null @@ -1,302 +0,0 @@ -/** - * @file myTask.cpp - * @brief Task framework implementation (C++ internal) - */ - -#include -#include "myTask.h" -#include "myBase.h" -#include -#include -#include -#include - -struct stru_task_timer -{ - timer_func_cb cb; - void *arg; - uint32_t ms; - int flags; - int active; - uint64_t next_us; - std::string name; -}; - -LOCAL std::map *g_timer_map = NULL; -LOCAL pthread_t g_timer_thread; -LOCAL int g_timer_running = 0; -LOCAL pthread_mutex_t g_timer_lock = PTHREAD_MUTEX_INITIALIZER; - -LOCAL uint64_t now_us(void) -{ - struct timeval tv; gettimeofday(&tv, NULL); - return (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec; -} - -LOCAL void *timer_worker(void *arg) -{ - (void)arg; - while (g_timer_running) - { - pthread_mutex_lock(&g_timer_lock); - uint64_t now = now_us(); - for (auto &kv : *g_timer_map) - { - stru_task_timer_t pt = kv.first; - if (!pt->active || now < pt->next_us) continue; - int ret = pt->cb(pt->arg); - if (ret != 0 || pt->flags == 0) pt->active = 0; - else pt->next_us = now + (uint64_t)pt->ms * 1000; - } - pthread_mutex_unlock(&g_timer_lock); - usleep(10000); - } - return NULL; -} - -LOCAL void timer_ensure_init(void) -{ - if (g_timer_map) return; - g_timer_map = new std::map(); - g_timer_running = 1; - pthread_create(&g_timer_thread, NULL, timer_worker, NULL); -} - -struct stru_task_event -{ - uint32_t bits; pthread_mutex_t lock; pthread_cond_t cond; std::string name; -}; - -struct stru_task_msg_queue -{ - uint8_t *buf; uint32_t msg_size, msg_num, head, tail, count; - pthread_mutex_t lock; pthread_cond_t cond_read, cond_write; std::string name; -}; - -/* ================================================================ - * Exported API (extern "C" — C linkage) - * ================================================================ */ - -extern "C" { - -stru_task_timer_t task_timer_create(const char *name, timer_func_cb cb, - void *arg, uint32_t ms, int flags) -{ - timer_ensure_init(); - stru_task_timer_t pt = new stru_task_timer(); - if (!pt) return NULL; - pt->cb = cb; pt->arg = arg; pt->ms = ms; pt->flags = flags; - pt->active = 0; pt->next_us = 0; pt->name = name ? name : "?"; - pthread_mutex_lock(&g_timer_lock); - (*g_timer_map)[pt] = 1; - pthread_mutex_unlock(&g_timer_lock); - return pt; -} - -int task_timer_start(stru_task_timer_t pt) -{ - if (!pt) return -1; - pthread_mutex_lock(&g_timer_lock); - pt->next_us = now_us() + (uint64_t)pt->ms * 1000; - pt->active = 1; - pthread_mutex_unlock(&g_timer_lock); - return 0; -} - -int task_timer_stop(stru_task_timer_t pt) -{ - if (!pt) return -1; - pthread_mutex_lock(&g_timer_lock); pt->active = 0; pthread_mutex_unlock(&g_timer_lock); - return 0; -} - -int task_timer_restart(stru_task_timer_t pt, uint32_t ms) -{ - if (!pt) return -1; - pthread_mutex_lock(&g_timer_lock); - pt->ms = ms; pt->next_us = now_us() + (uint64_t)ms * 1000; pt->active = 1; - pthread_mutex_unlock(&g_timer_lock); - return 0; -} - -int task_timer_destroy(stru_task_timer_t pt) -{ - if (!pt) return -1; - pthread_mutex_lock(&g_timer_lock); g_timer_map->erase(pt); pthread_mutex_unlock(&g_timer_lock); - delete pt; - return 0; -} - -int task_timer_is_active(stru_task_timer_t pt) -{ - if (!pt) return 0; - pthread_mutex_lock(&g_timer_lock); - int a = pt->active; pthread_mutex_unlock(&g_timer_lock); - return a; -} - -stru_task_event_t task_event_create(const char *name) -{ - stru_task_event_t pe = new stru_task_event(); - if (!pe) return NULL; - pe->bits = 0; - pe->lock = PTHREAD_MUTEX_INITIALIZER; - pe->cond = PTHREAD_COND_INITIALIZER; - pe->name = name ? name : "?"; - return pe; -} - -int task_event_destroy(stru_task_event_t pe) -{ - if (!pe) return -1; - pthread_mutex_destroy(&pe->lock); pthread_cond_destroy(&pe->cond); delete pe; - return 0; -} - -int task_event_send(stru_task_event_t pe, uint32_t bits) -{ - if (!pe) return -1; - pthread_mutex_lock(&pe->lock); pe->bits |= bits; pthread_cond_broadcast(&pe->cond); pthread_mutex_unlock(&pe->lock); - return 0; -} - -int task_event_recv(stru_task_event_t pe, uint32_t set, uint32_t opt, - uint32_t timeout_ms, uint32_t *recved) -{ - if (!pe || !recved) return -1; - pthread_mutex_lock(&pe->lock); - while (1) - { - uint32_t m = pe->bits & set; - if ((opt == 0 && m == set) || (opt == 1 && m != 0)) - { *recved = m; pe->bits &= ~set; pthread_mutex_unlock(&pe->lock); return 0; } - if (timeout_ms == 0) { pthread_cond_wait(&pe->cond, &pe->lock); } - else - { - struct timeval tv; gettimeofday(&tv, NULL); - struct timespec ts; - ts.tv_sec = tv.tv_sec + timeout_ms / 1000; - ts.tv_nsec = (tv.tv_usec + (timeout_ms % 1000) * 1000) * 1000; - if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } - if (pthread_cond_timedwait(&pe->cond, &pe->lock, &ts) == ETIMEDOUT) - { pthread_mutex_unlock(&pe->lock); return -1; } - } - } -} - -int task_event_clear(stru_task_event_t pe, uint32_t bits) -{ - if (!pe) return -1; - pthread_mutex_lock(&pe->lock); pe->bits &= ~bits; pthread_mutex_unlock(&pe->lock); - return 0; -} - -uint32_t task_event_query(stru_task_event_t pe) -{ - if (!pe) return 0; - pthread_mutex_lock(&pe->lock); - uint32_t b = pe->bits; pthread_mutex_unlock(&pe->lock); - return b; -} - -stru_task_msg_queue_t task_msg_queue_create(const char *name, - uint32_t msg_size, uint32_t msg_num) -{ - if (msg_size == 0 || msg_num == 0) return NULL; - stru_task_msg_queue_t pq = new stru_task_msg_queue(); - if (!pq) return NULL; - pq->buf = (uint8_t *)malloc(msg_size * msg_num); - pq->msg_size = msg_size; pq->msg_num = msg_num; - pq->head = 0; pq->tail = 0; pq->count = 0; - pq->lock = PTHREAD_MUTEX_INITIALIZER; - pq->cond_read = PTHREAD_COND_INITIALIZER; - pq->cond_write = PTHREAD_COND_INITIALIZER; - pq->name = name ? name : "?"; - return pq; -} - -int task_msg_queue_destroy(stru_task_msg_queue_t pq) -{ - if (!pq) return -1; - pthread_mutex_destroy(&pq->lock); pthread_cond_destroy(&pq->cond_read); pthread_cond_destroy(&pq->cond_write); - free(pq->buf); delete pq; - return 0; -} - -int task_msg_queue_send(stru_task_msg_queue_t pq, const void *msg, uint32_t size) -{ return task_msg_queue_send_timeout(pq, msg, size, 0); } - -int task_msg_queue_send_timeout(stru_task_msg_queue_t pq, const void *msg, - uint32_t size, uint32_t timeout_ms) -{ - if (!pq || !msg || size > pq->msg_size) return -1; - pthread_mutex_lock(&pq->lock); - while (pq->count >= pq->msg_num) - { - if (timeout_ms == 0) pthread_cond_wait(&pq->cond_write, &pq->lock); - else { - struct timeval tv; gettimeofday(&tv, NULL); - struct timespec ts; - ts.tv_sec = tv.tv_sec + timeout_ms / 1000; - ts.tv_nsec = (tv.tv_usec + (timeout_ms % 1000) * 1000) * 1000; - if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } - if (pthread_cond_timedwait(&pq->cond_write, &pq->lock, &ts) == ETIMEDOUT) - { pthread_mutex_unlock(&pq->lock); return -1; } - } - } - memcpy(pq->buf + pq->head * pq->msg_size, msg, size); - pq->head = (pq->head + 1) % pq->msg_num; pq->count++; - pthread_cond_signal(&pq->cond_read); pthread_mutex_unlock(&pq->lock); - return 0; -} - -int task_msg_queue_recv(stru_task_msg_queue_t pq, void *msg, uint32_t size, - uint32_t timeout_ms) -{ - if (!pq || !msg) return -1; - pthread_mutex_lock(&pq->lock); - while (pq->count == 0) - { - if (timeout_ms == 0) pthread_cond_wait(&pq->cond_read, &pq->lock); - else { - struct timeval tv; gettimeofday(&tv, NULL); - struct timespec ts; - ts.tv_sec = tv.tv_sec + timeout_ms / 1000; - ts.tv_nsec = (tv.tv_usec + (timeout_ms % 1000) * 1000) * 1000; - if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } - if (pthread_cond_timedwait(&pq->cond_read, &pq->lock, &ts) == ETIMEDOUT) - { pthread_mutex_unlock(&pq->lock); return -1; } - } - } - memcpy(msg, pq->buf + pq->tail * pq->msg_size, (size < pq->msg_size) ? size : pq->msg_size); - pq->tail = (pq->tail + 1) % pq->msg_num; pq->count--; - pthread_cond_signal(&pq->cond_write); pthread_mutex_unlock(&pq->lock); - return 0; -} - -int task_msg_queue_try_recv(stru_task_msg_queue_t pq, void *msg, uint32_t size) -{ - if (!pq || !msg) return -1; - pthread_mutex_lock(&pq->lock); - if (pq->count == 0) { pthread_mutex_unlock(&pq->lock); return -1; } - memcpy(msg, pq->buf + pq->tail * pq->msg_size, (size < pq->msg_size) ? size : pq->msg_size); - pq->tail = (pq->tail + 1) % pq->msg_num; pq->count--; - pthread_cond_signal(&pq->cond_write); pthread_mutex_unlock(&pq->lock); - return 0; -} - -uint32_t task_msg_queue_get_count(stru_task_msg_queue_t pq) -{ - if (!pq) return 0; - pthread_mutex_lock(&pq->lock); uint32_t c = pq->count; pthread_mutex_unlock(&pq->lock); - return c; -} - -uint32_t task_msg_queue_space(stru_task_msg_queue_t pq) -{ - if (!pq) return 0; - pthread_mutex_lock(&pq->lock); uint32_t s = pq->msg_num - pq->count; pthread_mutex_unlock(&pq->lock); - return s; -} - -} /* extern "C" */