blob: 738a96a9f502e568101be95d3a62f0e067416051 [file] [log] [blame] [edit]
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#if defined(DART_HOST_OS_FUCHSIA) && !defined(DART_USE_ABSL)
#include "bin/thread.h"
#include <errno.h> // NOLINT
#include <sys/time.h> // NOLINT
#include <zircon/status.h>
#include <zircon/syscalls.h>
#include <zircon/threads.h>
#include <zircon/types.h>
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
class ThreadStartData {
public:
ThreadStartData(const char* name,
Thread::ThreadStartFunction function,
uword parameter)
: name_(name), function_(function), parameter_(parameter) {}
const char* name() const { return name_; }
Thread::ThreadStartFunction function() const { return function_; }
uword parameter() const { return parameter_; }
private:
const char* name_;
Thread::ThreadStartFunction function_;
uword parameter_;
DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
};
// Dispatch to the thread start function provided by the caller. This trampoline
// is used to ensure that the thread is properly destroyed if the thread just
// exits.
static void* ThreadStart(void* data_ptr) {
ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
const char* name = data->name();
Thread::ThreadStartFunction function = data->function();
uword parameter = data->parameter();
delete data;
// Set the thread name.
char truncated_name[ZX_MAX_NAME_LEN];
snprintf(truncated_name, ZX_MAX_NAME_LEN, "%s", name);
zx_handle_t thread_handle = thrd_get_zx_handle(thrd_current());
zx_object_set_property(thread_handle, ZX_PROP_NAME, truncated_name,
ZX_MAX_NAME_LEN);
// Call the supplied thread start function handing it its parameters.
function(parameter);
return nullptr;
}
int Thread::TryStart(const char* name,
ThreadStartFunction function,
uword parameter) {
pthread_attr_t attr;
int result = pthread_attr_init(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize());
RETURN_ON_PTHREAD_FAILURE(result);
ThreadStartData* data = new ThreadStartData(name, function, parameter);
pthread_t tid;
result = pthread_create(&tid, &attr, ThreadStart, data);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_destroy(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
return 0;
}
intptr_t Thread::GetMaxStackSize() {
const int kStackSize = (128 * kWordSize * KB);
return kStackSize;
}
} // namespace bin
} // namespace dart
#endif // defined(DART_HOST_OS_FUCHSIA) && !defined(DART_USE_ABSL)