Apollo 10.0
自动驾驶开放平台
thread.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 *****************************************************************************/
17
18#include <csignal>
19
20#include "cyber/common/log.h"
21
22namespace apollo {
23namespace perception {
24namespace lib {
25
27 pthread_attr_t attr;
28 CHECK_EQ(pthread_attr_init(&attr), 0);
29 CHECK_EQ(
30 pthread_attr_setdetachstate(
31 &attr, joinable_ ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED),
32 0);
33 CHECK_EQ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr), 0);
34 CHECK_EQ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr), 0);
35
36 int result = pthread_create(&tid_, &attr, &ThreadRunner, this);
37 CHECK_EQ(result, 0) << "Could not create thread (" << result << ")";
38
39 CHECK_EQ(pthread_attr_destroy(&attr), 0);
40
41 started_ = true;
42}
43
45 ACHECK(joinable_) << "Thread is not joinable";
46 int result = pthread_join(tid_, nullptr);
47 CHECK_EQ(result, 0) << "Could not join thread (" << tid_ << ", "
48 << thread_name_ << ")";
49 tid_ = 0;
50}
51
53 if (tid_ == 0) {
54 return false;
55 }
56 // no signal sent, just check existence for thread
57 int ret = pthread_kill(tid_, 0);
58 if (ret == ESRCH) {
59 return false;
60 }
61 if (ret == EINVAL) {
62 AWARN << "Invalid singal.";
63 return false;
64 }
65
66 return true;
67}
68
69} // namespace lib
70} // namespace perception
71} // namespace apollo
static void * ThreadRunner(void *arg)
Definition thread.h:52
#define ACHECK(cond)
Definition log.h:80
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37