Browse Source

fix(osal): fix threadx delete

Signed-off-by: sakumisu <1203593632@qq.com>
sakumisu 2 months ago
parent
commit
54083feaba
1 changed files with 56 additions and 43 deletions
  1. 56 43
      osal/ec_osal_threadx.c

+ 56 - 43
osal/ec_osal_threadx.c

@@ -6,35 +6,23 @@
 #include "ec_master.h"
 #include "tx_api.h"
 
-/* create bytepool in tx_application_define
- *
- * tx_byte_pool_create(&ec_byte_pool, "ec byte pool", memory_area, 65536);
- */
+extern TX_BYTE_POOL ec_byte_pool; // define ec_byte_pool and call ec_osal_init first
 
-extern TX_BYTE_POOL ec_byte_pool;
+TX_QUEUE *ec_osal_mq = TX_NULL;
 
 ec_osal_thread_t ec_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, ec_thread_entry_t entry, void *args)
 {
-    CHAR *pointer = TX_NULL;
     TX_THREAD *thread_ptr = TX_NULL;
 
-    tx_byte_allocate(&ec_byte_pool, (VOID **)&thread_ptr, sizeof(TX_THREAD), TX_NO_WAIT);
-
+    tx_byte_allocate(&ec_byte_pool, (VOID **)&thread_ptr, EC_ALIGN_UP(sizeof(TX_THREAD), 4) + stack_size, TX_NO_WAIT);
     if (thread_ptr == TX_NULL) {
         EC_LOG_ERR("Create thread %s failed\r\n", name);
         while (1) {
         }
     }
 
-    tx_byte_allocate(&ec_byte_pool, (VOID **)&pointer, stack_size, TX_NO_WAIT);
-    if (pointer == TX_NULL) {
-        EC_LOG_ERR("Create thread %s failed\r\n", name);
-        while (1) {
-        }
-    }
-
     tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args,
-                     pointer, stack_size,
+                     ((CHAR *)thread_ptr + EC_ALIGN_UP(sizeof(TX_THREAD), 4)), stack_size,
                      prio, prio, TX_NO_TIME_SLICE, TX_AUTO_START);
 
     return (ec_osal_thread_t)thread_ptr;
