| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <stdexcept> | 7 #include <stdexcept> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "native_client/src/shared/platform/nacl_log.h" | 10 #include "native_client/src/shared/platform/nacl_log.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 return mutex_; | 34 return mutex_; |
| 35 } | 35 } |
| 36 | 36 |
| 37 static IThread::ThreadMap_t *ThreadGetMap() { | 37 static IThread::ThreadMap_t *ThreadGetMap() { |
| 38 static IThread::ThreadMap_t* map_ = new IThread::ThreadMap_t; | 38 static IThread::ThreadMap_t* map_ = new IThread::ThreadMap_t; |
| 39 return map_; | 39 return map_; |
| 40 } | 40 } |
| 41 | 41 |
| 42 class Thread : public IThread { | 42 class Thread : public IThread { |
| 43 public: | 43 public: |
| 44 explicit Thread(uint32_t id) : ref_(1), id_(id), state_(DEAD) {} | 44 Thread(uint32_t id, struct NaClAppThread *natp) |
| 45 : ref_(1), id_(id), natp_(natp), state_(DEAD) {} |
| 45 ~Thread() {} | 46 ~Thread() {} |
| 46 | 47 |
| 47 uint32_t GetId() { | 48 uint32_t GetId() { |
| 48 return id_; | 49 return id_; |
| 49 } | 50 } |
| 50 | 51 |
| 51 State GetState() { | 52 State GetState() { |
| 52 return state_; | 53 return state_; |
| 53 } | 54 } |
| 54 | 55 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 return NACL_SIGNAL_RETURN; | 118 return NACL_SIGNAL_RETURN; |
| 118 } else { | 119 } else { |
| 119 // Do not attempt to debug crashes in trusted code. | 120 // Do not attempt to debug crashes in trusted code. |
| 120 return NACL_SIGNAL_SEARCH; | 121 return NACL_SIGNAL_SEARCH; |
| 121 } | 122 } |
| 122 } | 123 } |
| 123 | 124 |
| 124 private: | 125 private: |
| 125 uint32_t ref_; | 126 uint32_t ref_; |
| 126 uint32_t id_; | 127 uint32_t id_; |
| 128 struct NaClAppThread *natp_; |
| 127 State state_; | 129 State state_; |
| 128 struct NaClSignalContext context_; | 130 struct NaClSignalContext context_; |
| 129 | 131 |
| 130 friend class IThread; | 132 friend class IThread; |
| 131 }; | 133 }; |
| 132 | 134 |
| 133 // TODO(mseaborn): This is duplicated in the Windows version. | 135 // TODO(eaeltsin): This is duplicated in the Windows version. |
| 134 IThread* IThread::Acquire(uint32_t id, bool create) { | 136 IThread* IThread::Create(uint32_t id, struct NaClAppThread* natp) { |
| 135 MutexLock lock(ThreadGetLock()); | 137 MutexLock lock(ThreadGetLock()); |
| 136 Thread* thread; | 138 Thread* thread; |
| 137 ThreadMap_t &map = *ThreadGetMap(); | 139 ThreadMap_t &map = *ThreadGetMap(); |
| 138 | 140 |
| 139 // Check if we have that thread | |
| 140 if (map.count(id)) { | 141 if (map.count(id)) { |
| 141 thread = static_cast<Thread*>(map[id]); | 142 NaClLog(LOG_FATAL, "IThread::Create: thread 0x%x already exists\n", id); |
| 142 thread->ref_++; | |
| 143 return thread; | |
| 144 } | 143 } |
| 145 | 144 |
| 146 // If not, can we create it? | 145 thread = new Thread(id, natp); |
| 147 if (create) { | 146 map[id] = thread; |
| 148 // If not add it to the map | 147 return thread; |
| 149 thread = new Thread(id); | 148 } |
| 150 map[id] = thread; | 149 |
| 151 return thread; | 150 // TODO(eaeltsin): This is duplicated in the Windows version. |
| 151 IThread* IThread::Acquire(uint32_t id) { |
| 152 MutexLock lock(ThreadGetLock()); |
| 153 Thread* thread; |
| 154 ThreadMap_t &map = *ThreadGetMap(); |
| 155 |
| 156 if (map.count(id) == 0) { |
| 157 NaClLog(LOG_FATAL, "IThread::Acquire: thread 0x%x does not exist\n", id); |
| 152 } | 158 } |
| 153 | 159 |
| 154 return NULL; | 160 thread = static_cast<Thread*>(map[id]); |
| 161 thread->ref_++; |
| 162 return thread; |
| 155 } | 163 } |
| 156 | 164 |
| 157 // TODO(mseaborn): This is duplicated in the Windows version. | 165 // TODO(eaeltsin): This is duplicated in the Windows version. |
| 158 void IThread::Release(IThread *ithread) { | 166 void IThread::Release(IThread *ithread) { |
| 159 MutexLock lock(ThreadGetLock()); | 167 MutexLock lock(ThreadGetLock()); |
| 160 Thread* thread = static_cast<Thread*>(ithread); | 168 Thread* thread = static_cast<Thread*>(ithread); |
| 161 thread->ref_--; | 169 thread->ref_--; |
| 162 | 170 |
| 163 if (thread->ref_ == 0) { | 171 if (thread->ref_ == 0) { |
| 164 ThreadGetMap()->erase(thread->id_); | 172 ThreadGetMap()->erase(thread->id_); |
| 165 delete static_cast<IThread*>(thread); | 173 delete static_cast<IThread*>(thread); |
| 166 } | 174 } |
| 167 } | 175 } |
| 168 | 176 |
| 169 void IThread::SetExceptionCatch(IThread::CatchFunc_t func, void *cookie) { | 177 void IThread::SetExceptionCatch(IThread::CatchFunc_t func, void *cookie) { |
| 170 NaClSignalHandlerAdd(Thread::SignalHandler); | 178 NaClSignalHandlerAdd(Thread::SignalHandler); |
| 171 s_CatchFunc = func; | 179 s_CatchFunc = func; |
| 172 s_CatchCookie = cookie; | 180 s_CatchCookie = cookie; |
| 173 } | 181 } |
| 174 | 182 |
| 175 | 183 |
| 176 } // End of port namespace | 184 } // End of port namespace |
| 177 | 185 |
| OLD | NEW |