Shaka Player Embedded
thread.cc
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/debug/thread.h"
16 
17 #include <glog/logging.h>
18 
19 #include <utility>
20 
22 #include "src/util/utils.h"
23 
24 namespace shaka {
25 
26 namespace {
27 
28 void ThreadMain(const std::string& name, std::function<void()> callback) {
29 #if defined(OS_MAC) || defined(OS_IOS)
30  pthread_setname_np(name.c_str());
31 #elif defined(OS_POSIX)
32  pthread_setname_np(pthread_self(), name.c_str());
33 #else
34 # error "Not implemented for Windows"
35 #endif
36 
37 #ifdef DEBUG_DEADLOCKS
38  util::Finally scope(&WaitingTracker::ThreadExit);
39 #endif
40  callback();
41 }
42 
43 } // namespace
44 
45 Thread::Thread(const std::string& name, std::function<void()> callback)
46  : name_(name), thread_(&ThreadMain, name, std::move(callback)) {
47  DCHECK_LT(name.size(), 16u) << "Name too long: " << name;
48 #ifdef DEBUG_DEADLOCKS
49  original_id_ = thread_.get_id();
50  WaitingTracker::AddThread(this);
51 #endif
52 }
53 
55  DCHECK(!thread_.joinable());
56 #ifdef DEBUG_DEADLOCKS
57  WaitingTracker::RemoveThread(this);
58 #endif
59 }
60 
61 } // namespace shaka
const char * name
Thread(const std::string &name, std::function< void()> callback)
Definition: thread.cc:45