2022-06-27 13:22:12 -04:00
|
|
|
/* Copyright (c) 2015, 2020 The Linux Foundation. All rights reserved.
|
2020-08-02 09:55:00 -04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* * Neither the name of The Linux Foundation, nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
|
|
|
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
2022-06-27 13:22:12 -04:00
|
|
|
#include <sys/prctl.h>
|
2020-08-02 09:55:00 -04:00
|
|
|
#include <LocThread.h>
|
|
|
|
#include <string.h>
|
2022-06-27 13:22:12 -04:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2020-08-02 09:55:00 -04:00
|
|
|
#include <loc_pla.h>
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
using std::weak_ptr;
|
|
|
|
using std::shared_ptr;
|
|
|
|
using std::thread;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace loc_util {
|
|
|
|
|
2020-08-02 09:55:00 -04:00
|
|
|
class LocThreadDelegate {
|
2022-06-27 13:22:12 -04:00
|
|
|
static const char defaultThreadName[];
|
|
|
|
weak_ptr<LocRunnable> mRunnable;
|
|
|
|
thread mThread;
|
|
|
|
LocThreadDelegate(const string tName, shared_ptr<LocRunnable> r);
|
2020-08-02 09:55:00 -04:00
|
|
|
public:
|
2022-06-27 13:22:12 -04:00
|
|
|
~LocThreadDelegate() {
|
|
|
|
shared_ptr<LocRunnable> runnable = mRunnable.lock();
|
|
|
|
if (nullptr != runnable) {
|
|
|
|
runnable->interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline static LocThreadDelegate* create(const char* tName, shared_ptr<LocRunnable> runnable);
|
2020-08-02 09:55:00 -04:00
|
|
|
};
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
const char LocThreadDelegate::defaultThreadName[] = "LocThread";
|
2020-08-02 09:55:00 -04:00
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
LocThreadDelegate* LocThreadDelegate::create(const char* tName, shared_ptr<LocRunnable> runnable) {
|
|
|
|
LocThreadDelegate* threadDelegate = nullptr;
|
2020-08-02 09:55:00 -04:00
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
if (nullptr != runnable) {
|
|
|
|
if (!tName) {
|
|
|
|
tName = defaultThreadName;
|
|
|
|
}
|
2020-08-02 09:55:00 -04:00
|
|
|
|
|
|
|
char lname[16];
|
2022-06-27 13:22:12 -04:00
|
|
|
auto nameSize = strlen(tName) + 1;
|
|
|
|
int len = std::min(sizeof(lname), nameSize) - 1;
|
|
|
|
memcpy(lname, tName, len);
|
2020-08-02 09:55:00 -04:00
|
|
|
lname[len] = 0;
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
threadDelegate = new LocThreadDelegate(lname, runnable);
|
2020-08-02 09:55:00 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
return threadDelegate;
|
2020-08-02 09:55:00 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
LocThreadDelegate::LocThreadDelegate(const string tName, shared_ptr<LocRunnable> runnable) :
|
|
|
|
mRunnable(runnable),
|
|
|
|
mThread([tName, runnable] {
|
|
|
|
prctl(PR_SET_NAME, tName.c_str(), 0, 0, 0);
|
2020-08-02 09:55:00 -04:00
|
|
|
runnable->prerun();
|
2022-06-27 13:22:12 -04:00
|
|
|
while (runnable->run());
|
2020-08-02 09:55:00 -04:00
|
|
|
runnable->postrun();
|
2022-06-27 13:22:12 -04:00
|
|
|
}) {
|
2020-08-02 09:55:00 -04:00
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
mThread.detach();
|
2020-08-02 09:55:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
|
|
|
|
bool LocThread::start(const char* tName, shared_ptr<LocRunnable> runnable) {
|
2020-08-02 09:55:00 -04:00
|
|
|
bool success = false;
|
|
|
|
if (!mThread) {
|
2022-06-27 13:22:12 -04:00
|
|
|
mThread = LocThreadDelegate::create(tName, runnable);
|
2020-08-02 09:55:00 -04:00
|
|
|
// true only if thread is created successfully
|
|
|
|
success = (NULL != mThread);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocThread::stop() {
|
2022-06-27 13:22:12 -04:00
|
|
|
if (nullptr != mThread) {
|
|
|
|
delete mThread;
|
|
|
|
mThread = nullptr;
|
2020-08-02 09:55:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:22:12 -04:00
|
|
|
} // loc_util
|