Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: runtime/vm/os_thread_linux.cc

Issue 1439483003: - Add an OSThread structure which is the generic TLS structure for all C++ (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: code-review Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/os_thread_android.cc ('k') | runtime/vm/os_thread_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" // NOLINT 5 #include "platform/globals.h" // NOLINT
6 #if defined(TARGET_OS_LINUX) 6 #if defined(TARGET_OS_LINUX)
7 7
8 #include "vm/os_thread.h" 8 #include "vm/os_thread.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 ts->tv_nsec += nanos; 71 ts->tv_nsec += nanos;
72 if (ts->tv_nsec >= kNanosecondsPerSecond) { 72 if (ts->tv_nsec >= kNanosecondsPerSecond) {
73 ts->tv_sec += 1; 73 ts->tv_sec += 1;
74 ts->tv_nsec -= kNanosecondsPerSecond; 74 ts->tv_nsec -= kNanosecondsPerSecond;
75 } 75 }
76 } 76 }
77 77
78 78
79 class ThreadStartData { 79 class ThreadStartData {
80 public: 80 public:
81 ThreadStartData(OSThread::ThreadStartFunction function, 81 ThreadStartData(const char* name,
82 OSThread::ThreadStartFunction function,
82 uword parameter) 83 uword parameter)
83 : function_(function), parameter_(parameter) {} 84 : name_(name), function_(function), parameter_(parameter) {}
84 85
86 const char* name() const { return name_; }
85 OSThread::ThreadStartFunction function() const { return function_; } 87 OSThread::ThreadStartFunction function() const { return function_; }
86 uword parameter() const { return parameter_; } 88 uword parameter() const { return parameter_; }
87 89
88 private: 90 private:
91 const char* name_;
89 OSThread::ThreadStartFunction function_; 92 OSThread::ThreadStartFunction function_;
90 uword parameter_; 93 uword parameter_;
91 94
92 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); 95 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
93 }; 96 };
94 97
95 98
96 // Dispatch to the thread start function provided by the caller. This trampoline 99 // Dispatch to the thread start function provided by the caller. This trampoline
97 // is used to ensure that the thread is properly destroyed if the thread just 100 // is used to ensure that the thread is properly destroyed if the thread just
98 // exits. 101 // exits.
99 static void* ThreadStart(void* data_ptr) { 102 static void* ThreadStart(void* data_ptr) {
100 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); 103 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
101 104
105 const char* name = data->name();
102 OSThread::ThreadStartFunction function = data->function(); 106 OSThread::ThreadStartFunction function = data->function();
103 uword parameter = data->parameter(); 107 uword parameter = data->parameter();
104 delete data; 108 delete data;
105 109
110 // Create new OSThread object and set as TLS for new thread.
111 OSThread* thread = new OSThread();
112 OSThread::SetCurrent(thread);
113 thread->set_name(name);
114
106 // Call the supplied thread start function handing it its parameters. 115 // Call the supplied thread start function handing it its parameters.
107 function(parameter); 116 function(parameter);
108 117
109 return NULL; 118 return NULL;
110 } 119 }
111 120
112 121
113 int OSThread::Start(ThreadStartFunction function, uword parameter) { 122 int OSThread::Start(const char* name,
123 ThreadStartFunction function,
124 uword parameter) {
114 pthread_attr_t attr; 125 pthread_attr_t attr;
115 int result = pthread_attr_init(&attr); 126 int result = pthread_attr_init(&attr);
116 RETURN_ON_PTHREAD_FAILURE(result); 127 RETURN_ON_PTHREAD_FAILURE(result);
117 128
118 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); 129 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize());
119 RETURN_ON_PTHREAD_FAILURE(result); 130 RETURN_ON_PTHREAD_FAILURE(result);
120 131
121 ThreadStartData* data = new ThreadStartData(function, parameter); 132 ThreadStartData* data = new ThreadStartData(name, function, parameter);
122 133
123 pthread_t tid; 134 pthread_t tid;
124 result = pthread_create(&tid, &attr, ThreadStart, data); 135 result = pthread_create(&tid, &attr, ThreadStart, data);
125 RETURN_ON_PTHREAD_FAILURE(result); 136 RETURN_ON_PTHREAD_FAILURE(result);
126 137
127 result = pthread_attr_destroy(&attr); 138 result = pthread_attr_destroy(&attr);
128 RETURN_ON_PTHREAD_FAILURE(result); 139 RETURN_ON_PTHREAD_FAILURE(result);
129 140
130 return 0; 141 return 0;
131 } 142 }
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 void Monitor::NotifyAll() { 424 void Monitor::NotifyAll() {
414 // When running with assertions enabled we track the owner. 425 // When running with assertions enabled we track the owner.
415 ASSERT(IsOwnedByCurrentThread()); 426 ASSERT(IsOwnedByCurrentThread());
416 int result = pthread_cond_broadcast(data_.cond()); 427 int result = pthread_cond_broadcast(data_.cond());
417 VALIDATE_PTHREAD_RESULT(result); 428 VALIDATE_PTHREAD_RESULT(result);
418 } 429 }
419 430
420 } // namespace dart 431 } // namespace dart
421 432
422 #endif // defined(TARGET_OS_LINUX) 433 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « runtime/vm/os_thread_android.cc ('k') | runtime/vm/os_thread_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698