blob: 125405625bb39c041ff4d5a21cc1d8edeb088761 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread.h"
18
19namespace art {
20
Elliott Hughes8daa0922011-09-11 13:46:25 -070021void Thread::SetNativePriority(int) {
22 // Do nothing.
23}
24
25int Thread::GetNativePriority() {
Elliott Hughes34e06962012-04-09 13:55:55 -070026 return kNormThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070027}
28
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070029static void SigAltStack(stack_t* new_stack, stack_t* old_stack) {
30 if (sigaltstack(new_stack, old_stack) == -1) {
31 PLOG(FATAL) << "sigaltstack failed";
32 }
33}
34
Dave Allisondfd3b472014-07-16 16:04:32 -070035// The default SIGSTKSZ on linux is 8K. If we do any logging in a signal
Ian Rogersc24a1e02014-08-13 14:37:26 -070036// handler this is too small. We allocate 16K instead or the minimum signal
37// stack size.
38// TODO: We shouldn't do logging (with locks) in signal handlers.
39static constexpr int kHostAltSigStackSize =
40 16 * KB < MINSIGSTKSZ ? MINSIGSTKSZ : 16 * KB;
Dave Allisondfd3b472014-07-16 16:04:32 -070041
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070042void Thread::SetUpAlternateSignalStack() {
43 // Create and set an alternate signal stack.
Dave Allisondfd3b472014-07-16 16:04:32 -070044#ifdef HAVE_ANDROID_OS
45 LOG(FATAL) << "Invalid use of alternate signal stack on Android";
46#endif
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070047 stack_t ss;
Dave Allisondfd3b472014-07-16 16:04:32 -070048 ss.ss_sp = new uint8_t[kHostAltSigStackSize];
49 ss.ss_size = kHostAltSigStackSize;
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070050 ss.ss_flags = 0;
51 CHECK(ss.ss_sp != NULL);
52 SigAltStack(&ss, NULL);
53
54 // Double-check that it worked.
55 ss.ss_sp = NULL;
56 SigAltStack(NULL, &ss);
57 VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp;
58}
59
60void Thread::TearDownAlternateSignalStack() {
61 // Get the pointer so we can free the memory.
62 stack_t ss;
63 SigAltStack(NULL, &ss);
64 uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp);
65
66 // Tell the kernel to stop using it.
67 ss.ss_sp = NULL;
68 ss.ss_flags = SS_DISABLE;
Dave Allisondfd3b472014-07-16 16:04:32 -070069 ss.ss_size = kHostAltSigStackSize; // Avoid ENOMEM failure with Mac OS' buggy libc.
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070070 SigAltStack(&ss, NULL);
71
72 // Free it.
73 delete[] allocated_signal_stack;
74}
75
Elliott Hughes8daa0922011-09-11 13:46:25 -070076} // namespace art