OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/platform_thread.h" | 5 #include "base/platform_thread.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sched.h> | 8 #include <sched.h> |
9 | 9 |
10 #if defined(OS_MACOSX) | 10 #if defined(OS_MACOSX) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // static | 61 // static |
62 void PlatformThread::SetName(const char* name) { | 62 void PlatformThread::SetName(const char* name) { |
63 // The POSIX standard does not provide for naming threads, and neither Linux | 63 // The POSIX standard does not provide for naming threads, and neither Linux |
64 // nor Mac OS X (our two POSIX targets) provide any non-portable way of doing | 64 // nor Mac OS X (our two POSIX targets) provide any non-portable way of doing |
65 // it either. (Some BSDs provide pthread_set_name_np but that isn't much of a | 65 // it either. (Some BSDs provide pthread_set_name_np but that isn't much of a |
66 // consolation prize.) | 66 // consolation prize.) |
67 // TODO(darin): decide whether stuffing the name in TLS or other in-memory | 67 // TODO(darin): decide whether stuffing the name in TLS or other in-memory |
68 // structure would be useful for debugging or not. | 68 // structure would be useful for debugging or not. |
69 } | 69 } |
70 | 70 |
71 // static | 71 namespace { |
72 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 72 |
73 PlatformThreadHandle* thread_handle) { | 73 bool CreateThread(size_t stack_size, bool joinable, |
| 74 PlatformThread::Delegate* delegate, |
| 75 PlatformThreadHandle* thread_handle) { |
74 #if defined(OS_MACOSX) | 76 #if defined(OS_MACOSX) |
75 base::InitThreading(); | 77 base::InitThreading(); |
76 #endif // OS_MACOSX | 78 #endif // OS_MACOSX |
77 | 79 |
78 bool success = false; | 80 bool success = false; |
79 pthread_attr_t attributes; | 81 pthread_attr_t attributes; |
80 pthread_attr_init(&attributes); | 82 pthread_attr_init(&attributes); |
81 | 83 |
82 // Pthreads are joinable by default, so we don't need to specify any special | 84 // Pthreads are joinable by default, so only specify the detached attribute if |
83 // attributes to be able to call pthread_join later. | 85 // the thread should be non-joinable. |
| 86 if (!joinable) { |
| 87 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
| 88 } |
84 | 89 |
85 if (stack_size > 0) | 90 if (stack_size > 0) |
86 pthread_attr_setstacksize(&attributes, stack_size); | 91 pthread_attr_setstacksize(&attributes, stack_size); |
87 | 92 |
88 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); | 93 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); |
89 | 94 |
90 pthread_attr_destroy(&attributes); | 95 pthread_attr_destroy(&attributes); |
91 return success; | 96 return success; |
92 } | 97 } |
93 | 98 |
| 99 } // anonymous namespace |
| 100 |
| 101 // static |
| 102 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 103 PlatformThreadHandle* thread_handle) { |
| 104 return CreateThread(stack_size, true /* joinable thread */, |
| 105 delegate, thread_handle); |
| 106 } |
| 107 |
| 108 // static |
| 109 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 110 PlatformThreadHandle unused; |
| 111 |
| 112 bool result = CreateThread(stack_size, false /* non-joinable thread */, |
| 113 delegate, &unused); |
| 114 return result; |
| 115 } |
| 116 |
94 // static | 117 // static |
95 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 118 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
96 pthread_join(thread_handle, NULL); | 119 pthread_join(thread_handle, NULL); |
97 } | 120 } |
OLD | NEW |