<commit_before>// Copyright (c) 2011, 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 <errno.h>
#include <sys/time.h>

#include "vm/thread.h"

#include "vm/assert.h"

namespace dart {

#define VALIDATE_PTHREAD_RESULT(result) \
  if (result != 0) { \
    FATAL2("pthread error: %d (%s)", result, strerror(result)); \
  }


static void ComputeTimeSpec(struct timespec* ts, int64_t millis) {
  struct timeval time;
  struct timeval delta;
  // Convert the millis to a timeval delta.
  int64_t secs = millis / 1000;
  int64_t micros = (millis - (secs * 1000)) * 1000;
  delta.tv_sec = secs;
  delta.tv_usec = micros;
  // Get the current time and add the delta. Convert the result to a timespec.
  int result = gettimeofday(&time, NULL);
  ASSERT(result == 0);
  timeradd(&time, &delta, &time);
  TIMEVAL_TO_TIMESPEC(&time, ts);
}


class ThreadStartData {
 public:
  ThreadStartData(Thread::ThreadStartFunction function,
                  uword parameter,
                  Thread* thread)
      : function_(function), parameter_(parameter), thread_(thread) {}

  Thread::ThreadStartFunction function() const { return function_; }
  uword parameter() const { return parameter_; }
  Thread* thread() const { return thread_; }

 private:
  Thread::ThreadStartFunction function_;
  uword parameter_;
  Thread* thread_;

  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);

  Thread::ThreadStartFunction function = data->function();
  uword parameter = data->parameter();
  Thread* thread = data->thread();
  delete data;

  // Call the supplied thread start function handing it its parameters.
  function(parameter);

  // When the function returns here, make sure that the thread is deleted.
  delete thread;

  return NULL;
}


Thread::Thread(ThreadStartFunction function, uword parameter) {
  pthread_attr_t attr;
  int result = pthread_attr_init(&attr);
  VALIDATE_PTHREAD_RESULT(result);

  result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  VALIDATE_PTHREAD_RESULT(result);

  result = pthread_attr_setstacksize(&attr, 32 * KB);
  VALIDATE_PTHREAD_RESULT(result);

  ThreadStartData* data = new ThreadStartData(function, parameter, this);

  pthread_t tid;
  result = pthread_create(&tid,
                          &attr,
                          ThreadStart,
                          data);
  VALIDATE_PTHREAD_RESULT(result);

  data_.tid_ = tid;

  result = pthread_attr_destroy(&attr);
  VALIDATE_PTHREAD_RESULT(result);
}


Thread::~Thread() {
}


Mutex::Mutex() {
  pthread_mutexattr_t attr;
  int result = pthread_mutexattr_init(&attr);
  VALIDATE_PTHREAD_RESULT(result);
<commit_msg>Bump stack size for pure Dart isolates. Review URL: http://codereview.chromium.org//8495007<commit_after>// Copyright (c) 2011, 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 <errno.h>
#include <sys/time.h>

#include "vm/thread.h"

#include "vm/assert.h"

namespace dart {

#define VALIDATE_PTHREAD_RESULT(result) \
  if (result != 0) { \
    FATAL2("pthread error: %d (%s)", result, strerror(result)); \
  }


static void ComputeTimeSpec(struct timespec* ts, int64_t millis) {
  struct timeval time;
  struct timeval delta;
  // Convert the millis to a timeval delta.
  int64_t secs = millis / 1000;
  int64_t micros = (millis - (secs * 1000)) * 1000;
  delta.tv_sec = secs;
  delta.tv_usec = micros;
  // Get the current time and add the delta. Convert the result to a timespec.
  int result = gettimeofday(&time, NULL);
  ASSERT(result == 0);
  timeradd(&time, &delta, &time);
  TIMEVAL_TO_TIMESPEC(&time, ts);
}


class ThreadStartData {
 public:
  ThreadStartData(Thread::ThreadStartFunction function,
                  uword parameter,
                  Thread* thread)
      : function_(function), parameter_(parameter), thread_(thread) {}

  Thread::ThreadStartFunction function() const { return function_; }
  uword parameter() const { return parameter_; }
  Thread* thread() const { return thread_; }

 private:
  Thread::ThreadStartFunction function_;
  uword parameter_;
  Thread* thread_;

  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);

  Thread::ThreadStartFunction function = data->function();
  uword parameter = data->parameter();
  Thread* thread = data->thread();
  delete data;

  // Call the supplied thread start function handing it its parameters.
  function(parameter);

  // When the function returns here, make sure that the thread is deleted.
  delete thread;

  return NULL;
}


Thread::Thread(ThreadStartFunction function, uword parameter) {
  pthread_attr_t attr;
  int result = pthread_attr_init(&attr);
  VALIDATE_PTHREAD_RESULT(result);

  result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  VALIDATE_PTHREAD_RESULT(result);

  result = pthread_attr_setstacksize(&attr, 1024 * KB);
  VALIDATE_PTHREAD_RESULT(result);

  ThreadStartData* data = new ThreadStartData(function, parameter, this);

  pthread_t tid;
  result = pthread_create(&tid,
                          &attr,
                          ThreadStart,
                          data);
  VALIDATE_PTHREAD_RESULT(result);

  data_.tid_ = tid;

  result = pthread_attr_destroy(&attr);
  VALIDATE_PTHREAD_RESULT(result);
}


Thread::~Thread() {
}


Mutex::Mutex() {
  pthread_mutexattr_t attr;
  int result = pthread_mutexattr_init(&attr);
  VALIDATE_PTHREAD_RESULT(result);

{'fraction_non_alphanumeric': 0.10275080906148867, 'fraction_numerical': 0.010194174757281554, 'mean_word_length': 3.817614964925955, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7628997', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1852, 'n_words': 549}