platform_thread.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-23 19:26:27
  5. * @LastEditTime: 2020-02-23 16:19:07
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_thread.h"
  9. #include "platform_memory.h"
  10. platform_thread_t *platform_thread_init( const char *name,
  11. void (*entry)(void *),
  12. void * const param,
  13. unsigned int stack_size,
  14. unsigned int priority,
  15. unsigned int tick)
  16. {
  17. int res;
  18. platform_thread_t *thread;
  19. void *(*thread_entry) (void *);
  20. thread_entry = (void *(*)(void*))entry;
  21. thread = platform_memory_alloc(sizeof(platform_thread_t));
  22. res = pthread_create(&thread->thread, NULL, thread_entry, param);
  23. if(res != 0) {
  24. platform_memory_free(thread);
  25. }
  26. thread->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
  27. thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
  28. return thread;
  29. }
  30. void platform_thread_startup(platform_thread_t* thread)
  31. {
  32. (void) thread;
  33. }
  34. void platform_thread_stop(platform_thread_t* thread)
  35. {
  36. pthread_mutex_lock(&(thread->mutex));
  37. pthread_cond_wait(&(thread->cond), &(thread->mutex));
  38. pthread_mutex_unlock(&(thread->mutex));
  39. }
  40. void platform_thread_start(platform_thread_t* thread)
  41. {
  42. pthread_mutex_lock(&(thread->mutex));
  43. pthread_cond_signal(&(thread->cond));
  44. pthread_mutex_unlock(&(thread->mutex));
  45. }
  46. void platform_thread_destroy(platform_thread_t* thread)
  47. {
  48. if (NULL != thread)
  49. pthread_detach(thread->thread);
  50. }