thread.c++ 3.53 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Kenton Varda's avatar
Kenton Varda committed
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Kenton Varda's avatar
Kenton Varda committed
21 22 23

#include "thread.h"
#include "debug.h"
24 25 26 27

#if _WIN32
#include <windows.h>
#else
Kenton Varda's avatar
Kenton Varda committed
28
#include <pthread.h>
29
#include <signal.h>
30
#endif
Kenton Varda's avatar
Kenton Varda committed
31 32 33

namespace kj {

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#if _WIN32

Thread::Thread(Function<void()> func): func(kj::mv(func)) {
  threadHandle = CreateThread(nullptr, 0, &runThread, this, 0, nullptr);
  KJ_ASSERT(threadHandle != nullptr, "CreateThread failed.");
}

Thread::~Thread() noexcept(false) {
  if (!detached) {
    KJ_ASSERT(WaitForSingleObject(threadHandle, INFINITE) != WAIT_FAILED);

    KJ_IF_MAYBE(e, exception) {
      kj::throwRecoverableException(kj::mv(*e));
    }
  }
}

void Thread::detach() {
  KJ_ASSERT(CloseHandle(threadHandle));
  detached = true;
}

DWORD Thread::runThread(void* ptr) {
  Thread* thread = reinterpret_cast<Thread*>(ptr);
  KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() {
    thread->func();
  })) {
    thread->exception = kj::mv(*exception);
  }
  return 0;
}

#else  // _WIN32

68
Thread::Thread(Function<void()> func): func(kj::mv(func)) {
Kenton Varda's avatar
Kenton Varda committed
69 70 71
  static_assert(sizeof(threadId) >= sizeof(pthread_t),
                "pthread_t is larger than a long long on your platform.  Please port.");

72 73
  int pthreadResult = pthread_create(reinterpret_cast<pthread_t*>(&threadId),
                                     nullptr, &runThread, this);
Kenton Varda's avatar
Kenton Varda committed
74 75 76 77 78
  if (pthreadResult != 0) {
    KJ_FAIL_SYSCALL("pthread_create", pthreadResult);
  }
}

79
Thread::~Thread() noexcept(false) {
Kenton Varda's avatar
Kenton Varda committed
80 81 82 83 84
  if (!detached) {
    int pthreadResult = pthread_join(*reinterpret_cast<pthread_t*>(&threadId), nullptr);
    if (pthreadResult != 0) {
      KJ_FAIL_SYSCALL("pthread_join", pthreadResult) { break; }
    }
85

Kenton Varda's avatar
Kenton Varda committed
86 87 88
    KJ_IF_MAYBE(e, exception) {
      kj::throwRecoverableException(kj::mv(*e));
    }
89
  }
90 91 92 93 94 95 96
}

void Thread::sendSignal(int signo) {
  int pthreadResult = pthread_kill(*reinterpret_cast<pthread_t*>(&threadId), signo);
  if (pthreadResult != 0) {
    KJ_FAIL_SYSCALL("pthread_kill", pthreadResult) { break; }
  }
Kenton Varda's avatar
Kenton Varda committed
97 98
}

Kenton Varda's avatar
Kenton Varda committed
99 100 101 102 103 104 105 106
void Thread::detach() {
  int pthreadResult = pthread_detach(*reinterpret_cast<pthread_t*>(&threadId));
  if (pthreadResult != 0) {
    KJ_FAIL_SYSCALL("pthread_detach", pthreadResult) { break; }
  }
  detached = true;
}

107 108 109 110 111 112 113 114 115 116
void* Thread::runThread(void* ptr) {
  Thread* thread = reinterpret_cast<Thread*>(ptr);
  KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() {
    thread->func();
  })) {
    thread->exception = kj::mv(*exception);
  }
  return nullptr;
}

117 118
#endif  // _WIN32, else

Kenton Varda's avatar
Kenton Varda committed
119
}  // namespace kj