Need help to implement an interrupt controller simulator which handles the ISR - systemc

I am on the implementation of an interrupt controller simulator, which will take signals from other rest of the HW modules in simulation and run the ISR.
Below is the SystemC code roughly made to get the concept clear. In this case, we need ISR to be handled in a way that, even if the FW_main is stuck inside while(1) loop.
With the below implementation the context is inside FW_main loop only. Adding a wait in FW_main is not the one we want. We need the correct interrupt controller functionality. Any ideas to get rid of this problem?
SC_MODULE (processor)
{
sc_in < bool > interrupt;
void ISR(void)
{
cout << "i am in ISR\n";
}
void FW_main(void)
{
while(1)
{
cout << "i am in FW_main\n";
}
}
SC_CTOR (processor)
{
SC_METHOD(ISR);
sensitive << interrupt;
SC_THREAD(FW_main);
}
};

Unfortunately SystemC processes are cooperative, not preemptive. Even the SystemC kernel can't step in and suspend the FW_main method.
No processor system / FW truly gets stuck in a while loop this way. Any instruction set simulator must walk the time in steps on some sort of strobes or events, ideally clock edges.
Functional representation of a system you are trying to model would look something like follows.
SC_MODULE (processor)
{
sc_in < bool > clk;
sc_in < bool > interrupt;
void ISR(void)
{
cout << "i am in ISR\n";
}
void FW_main(void)
{
cout << "i am in FW_main\n";
}
SC_CTOR (processor)
{
SC_METHOD(ISR);
sensitive << interrupt;
SC_METHOD(FW_main);
sensitive << clk;
}
};
There are two problems in above code I suggested. First, you probably don't want an actual clock signal that needs toggling externally or any sense of time at all for that matter. Second, in a single core processor system, ISRs and FW_Main aren't really parallel in nature. A more realistic implementation of what you are trying to model would be as follows.
SC_MODULE(processor)
{
sc_in < bool > interrupt;
void ISR(void)
{
cout << "i am in ISR\n";
}
void FW_main(void)
{
if(interrupt.read())
{
ISR();
}
cout << "i am in FW_main\n";
next_trigger(SC_ZERO_TIME, interrupt);
}
SC_CTOR (processor)
{
SC_METHOD(FW_main);
}
};
The next_trigger(SC_ZERO_TIME, interrupt) statement makes the FW_main emulate while(1) while also being sensitive to interrupt input.

Related

how spawn and post works with asio?

