hal_thread.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * thread_hal.h
  3. *
  4. * Multi-threading abstraction layer
  5. *
  6. * Copyright 2013-2021 Michael Zillgith
  7. *
  8. * This file is part of Platform Abstraction Layer (libpal)
  9. * for libiec61850, libmms, and lib60870.
  10. */
  11. #ifndef THREAD_HAL_H_
  12. #define THREAD_HAL_H_
  13. #include "hal_base.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * \file hal_thread.h
  19. * \brief Abstraction layer for threading and synchronization
  20. */
  21. /*! \addtogroup hal
  22. *
  23. * @{
  24. */
  25. /**
  26. * @defgroup HAL_THREAD Threading and synchronization API
  27. *
  28. * @{
  29. */
  30. /** Opaque reference of a Thread instance */
  31. typedef struct sThread* Thread;
  32. /** Qpaque reference of a Semaphore instance */
  33. typedef void* Semaphore;
  34. /** Reference to a function that is called when starting the thread */
  35. typedef void* (*ThreadExecutionFunction) (void*);
  36. /**
  37. * \brief Create a new Thread instance
  38. *
  39. * \param function the entry point of the thread
  40. * \param parameter a parameter that is passed to the threads start function
  41. * \param autodestroy the thread is automatically destroyed if the ThreadExecutionFunction has finished.
  42. *
  43. * \return the newly created Thread instance
  44. */
  45. PAL_API Thread
  46. Thread_create(ThreadExecutionFunction function, void* parameter, bool autodestroy);
  47. /**
  48. * \brief Start a Thread.
  49. *
  50. * This function invokes the start function of the thread. The thread terminates when
  51. * the start function returns.
  52. *
  53. * \param thread the Thread instance to start
  54. */
  55. PAL_API void
  56. Thread_start(Thread thread);
  57. /**
  58. * \brief Destroy a Thread and free all related resources.
  59. *
  60. * \param thread the Thread instance to destroy
  61. */
  62. PAL_API void
  63. Thread_destroy(Thread thread);
  64. /**
  65. * \brief Suspend execution of the Thread for the specified number of milliseconds
  66. */
  67. PAL_API void
  68. Thread_sleep(int millies);
  69. PAL_API Semaphore
  70. Semaphore_create(int initialValue);
  71. /* Wait until semaphore value is greater than zero. Then decrease the semaphore value. */
  72. PAL_API void
  73. Semaphore_wait(Semaphore self);
  74. PAL_API void
  75. Semaphore_post(Semaphore self);
  76. PAL_API void
  77. Semaphore_destroy(Semaphore self);
  78. /*! @} */
  79. /*! @} */
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif /* THREAD_HAL_H_ */