added vrpmdv-rtservice, vrpmdv-mon-datafile,
vrpmdv-monitoring-controler, vrpmdv-mon-tty
This commit is contained in:
116
recipes-vrpmdv/recipes-base/vrpmdv-mon-datafile/files/lock.csave
Normal file
116
recipes-vrpmdv/recipes-base/vrpmdv-mon-datafile/files/lock.csave
Normal file
@@ -0,0 +1,116 @@
|
||||
include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
std::string data;
|
||||
bool ready = false;
|
||||
bool processed = false;
|
||||
|
||||
void worker_thread()
|
||||
{
|
||||
// wait until main() sends data
|
||||
std::unique_lock lk(m);
|
||||
cv.wait(lk, []{ return ready; });
|
||||
|
||||
// after the wait, we own the lock
|
||||
std::cout << "Worker thread is processing data\n";
|
||||
data += " after processing";
|
||||
|
||||
// send data back to main()
|
||||
processed = true;
|
||||
std::cout << "Worker thread signals data processing completed\n";
|
||||
|
||||
// manual unlocking is done before notifying, to avoid waking up
|
||||
// the waiting thread only to block again (see notify_one for details)
|
||||
lk.unlock();
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::thread worker(worker_thread);
|
||||
|
||||
data = "Example data";
|
||||
// send data to the worker thread
|
||||
{
|
||||
std::lock_guard lk(m);
|
||||
ready = true;
|
||||
std::cout << "main() signals data ready for processing\n";
|
||||
}
|
||||
cv.notify_one();
|
||||
|
||||
// wait for the worker
|
||||
{
|
||||
std::unique_lock lk(m);
|
||||
cv.wait(lk, []{ return processed; });
|
||||
}
|
||||
std::cout << "Back in main(), data = " << data << '\n';
|
||||
|
||||
worker.join();
|
||||
}
|
||||
|
||||
--------------------
|
||||
|
||||
// waiting for timeout after 5 seconds
|
||||
std::chrono::seconds timeoutPeriod = 5;
|
||||
auto timePoint = std::chrono::system_clock::now() + timeoutPeriod;
|
||||
std::unique_lock<std::mutex> uLock(myDataMutex);
|
||||
while(!DataAreReadyForProcessing())
|
||||
{
|
||||
if (myCondVar.wait_until(uLock, timePoint) //<##
|
||||
== std::cv_status::timeout)
|
||||
{
|
||||
// data conditions where not fulfilled within
|
||||
// the time period; e.g. do some error handling
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
--
|
||||
|
||||
if (myCondVar.wait_for(uLock, timeoutPeriod,
|
||||
DataAreReadyForProcessing))
|
||||
{
|
||||
// data conditions where fulfilled
|
||||
// regular processing
|
||||
}
|
||||
else // timeout occured, conditions are not fulfilled
|
||||
{
|
||||
// e.g. do some error handling
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-------------
|
||||
|
||||
|
||||
static int rpmsg_sample_probe(struct rpmsg_device *rpdev)
|
||||
{
|
||||
|
||||
int ret;
|
||||
struct instance_data *idata;
|
||||
|
||||
dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
|
||||
rpdev->src, rpdev->dst);
|
||||
|
||||
idata = devm_kzalloc(&rpdev->dev, sizeof(*idata), GFP_KERNEL);
|
||||
if (!idata)
|
||||
return -ENOMEM;
|
||||
|
||||
dev_set_drvdata(&rpdev->dev, idata);
|
||||
|
||||
/* send a message to our remote processor */
|
||||
ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
|
||||
if (ret) {
|
||||
dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
Reference in New Issue
Block a user