Page 2 of 2
Re: C++ blocking API needs updating
Posted: Fri Apr 27, 2018 8:35 am
by Giovanni
Hi,
I added const modifiers and replaced NULL with nullptr, I still fail to see how BaseThread is a descendant of ThreadReference (which I made final). Could you point out where this slicing would occur?
Giovanni
Re: C++ blocking API needs updating
Posted: Fri Apr 27, 2018 8:52 am
by Spym
Sorry! I just looked at the code again and indeed, the problem is gone. Previously I was looking at an outdated file.
It's an unexpected solution but it works. It would make sense for the reference to be a base class of BaseThread, but it certainly isn't necessary.
Re: C++ blocking API needs updating
Posted: Fri Apr 27, 2018 9:15 am
by Giovanni
Some kind of object is needed for all those functions that return a pointer to threads, it is basically just an encapsulated pointer.
Giovanni
Re: C++ blocking API needs updating
Posted: Fri Apr 27, 2018 3:36 pm
by Spym
Thanks. It also occurred to me that we're missing RAII helpers for mutexes and critical sections. I have them defined in my application, but there's no reason why they shouldn't be part of the OS. Consider the following code (copy-pasted from my application with minimal changes):
Code: Select all
namespace impl_
{
class MutexLockerImpl
{
chibios_rt::Mutex& mutex_;
public:
MutexLockerImpl(chibios_rt::Mutex& m) : mutex_(m) { mutex_.lock(); }
~MutexLockerImpl() { mutex_.unlock(); }
};
class CriticalSectionLockerImpl
{
volatile const ::syssts_t st_ = chSysGetStatusAndLockX();
public:
~CriticalSectionLockerImpl() { chSysRestoreStatusX(st_); }
};
}
/**
* RAII mutex locker.
* Must be volatile in order to prevent the optimizer from throwing it away.
*/
using MutexLocker = volatile impl_::MutexLockerImpl;
/**
* RAII critical section locker.
* Must be volatile in order to prevent the optimizer from throwing it away.
*/
using CriticalSectionLocker = volatile impl_::CriticalSectionLockerImpl;
Would you be up to adding that to the wrapper?
Re: C++ blocking API needs updating
Posted: Sat Apr 28, 2018 9:46 am
by Giovanni
Merged.
Giovanni
Re: C++ blocking API needs updating
Posted: Sat Apr 28, 2018 5:25 pm
by Giovanni
I added a "Monitor" construct but there is no obvious way to implement Java-like synchronized methods, locking and unlocking the inner mutex must be done explicitly.
Several other refinements and documentation fixes.
I think it needs more work before making it "official".
Giovanni.
Re: C++ blocking API needs updating
Posted: Sat Apr 28, 2018 6:55 pm
by Spym
RAII is a common pattern in C++. The code I posted is a complete solution that requires no further modifications, it's quite well tested as well.
Re: C++ blocking API needs updating
Posted: Sat Apr 28, 2018 8:06 pm
by Giovanni
By "it" I meant the whole wrapper.
Giovanni