Thread.cpp 792 B

12345678910111213141516171819202122232425262728293031
  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. id = INVALID_ID;
  13. return Error::THREAD_ERROR;
  14. }
  15. return Error::NONE;
  16. }
  17. Core::Error Core::Thread::join(Id id, int* returnValue) {
  18. thrd_t* t = threads.search(id);
  19. if(t == nullptr) {
  20. return Error::INVALID_ID;
  21. }
  22. if(thrd_join(*t, returnValue) != thrd_success) {
  23. return Error::THREAD_ERROR;
  24. }
  25. return threads.remove(id);
  26. }