Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9KB

  1. #ifndef STK_THREAD_H
  2. #define STK_THREAD_H
  3. #include "Stk.h"
  4. #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  5. #include <pthread.h>
  6. #define THREAD_TYPE
  7. typedef pthread_t THREAD_HANDLE;
  8. typedef void * THREAD_RETURN;
  9. typedef void * (*THREAD_FUNCTION)(void *);
  10. #elif defined(__OS_WINDOWS__)
  11. #include <windows.h>
  12. #include <process.h>
  13. #define THREAD_TYPE __stdcall
  14. typedef unsigned long THREAD_HANDLE;
  15. typedef unsigned THREAD_RETURN;
  16. typedef unsigned (__stdcall *THREAD_FUNCTION)(void *);
  17. #endif
  18. namespace stk {
  19. /***************************************************/
  20. /*! \class Thread
  21. \brief STK thread class.
  22. This class provides a uniform interface for cross-platform
  23. threads. On unix systems, the pthread library is used. Under
  24. Windows, the C runtime threadex functions are used.
  25. Each instance of the Thread class can be used to control a single
  26. thread process. Routines are provided to signal cancelation
  27. and/or joining with a thread, though it is not possible for this
  28. class to know the running status of a thread once it is started.
  29. For cross-platform compatability, thread functions should be
  30. declared as follows:
  31. THREAD_RETURN THREAD_TYPE thread_function(void *ptr)
  32. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  33. */
  34. /***************************************************/
  35. class Thread : public Stk
  36. {
  37. public:
  38. //! Default constructor.
  39. Thread();
  40. //! The class destructor does not attempt to cancel or join a thread.
  41. ~Thread();
  42. //! Begin execution of the thread \e routine. Upon success, true is returned.
  43. /*!
  44. A data pointer can be supplied to the thread routine via the
  45. optional \e ptr argument. If the thread cannot be created, the
  46. return value is false.
  47. */
  48. bool start( THREAD_FUNCTION routine, void * ptr = NULL );
  49. //! Signal cancellation of a thread routine, returning \e true on success.
  50. /*!
  51. This function only signals thread cancellation. It does not
  52. wait to verify actual routine termination. A \e true return value
  53. only signifies that the cancellation signal was properly executed,
  54. not thread cancellation. A thread routine may need to make use of
  55. the testCancel() function to specify a cancellation point.
  56. */
  57. bool cancel(void);
  58. //! Block the calling routine indefinitely until the thread terminates.
  59. /*!
  60. This function suspends execution of the calling routine until the thread has terminated. It will return immediately if the thread was already terminated. A \e true return value signifies successful termination. A \e false return value indicates a problem with the wait call.
  61. */
  62. bool wait(void);
  63. //! Create a cancellation point within a thread routine.
  64. /*!
  65. This function call checks for thread cancellation, allowing the
  66. thread to be terminated if a cancellation request was previously
  67. signaled.
  68. */
  69. void testCancel(void);
  70. protected:
  71. THREAD_HANDLE thread_;
  72. };
  73. } // stk namespace
  74. #endif