Baselib_Thread.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "Baselib_Timer.h"
  3. #include "Baselib_ErrorState.h"
  4. #ifdef __cplusplus
  5. BASELIB_C_INTERFACE
  6. {
  7. #endif
  8. // Unique thread id that can be used to compare different threads or stored for bookkeeping etc..
  9. typedef intptr_t Baselib_Thread_Id;
  10. // Baselib_Thread_Id that is guaranteed not to represent a thread
  11. static const Baselib_Thread_Id Baselib_Thread_InvalidId = 0;
  12. // Yields the execution context of the current thread to other threads, potentially causing a context switch.
  13. //
  14. // The operating system may decide to not switch to any other thread.
  15. BASELIB_API void Baselib_Thread_YieldExecution(void);
  16. // Return the thread id of the current thread, i.e. the thread that is calling this function
  17. BASELIB_API Baselib_Thread_Id Baselib_Thread_GetCurrentThreadId(void);
  18. // We currently do not allow creating threads from C# bindings,
  19. // since there is right now no way accessible way to inform the garbage collector about new baselib threads.
  20. // I.e. any managed allocation on a baselib thread created from C# would never be garbage collected!
  21. #ifndef BASELIB_BINDING_GENERATION
  22. // The minimum guaranteed number of max concurrent threads that works on all platforms.
  23. //
  24. // This only applies if all the threads are created with Baselib.
  25. // In practice, it might not be possible to create this many threads either. If memory is exhausted,
  26. // by for example creating threads with very large stacks, that might translate to a lower limit in practice.
  27. // Note that on many platforms the actual limit is way higher.
  28. static const int Baselib_Thread_MinGuaranteedMaxConcurrentThreads = 64;
  29. typedef struct Baselib_Thread Baselib_Thread;
  30. typedef void (*Baselib_Thread_EntryPointFunction)(void* arg);
  31. typedef struct Baselib_Thread_Config
  32. {
  33. // Nullterminated name of the created thread (optional)
  34. // Useful exclusively for debugging - which tooling it is shown by and how it can be queried is platform dependent.
  35. const char* name;
  36. // The minimum size in bytes to allocate for the thread stack. (optional)
  37. // If not set, a platform/system specific default stack size will be used.
  38. // If the value set does not conform to platform specific minimum values or alignment requirements,
  39. // the actual stack size used will be bigger than what was requested.
  40. uint64_t stackSize;
  41. // Required, this is set by calling Baselib_Thread_ConfigCreate with a valid entry point function.
  42. Baselib_Thread_EntryPointFunction entryPoint;
  43. // Argument to the entry point function, does only need to be set if entryPoint takes an argument.
  44. void* entryPointArgument;
  45. } Baselib_Thread_Config;
  46. // Creates and starts a new thread.
  47. //
  48. // On some platforms the thread name is not set until the thread has begun executing, which is not guaranteed
  49. // to have happened when the creation function returns. There is typically a platform specific limit on the length of
  50. // the thread name. If config.name is longer than this limit, the name will be automatically truncated.
  51. //
  52. // \param config A pointer to a config object. entryPoint needs to be a valid function pointer, all other properties can be zero/null.
  53. //
  54. // Possible error codes:
  55. // - Baselib_ErrorCode_InvalidArgument: config.entryPoint is null
  56. // - Baselib_ErrorCode_OutOfSystemResources: there is not enough memory to create a thread with that stack size or the system limit of number of concurrent threads has been reached
  57. BASELIB_API Baselib_Thread* Baselib_Thread_Create(Baselib_Thread_Config config, Baselib_ErrorState* errorState);
  58. // Waits until a thread has finished its execution.
  59. //
  60. // Also frees its resources.
  61. // If called and completed successfully, no Baselib_Thread function can be called again on the same Baselib_Thread.
  62. //
  63. // \param thread A pointer to a thread object.
  64. // \param timeoutInMilliseconds Time to wait for the thread to finish
  65. //
  66. // Possible error codes:
  67. // - Baselib_ErrorCode_InvalidArgument: thread is null
  68. // - Baselib_ErrorCode_ThreadCannotJoinSelf: the thread parameter points to the current thread, i.e. the thread that is calling this function
  69. // - Baselib_ErrorCode_Timeout: timeout is reached before the thread has finished
  70. BASELIB_API void Baselib_Thread_Join(Baselib_Thread* thread, uint32_t timeoutInMilliseconds, Baselib_ErrorState* errorState);
  71. // Return the thread id of the thread given as argument
  72. //
  73. // \param thread A pointer to a thread object.
  74. BASELIB_API Baselib_Thread_Id Baselib_Thread_GetId(Baselib_Thread* thread);
  75. // Returns true if there is support in baselib for threads on this platform, otherwise false.
  76. BASELIB_API bool Baselib_Thread_SupportsThreads(void);
  77. #endif // !BASELIB_BINDING_GENERATION
  78. #ifdef __cplusplus
  79. } // BASELIB_C_INTERFACE
  80. #endif