C++ blocking API needs updating Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ blocking API needs updating

Postby Giovanni » Fri Apr 27, 2018 8:35 am

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

User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Location: Tallinn, Estonia
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: C++ blocking API needs updating

Postby Spym » Fri Apr 27, 2018 8:52 am

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.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ blocking API needs updating

Postby Giovanni » Fri Apr 27, 2018 9:15 am

Some kind of object is needed for all those functions that return a pointer to threads, it is basically just an encapsulated pointer.

Giovanni

User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Location: Tallinn, Estonia
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: C++ blocking API needs updating

Postby Spym » Fri Apr 27, 2018 3:36 pm

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?

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ blocking API needs updating

Postby Giovanni » Sat Apr 28, 2018 9:46 am

Merged.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ blocking API needs updating

Postby Giovanni » Sat Apr 28, 2018 5:25 pm

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.

User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Location: Tallinn, Estonia
Has thanked: 2 times
Been thanked: 3 times
Contact:

Re: C++ blocking API needs updating

Postby Spym » Sat Apr 28, 2018 6:55 pm

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.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: C++ blocking API needs updating

Postby Giovanni » Sat Apr 28, 2018 8:06 pm

By "it" I meant the whole wrapper.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 26 guests