I could not find a complete example code of async_request_update()
. Can anyone please post a simple example.
Asked
Active
Viewed 874 times
3

Zeeshan Hayat
- 401
- 6
- 13
1 Answers
3
#include <systemc>
#include <pthread.h>
#include <unistd.h>
using namespace sc_core;
class ThreadSafeEventIf : public sc_interface {
virtual void notify(sc_time delay = SC_ZERO_TIME) = 0;
virtual const sc_event &default_event(void) const = 0;
protected:
virtual void update(void) = 0;
};
class ThreadSafeEvent : public sc_prim_channel, public ThreadSafeEventIf {
public:
ThreadSafeEvent(const char *name = ""): event(name) {}
void notify(sc_time delay = SC_ZERO_TIME) {
this->delay = delay;
async_request_update();
}
const sc_event &default_event(void) const {
return event;
}
protected:
virtual void update(void) {
event.notify(delay);
}
sc_event event;
sc_time delay;
};
SC_MODULE(Foo)
{
public:
SC_CTOR(Foo)
{
SC_THREAD(main);
SC_METHOD(eventTriggered);
sensitive << event;
dont_initialize();
}
private:
void main() {
usleep(5 * 1000 * 1000); // Just for the example, event is added to pending events during this sleep
wait(SC_ZERO_TIME); // Schedule (event is evaluated here)
usleep(1 * 1000 * 1000); // Just for the example
std::cout << "Done" << std::endl;
}
void eventTriggered() {
std::cout << "Got event" << std::endl;
}
public:
ThreadSafeEvent event;
};
void *externalHostThread(void *arg) {
usleep(2 * 1000 * 1000); // Just for the example
Foo* foo = (Foo*)(arg);
foo->event.notify();
std::cout << "Event notified from an external host thread" << std::endl;
}
int sc_main(int argc, char *argv[])
{
Foo foo("foo");
pthread_t thread;
pthread_create(&thread, NULL, externalHostThread, &foo);
sc_start();
return 0;
}

Guillaume
- 1,277
- 8
- 11
-
Thanks for the example. Could you please explain Foo* foo = (Foo*)(arg); – Zeeshan Hayat Apr 13 '18 at 14:36
-
As pthread argument is a void*, I required to cast it to Foo* to access Foo class attributes. It was one option to access the event but there are many other ways. It is an example :) – Guillaume Apr 13 '18 at 14:57
-
Just want to make sure that I understood it right. so, (Foo*)(arg); is actually a type casting? and pointer foo in the externalHostThread points to the instance of FOO in the main()? – Zeeshan Hayat Apr 14 '18 at 11:33
-
Is it thread safe, if multiple pthreads are notifying at the same time? – Zeeshan Hayat Apr 15 '18 at 18:55
-
Yes it is thread safe. – Guillaume Apr 16 '18 at 08:14
-
Just curious! How does this work? There is no mutex and also no queue involved. apologies in advance (newbie to C++). – Zeeshan Hayat Apr 16 '18 at 09:42
-
There is a mutex for the queue of pending events to evaluate in SystemC kernel (at least in the Accellera implementation). – Guillaume Apr 16 '18 at 10:25
-
The event is only triggered once with the largest sc_time even if I try to trigger it with 3 different pthreads. It skips other events. – Zeeshan Hayat Apr 16 '18 at 12:02
-
By event, I mean timed notification. notify(10,SC_NS) – Zeeshan Hayat Apr 16 '18 at 12:06