luat_rtos_task_posix.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "luat_base.h"
  2. #include "luat_rtos.h"
  3. #include "pthread.h"
  4. #include "string.h"
  5. #include "luat_mem.h"
  6. #include "limits.h"
  7. #define LUAT_LOG_TAG "posix"
  8. #include "luat_log.h"
  9. #define PTHREAD_COUNT 16
  10. static pthread_t threads[PTHREAD_COUNT] = {0};
  11. static int next_thread_id(void) {
  12. for (size_t i = 0; i < PTHREAD_COUNT; i++)
  13. {
  14. if (threads[i] == NULL)
  15. return i;
  16. }
  17. return -1;
  18. }
  19. static void* pthread_proxy(void* params) {
  20. luat_thread_t* t = (luat_thread_t*) params;
  21. t->entry(t->userdata);
  22. threads[t->id] = NULL;
  23. return NULL;
  24. }
  25. LUAT_RET luat_thread_start(luat_thread_t* thread) {
  26. pthread_attr_t tattr;
  27. size_t size;
  28. int ret;
  29. size = (PTHREAD_STACK_MIN + 0x1000);
  30. /* setting a new size */
  31. ret = pthread_attr_setstacksize(&tattr, size);
  32. int thread_id = next_thread_id();
  33. if (thread_id < 0) {
  34. LLOGW("too many thread, can't create new thread");
  35. return LUAT_ERR_FAIL;
  36. }
  37. thread->id = thread_id;
  38. LLOGD("thread id %d", thread_id);
  39. ret = pthread_create(&threads[thread_id], NULL, pthread_proxy, thread);
  40. if (ret != 0) {
  41. LLOGW("pthread_create fail %d", ret);
  42. return LUAT_ERR_FAIL;
  43. }
  44. return LUAT_ERR_OK;
  45. }
  46. LUAT_RET luat_thread_stop(luat_thread_t* thread) {
  47. LLOGE("thread stop isn't supported");
  48. return LUAT_ERR_FAIL;
  49. }
  50. LUAT_RET luat_thread_delete(luat_thread_t* thread) {
  51. LLOGE("thread stop isn't supported");
  52. return LUAT_ERR_FAIL;
  53. }