@@ -42,27 +30,18 @@ ec_osal_thread_t ec_osal_thread_create(const char *name, uint32_t stack_size, ui
 
 void ec_osal_thread_delete(ec_osal_thread_t thread)
 {
-    TX_THREAD *thread_ptr = NULL;
-
     if (thread == NULL) {
-        /* Call the tx_thread_identify to get the control block pointer of the
-        currently executing thread. */
-        thread_ptr = tx_thread_identify();
-
-        /* Check if the current running thread pointer is not NULL */
-        if (thread_ptr != NULL) {
-            /* Call the tx_thread_terminate to terminates the specified application
-            thread regardless of whether the thread is suspended or not. A thread
-            may call this service to terminate itself. */
-            tx_thread_terminate(thread_ptr);
-            tx_byte_release(thread_ptr->tx_thread_stack_start);
-            tx_byte_release(thread_ptr);
-        }
+        thread = tx_thread_identify();
+
+        uintptr_t addr = (uintptr_t)thread;
+        tx_queue_send((TX_QUEUE *)ec_osal_mq, &addr, TX_NO_WAIT);
+        tx_thread_terminate(thread);
+
         return;
     }
 
     tx_thread_terminate(thread);
-    tx_byte_release(thread_ptr->tx_thread_stack_start);
+    tx_thread_delete(thread);
     tx_byte_release(thread);
 }
 
@@ -81,7 +60,6 @@ ec_osal_sem_t ec_osal_sem_create(uint32_t max_count, uint32_t initial_count)
     TX_SEMAPHORE *sem_ptr = TX_NULL;
 
     tx_byte_allocate(&ec_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
-
     if (sem_ptr == TX_NULL) {
         EC_LOG_ERR("Create semaphore failed\r\n");
         while (1) {
@@ -116,7 +94,7 @@ int ec_osal_sem_take(ec_osal_sem_t sem, uint32_t timeout)
 
 int ec_osal_sem_give(ec_osal_sem_t sem)
 {
-    return (int)tx_semaphore_put((TX_SEMAPHORE *)sem);
+    return (tx_semaphore_put((TX_SEMAPHORE *)sem) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
 }
 
 void ec_osal_sem_reset(ec_osal_sem_t sem)
@@ -129,14 +107,13 @@ ec_osal_mutex_t ec_osal_mutex_create(void)
     TX_MUTEX *mutex_ptr = TX_NULL;
 
     tx_byte_allocate(&ec_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
-
     if (mutex_ptr == TX_NULL) {
         EC_LOG_ERR("Create mutex failed\r\n");
         while (1) {
         }
     }
 
-    tx_mutex_create(mutex_ptr, "ec_mutx", TX_INHERIT);
+    tx_mutex_create(mutex_ptr, "ec_mutex", TX_INHERIT);
     return (ec_osal_mutex_t)mutex_ptr;
 }
 
@@ -164,7 +141,7 @@ int ec_osal_mutex_take(ec_osal_mutex_t mutex)
 
 int ec_osal_mutex_give(ec_osal_mutex_t mutex)
 {
-    return (int)(tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
+    return (tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -EC_ERR_INVAL;
 }
 
 struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms, ec_timer_handler_t handler, void *argument, bool is_period)
@@ -173,7 +150,6 @@ struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms
     struct ec_osal_timer *timer;
 
     tx_byte_allocate(&ec_byte_pool, (VOID **)&timer, sizeof(struct ec_osal_timer), TX_NO_WAIT);
-
     if (timer == TX_NULL) {
         EC_LOG_ERR("Create ec_osal_timer failed\r\n");
         while (1) {
@@ -182,7 +158,6 @@ struct ec_osal_timer *ec_osal_timer_create(const char *name, uint32_t timeout_ms
     memset(timer, 0, sizeof(struct ec_osal_timer));
 
     tx_byte_allocate(&ec_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
-
     if (timer_ptr == TX_NULL) {
         EC_LOG_ERR("Create TX_TIMER failed\r\n");
         while (1) {
@@ -211,8 +186,8 @@ void ec_osal_timer_start(struct ec_osal_timer *timer)
 {
     if (tx_timer_change((TX_TIMER *)timer->timer, timer->timeout_ms, timer->is_period ? timer->timeout_ms : 0) == TX_SUCCESS) {
         /* Call the tx_timer_activate to activates the specified application
-             timer. The expiration routines of timers that expire at the same
-             time are executed in the order they were activated. */
+            timer. The expiration routines of timers that expire at the same
+            time are executed in the order they were activated. */
         if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
             /* Return osOK for success */
         } else {
@@ -238,7 +213,7 @@ size_t ec_osal_enter_critical_section(void)
 
 void ec_osal_leave_critical_section(size_t flag)
 {
-    int interrupt_save;
+    TX_INTERRUPT_SAVE_AREA
 
     interrupt_save = flag;
     TX_RESTORE
@@ -264,4 +239,42 @@ void *ec_osal_malloc(size_t size)
 void ec_osal_free(void *ptr)
 {
     tx_byte_release(ptr);
-}
+}
+
+static void ec_osal_thread(CONFIG_EC_OSAL_THREAD_SET_ARGV)
+{
+    int ret;
+    ec_osal_thread_t thread;
+
+    while (1) {
+        ret = tx_queue_receive(ec_osal_mq, (uintptr_t *)&thread, TX_WAIT_FOREVER);
+        if (ret != TX_SUCCESS) {
+            continue;
+        }
+        tx_thread_delete(thread);
+        tx_byte_release(thread);
+    }
+}
+
+void ec_osal_init(uint8_t *mem, uint32_t mem_size)
+{
+    ec_osal_thread_t thread;
+
+    tx_byte_pool_create(&ec_byte_pool, "ec byte pool", mem, mem_size);
+
+    thread = ec_osal_thread_create("ec_osal", 2048, 10, ec_osal_thread, NULL);
+    if (thread == NULL) {
+        EC_LOG_ERR("Create ec_osal_thread failed\r\n");
+        while (1) {
+        }
+    }
+
+    tx_byte_allocate(&ec_byte_pool, (VOID **)&ec_osal_mq, EC_ALIGN_UP(sizeof(TX_QUEUE), 4) + sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
+    if (ec_osal_mq == TX_NULL) {
+        EC_LOG_ERR("Create ec_osal_mq failed\r\n");
+        while (1) {
+        }
+    }
+
+    tx_queue_create(ec_osal_mq, "ec_mq", sizeof(uintptr_t) / 4, (CHAR *)ec_osal_mq + EC_ALIGN_UP(sizeof(TX_QUEUE), 4), sizeof(uintptr_t) * 32);
+}