Thread.cpp 767 B

123456789101112131415161718192021222324252627282930
  1. #include "thread/Thread.h"
  2. #include <threads.h>
  3. #include "data/HashMap.h"
  4. static Core::HashMap<Core::Thread::Id, thrd_t> threads;
  5. static Core::Thread::Id idCounter = 0;
  6. Core::Error Core::Thread::start(Id& id, Function f, void* p) {
  7. id = idCounter++;
  8. thrd_t* t = nullptr;
  9. CORE_RETURN_ERROR(threads.tryEmplace(t, id));
  10. if(thrd_create(t, f, p) != thrd_success) {
  11. (void)threads.remove(id);
  12. return Error::THREAD_ERROR;
  13. }
  14. return Error::NONE;
  15. }
  16. Core::Error Core::Thread::join(Id id, int* returnValue) {
  17. thrd_t* t = threads.search(id);
  18. if(t == nullptr) {
  19. return Error::INVALID_ID;
  20. }
  21. if(thrd_join(*t, returnValue) != thrd_success) {
  22. return Error::THREAD_ERROR;
  23. }
  24. return threads.remove(id);
  25. }