// I asked this question
// https://stackoverflow.com/questions/61026135/asio-use-future-instead-of-yieldec
// and comments lead to need to modify code of answer and put in in this new
// question.
// I tried to ask questions in form of code trials and causes of writing them
// or how i under stand them
// asio_packaged_task.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#define BOOST_COROUTINES_NO_DEPRECATION_WARNING
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/use_future.hpp>
#include <boost/bind.hpp>
#include <iostream>
using boost::system::error_code;
namespace asio = boost::asio;
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token) {
#if BOOST_VERSION >= 106600
using result_type =
typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(
std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type handler(
std::forward<Token>(token));
asio::async_result<decltype(handler)> result(handler);
#endif
if (success)
handler(error_code{}, 42); // 4-18-2020 this line happens when
// async_meaning_of_life work is done,this
// line is calling the handler and passing it
// the result of async_meaning_of_life
// function which here for simplicity are
// supplied as error_code{} and 42
else
handler(asio::error::operation_aborted, 0);
return result.get();
}
void using_yield_ec(asio::yield_context yield) {
for (bool success : { true, false }) {
boost::system::error_code ec;
auto answer = async_meaning_of_life(success, yield[ec]);
std::cout << __FUNCTION__ << ": Result: " << ec.message() << "\n";
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
}
}
void using_yield_catch(asio::yield_context yield) {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, yield);
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
// 4-18-2020 something interesting happens here,when we call the function
// using_future or using_handler in the same thread we get into these two
// functions then inside them we call async_meaning_of_life which is an
// initiating function ,the async_meaning_of_life has two parts: its code which
// ends before if(success) then it calls the completion token passed to it which
// is promise OR lambda "it might be fuction object ,functor,function pointer, "
// using handler(error,42) where handler represents the true handler type
// according to the token passed to function. then it returns the result by
// result.get to using_future or using_handler. inside using handler we notice
// that code returns back to lambda after handler(error,42) .if completion token
// were bind or function object,we would have seen code jumping to bound
// function or function object
void using_future() {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
void using_handler() {
for (bool success : { true, false })
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "\n";
std::cout << "using_handler: Answer: " << answer << "\n";
});
}
void print(const boost::system::error_code& /*e*/) {
std::cout << "Hello, world!" << std::endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename Token>
auto async_meaning_of_life_composed(bool success, Token&& token) {
#if BOOST_VERSION >= 106600
using result_type =
typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(
std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type handler(
std::forward<Token>(token));
asio::async_result<decltype(handler)> result(handler);
#endif
// here i will add intermediate initiating functions
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "\n";
std::cout << "using_handler: Answer: " << answer << "\n";
});
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
}
// using_yield_ec(asio::yield_context yield);
// spawn(svc, using_yield_ec);
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
if (success)
handler(error_code{}, 42); // 4-18-2020 this line happens when
// async_meaning_of_life work is done,this
// line is calling the handler and passing it
// the result of async_meaning_of_life
// function which here for simplicity are
// supplied as error_code{} and 42
else
handler(asio::error::operation_aborted, 0);
return result.get();
}
void using_future_composed() {
for (bool success : { true, false })
try {
auto answer =
async_meaning_of_life_composed(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
int main() {
asio::io_service svc;
boost::asio::steady_timer t(svc, boost::asio::chrono::seconds(45));
// this function returns immediately and make new thread
t.async_wait(&print);
// this function returns immediately>>>>also it adds 1 out standing work to
// svc.is async_wait body runned in main threaed OR in another thread????if
// it is ran in another thread,how immediate return happens"not
// blocking"??why async_meaning is not returning immediately like
// async_wait?
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main thread
// >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
svc.post([]() { // this adds 1 outstanding work to svc and does not start
auto answer = async_meaning_of_life(true, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
svc.post(using_future);
// this increase outstanding work by 1
// boost::asio::yield_context yield;
// 4-18-2020 this is only used with spawn ,if you want to use stakeful
// coroutines,use push and pull types of coroutine "i wonder how to do
// this???"
// using_yield_ec( yield);this is wrong usage
// using_yield_catch( yield);this is wrong usage
// using_future();this is normal usage but it does not return immediately
// and it executes in main thread.
// using_handler();
svc.post(using_future_composed);
spawn(svc, using_yield_ec);
// this adds 2 outstanding work to svc why 2 works are made while we are
// launching one function????
spawn(svc, using_yield_catch);
// what i think i understand about mechanism of work of spawn:spawn is
// called from main thread>>>>it is just used with coroutines taking
// yield_context as argument,spawn post function to service,spawn makes link
// between the context in which service will be ran"may be main thread or
// new thread AND the context of coroutine function ran in same thread as
// service" or may be the coroutine makes new thread in which it is
// running???".Then when svc.run is called,svc calls task"here svc is caller
// and coroutine is callee",task is executing,yield is called as completion
// token"can we call yield outside initiating function to switch to caller
// "here caller is svc"????. then we are now in svc context which calls
// another task .....
// t.async_wait(&using_future);wrong usage leading to error?why can not in
// use using_future function as completion callback with async_wait???
// spawn(svc, using_future);wrong usage as using_future is not coroutine?
std::thread work([] {
using_future();
using_handler();
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main
// thread >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
std::thread work_io([&] { // this starts new thread in which svc is run
svc.run();
});
svc.run(); // this run svc in main thread
// general question:
/*
using_* is considered normal function or coroutine OR composed operation??
async_meaning is considered initiating function?
why does not it return immediately when ran in main thread?how can we make
it return immediately and then when certain result is present ,it calls its
callback??
async_wait is considered initiating function? why does it return
immediately then when timer expires ,it calls back its completion token??
can i make the following composed operation:
i will make composed operation which returns future to caller thread,
and inside it i shall call another composed operation with coroutine,
*/
work.join();
work_io.join();
}
boost::asio::steady_timer t(svc, boost::asio::chrono::seconds(45));
// this function returns immediately and make new thread
No it doesn't create a new thread. It just constructs a service object (the timer) and returns. obviously immediately, like std::string s("hello"); returns when the string is constructed.
t.async_wait(&print);
// this function returns immediately>>>>also it adds 1 out standing work to
// svc. is async_wait body runned in main threaed OR in another thread????if
// it is ran in another thread,how immediate return happens"not
// blocking"??why async_meaning is not returning immediately like
// async_wait?
Slow down.
is async_wait body runned in main threaed OR in another thread?
It's just a function. It runs on the current thread. Like when you called printf.
if it is ran in another thread, how immediate return happens "not
blocking"?
Well, it's not in another thread. But if it were, then it would be obvious how
it would return "not blocking": because the work is not happening on the
current thread.
Why async_meaning_of_life is not returning immediately like
async_wait?
It is returning immediately.
Now, a bit subtler: Even if you use it with a yield_context (inside a
coroutine). It will return immediately and cause the the coroutine to yield.
This means that other tasks get a chance to run on the service thread(s) and
only when the async operation completed, the coroutine will be resumed. From
the point of view of the coroutine, it will have appeared as if the call was
blocking. This is the whole point of (stackful) coroutines. It "abstracts away"
the asynchrony.
So, yes, async_meaning_of_life always (always) returns (almost) immediately.
svc.post([]() { // this adds 1 outstanding work to svc and does not start
Correct. Use a {poll|run}[_one,_for,_until] function to run tasks².
auto answer = async_meaning_of_life(true, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
You don't ask anything here, but using a future just to await it immediately is
an anti-pattern¹. It's absolutely useless, as it will always generate blocking
behaviour.
You should store the future somewhere, do other work and then when you need the result of the future (and it may or may not have already been completed) you await it (.get() You should store the future somewhere, do other work and then when you need the result of the future (and it may or may not have already been completed) you await it (e.g. by invoking .get()).
// using_yield_ec( yield);this is wrong usage
// using_yield_catch( yield);this is wrong usage
Correct. Used correctly, the Asio service will provide a yield context for you.
// boost::asio::yield_context yield;
// 4-18-2020 this is only used with spawn ,if you want to use stakeful
// coroutines,use push and pull types of coroutine "i wonder how to do
// this???"
No idea. Just refer to the documentation of Boost Coroutine (I suggest Boost Coroutine2). This is off-topic for Asio async operations.
// using_future();this is normal usage but it does not return immediately
// and it executes in main thread.
Well, duh. You took it from a minimal example that ONLY shows the mechanics of the different async_result tokens.
Just refer to a few lines above:
You should store the future somewhere, do other work and then when you need
the result of the future (and it may or may not have already been completed)
you await it (.get() You should store the future somewhere, do other work
and then when you need the result of the future (and it may or may not have
already been completed) you await it (e.g. by invoking .get()).
svc.post(using_future_composed);
Again, I see no questions, but I don't think it means you understand it. I tread.
I see using_future_composed is basically using_future but calling async_meaning_of_life_composed instead.
Now looking at async_meaning_of_life_composed I have no idea what that's supposed to do. It looks like async_meaning_of_life with random lines of code added, doing all kinds of things including blocking operations (see anti-pattern¹) in a function that is supposed to schedule a async operation only.
That's just not what you want to do. Ever.
spawn(svc, using_yield_ec);
// this adds 2 outstanding work to svc why 2 works are made while we are
// launching one function????
Honestly, I do not know. I assume it's because the launch of the coro itself is posted onto the work queue, so it runs exception-safely from one of the worker threads.
The bigger point here is that you still haven't actually started any io-workers, see [²] above.
spawn(svc, using_yield_catch);
// what i think i understand about mechanism of work of spawn:spawn is
// called from main thread>>>>it is just used with coroutines taking
// yield_context as argument,spawn post function to service,spawn makes link
// between the context in which service will be ran"may be main thread or
// new thread AND the context of coroutine function ran in same thread as
// service"...
Erm, basically, yes.
// ... or may be the coroutine makes new thread in which it is
// running???" ...
Definitely not. Both Coroutines and Asio are a device/framework to arrive at concurrency without necessarily multi-threading. Coroutine will never create a thread. Asio will typically not create any threads (unless to implement certain kinds of services on some platforms, but they'd be implementation-details and your tasks/handlers will never run on such a hidden thread).
// ... .Then when svc.run is called,svc calls task"here svc is caller
// and coroutine is callee",task is executing,yield is called as completion
// token"can we call yield outside initiating function to switch to caller
// "here caller is svc"????. then we are now in svc context which calls
// another task .....
Huh. No, yield_context is not a portal to a different time-space continuum.
I'm not very sure what you mean with `'call yield' so when you are thinking about calling it from outside the initiating function, I'd say: probably don't do that.
// t.async_wait(&using_future);wrong usage leading to error?why can not in
// use using_future function as completion callback with async_wait???
Because it doesn't satisfy the handler requirements for steady_time::async_wait (which should take a boost::system::error_code only. Did you perhaps mean use_future (from Asio) instead of your own using_future?
auto ignored_future = t.async_wait(boost::asio::use_future);
I admit the names are somewhat confusing. If it helps, rename all the using_XYZ functions to demonstration_using_XYZ.
// spawn(svc, using_future);wrong usage as using_future is not coroutine?
You got that right.
std::thread work([]
using_future();
using_handler();
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main
// thread >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
I believe you just copy/pasted the comment, but in case you really worried: no that is not run on the main thread. It's run on the work thread, and yes, that's because you block on the future::get(). See above¹.
std::thread work_io([&] { // this starts new thread in which svc is run
svc.run();
});
Better late than never :)
svc.run(); // this run svc in main thread
Correct, and running more doesn't hurt. Running the service on multiple threads may require handler synchronization: Why do I need strand per connection when using boost::asio?
// general question:
/*
using_* is considered normal function or coroutine OR composed operation??
Normal functions (see the clarification about renaming it to demonstration_using_XYZ above)
async_meaning is considered initiating function?
Correct.
why does not it return immediately when ran in main thread?
It does. See above. If you mean, why does your own function async_meaning_of_life_composed bblock? That's because you made it do blocking operations (see above).
how can we make
it return immediately and then when certain result is present ,it calls its
callback??
The usual way to do to it, is by launching other async operations. Say, for example, you wait for network operation to complete (asynchronously, e.g. using boost::asio::async_write) and when it's done, you invoke the handler. The async_result helper makes it so you don't have to know the actual completion_handler_type, and it will "magically" do the right thing regardless of how your initiating function was invoked.
async_wait is considered initiating function? why does it return
immediately then when timer expires ,it calls back its completion token??
Because that's how async operations are designed. They were designed that way because that is useful behaviour.
can i make the following composed operation:
i will make composed operation which returns future to caller thread,
and inside it i shall call another composed operation with coroutine,
*/
You are free to start a coroutine. Just make sure you transfer ownership of the async_result result so you can invoke the handler from there, to signal completion of your operation.
In the case of futures, the usual way to compose operations is by composing futures, like: https://www.boost.org/doc/libs/1_72_0/doc/html/thread/synchronization.html#thread.synchronization.futures.then
std::string someotheroperation(int);
future<int> fut1 = foo();
future<std::string> fut2 = foo().then(someotheroperation);
BONUS
The ultimate piece of documentation on writing Composed Operations with Asio is (ironically) this page in the Beast documentation. Perhaps seeing some more real-life examples may give you more ideas.
Keep in mind Beast comes with a few facilities that make library maintenance for /them/ a bit easier, but could well be overkill for your own application. Then again, if you err on theur path you will not overlook important things like the one we discussed here earlier:

Is it safe to call sc_fifo::nb_write() from a SC_THREAD process?

I am converting some of my code from a SC_THREAD to a SC_METHOD. My question is, do I need to stop using the sc_fifo class? I realize an SC_METHOD should not call sc_fifo.write() because this uses a wait call which is not allowed for functions that cannot be suspended. However, sc_fifo provides non-blocking versions of various functions and potentially I could use these instead. Some of the documentation I've read indicates you should never use sc_fifo from a SC_METHOD at all but provided no justification.
Here is a sample of code I am currently using.
class Example : public sc_module {
public:
sc_fifo<int> myFifo;
sc_in<bool> clock_in;
SC_HAS_PROCESS(Example);
// constructor
Example(sc_module_name name) : sc_module(name) {
SC_METHOD(read);
sensitive << clock_in;
}
void read() {
int value = -1;
bool success = myFifo.nb_read(value);
if (success) { cout << "Read value " << value << endl; }
else { cout << "No read done but that's okay." << endl; }
}
};
int sc_main(int argc, char* argv[]) {
sc_clock clock("clock");
Example example("example");
example.clock_in(clock);
sc_start(10, SC_NS);
return 0;
}
This throws no errors even though I am calling an sc_fifo function from a SC_METHOD. Is it bad policy to use nb_read() from inside a SC_METHOD? If so why?
Using sc_fifo non-blocking calls from SC_METHOD should be fine.
I have not found any place in standard manual that prohibits it.
Neither nb_read, nor nb_write, as their names suggest, call wait internally so it's fine to use them from an SC_METHOD.
While your example code works, it's rather inefficient when things are put into the fifo infrequently. If you want your code to be more event driven, you could make the SC_METHOD sensitive to sc_fifo.data_written_event(); then it will only be called when something is actually written to the fifo (though it's still a good idea to check that nb_read returns true in case something else pulled from the same fifo). Of course, this would skip your "No read done but that's okay." prints.
Also, I think the title of your question probably meant to ask about calling nb_write from SC_METHOD rather than SC_THREAD.

Can a io_context::strand guarantee the order between an async_* completion handler and custom functor?

I have this situation where I need to have an async_read operation "prepared" for reading before I call a custom function which is sending something on the counter-part of the websocket. I have the completion handler of the async_read wrapped in the same strand to which I post my functor.
The problem is that sometimes on::read is not called, so basically I think that the writeFromCounterpart() is called before the ws_1 was in "reading" state.
My understanding is that strand is guaranteeing the order between completion handlers, but I don't understand if guarantees that the async_* operation is "ready" (running in its thread and reading) before continuing with other operation from the strand FIFO.
Full code bellow: When running it, most of the time I've seen the following output:
on_read called
Run 2 Handlers
I was able to see the following also a few times (~1 in 20 in stress condition),
on_write called
Run 1 Handlers
I never seen both on_write and on_read
Code:
#include <boost/asio.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/core.hpp>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
boost::beast::multi_buffer buffer;
boost::asio::io_context ioc_1;
boost::asio::io_context ioc_2;
websocket::stream<tcp::socket> ws_1(ioc_1);
websocket::stream<tcp::socket> ws_2(ioc_2);
void on_write(boost::system::error_code, std::size_t) {
std::cout << "on_write called" << std::endl << std::flush;
}
void writeFromCounterpart() {
ws_2.async_write(boost::asio::buffer(std::string("Hello")), on_write);
}
void on_read(boost::system::error_code, std::size_t) {
std::cout << "on_read called" <<std::endl << std::flush;
}
int main() {
std::thread t([](){
auto const address = boost::asio::ip::make_address("127.0.0.1");
tcp::acceptor acceptor{ioc_2, {address, 30000}};
tcp::socket socket{ioc_2};
acceptor.accept(socket);
websocket::stream<tcp::socket> ws{std::move(socket)};
ws.accept();
ws_2 = std::move(ws);
ioc_2.run();
});
t.detach();
// allow acceptor to accept
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tcp::resolver resolver_(ioc_1);
boost::asio::io_context::strand strand_(ioc_1);
auto const results = resolver_.resolve("127.0.0.1", "30000");
boost::asio::connect(ws_1.next_layer(), results.begin(), results.end());
ws_1.handshake("127.0.0.1", "/");
ws_1.async_read(buffer, strand_.wrap(on_read));
strand_.post(writeFromCounterpart);
auto handlers = ioc_1.run_for(std::chrono::milliseconds(5000));
std::cout << "Run " + std::to_string(handlers) + " Handlers" << std::endl << std::flush;
}
The short answer is: yes.
The documentation for it is here:
https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/io_context__strand.html#boost_asio.reference.io_context__strand.order_of_handler_invocation
The problem is that sometimes when I run this flow, on::read is not called, so basically I think that the SendSomethingOnTheWs is called before the ws_ was in "reading" state.
Why would that matter? Streaming sockets (ws or otherwise) behave like streams. If the socket was open the data will just be buffered.

Understanding semaphores with pthreads (code included)

So in class, we learned about semaphores and stuff and our professor let us know that this code below would be handy to learn for our exam. Unfortunately our exam is on Friday, and whole list of excuses, i just need to be able to understand this code for the exam and for future cases. I understand that the mutex_t is a lock system and the cond_t is a condition system in which signals get passed through sema_P and sema_V (if the value is 0, race condition occurs and the thread is locked out by cond_wait until another thread increases the value and is unlocked by cond_signal), but why does a lock need to get passed around? Why is there a mutex_lock and mutex_unlock in both decrementer P() and incrementer V()? How does this work with the threads and the conditions (cont_t)?
typedef struct
{
pthread_mutex_t lock;
pthread_cond_t wait;
int value;
} sema;
void pthread_sema_init(sema *s, int count)
{
s->value = count;
pthread_cond_init(&(s->wait),NULL);
pthread_mutex_init(&(s->lock),NULL);
return;
}
void pthread_sema_P(sema *s)
{
pthread_mutex_lock(&(s->lock));
s->value--;
if(s->value < 0) {
pthread_cond_wait(&(s->wait),&(s->lock));
}
pthread_mutex_unlock(&(s->lock));
return;
}
void pthread_sema_V(sema *s)
{
pthread_mutex_lock(&(s->lock));
s->value++;
if(s->value <= 0) {
pthread_cond_signal(&(s->wait));
}
pthread_mutex_unlock(&(s->lock));
}
The mutex sema.lock is there to protect the shared variable sema.value, ensuring that only one thread accesses that value at a time. Both pthread_sema_P() and pthread_sema_V() must take the lock because they both access sema.value.
That implementation of sempahores is buggy, by the way - it doesn't handle spurious wakeups (a "spurious wakeup" is where pthread_cond_wait() wakes up despite not being signalled - this is allowed by the spec).
A more traditional implementation might be:
void pthread_sema_P(sema *s)
{
pthread_mutex_lock(&s->lock);
while (s->value < 1) {
pthread_cond_wait(&s->wait, &s->lock);
}
s->value--;
pthread_mutex_unlock(&s->lock);
}
void pthread_sema_V(sema *s)
{
pthread_mutex_lock(&s->lock);
s->value++;
pthread_cond_signal(&s->wait);
pthread_mutex_unlock(&s->lock);
}

How can I get fewer Techlog DataSelection events?

I wrote this test for DataSelection Event in my ternary plot in Techlog, and it gets too much data.
What am I doing wrong here?
In run method – subscribe to event:
void SetupTernaryPlot::run()
{
workspace.connect(Workspace::SelectionChanged, this, SLOT(onSelectionChanged(const Slb::Techlog::SelectionChangedArgs&)));
}
This is my event handler:
void SetupTernaryPlot::onSelectionChanged(const Slb::Techlog::SelectionChangedArgs args)
{
qWarning() << "TernaryPlot::onInteractiveSelectionChanged";
int i=0;
foreach (Selection selection, args.selectionsChanged())
{
qWarning() << "Interactive Selection " << i++ << selection.dataset().referenceVariable().rowCount() << endl; //selectlist;
}
}
My event handler is getting too many points. How can I know what has been selected?
It is indeed possible to achieve this, even though the solution is not very elegant at this point.
If you study the "setuplogview" sample (SetupLogview::onSelectionChanged) in the package, you can find that you may use using -1 to filter out non-selected indices.
(If you have access to WAT, there is a ticket created to enhance this https://wat.grabels-fr0235.slb.com/techlog/ticket/24300)