The new C++ wrapper thread

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 13012
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 744 times
Been thanked: 620 times
Contact:

The new C++ wrapper thread

Postby Giovanni » Wed Jan 02, 2013 12:32 pm

Hi,

I finally updated the old C++ wrapper to workaround the various problems and improve the overall architecture. I think now it is a good base for all the improvements discussed in the old thread.

The biggest change is the new "ThreadReference" class that wraps all the operations performed on a specific thread (the function taking a Thread* as parameter), now pointers to threads should no more be necessary.

The BaseThread class is extends ThreadReference and is the ancestor of all thread types. BaseStaticThread is the base class for all static threads. There are not yet classes for dynamic thread but those should fit well in the new architecture.

Queues, Messages, Events, Binary Semaphores, Counter Semaphores, Pools, Mailboxes, Mutexes and CondVars are supported.

TODO: Dynamic Threads, Registry, Channels (how to do channels?), Heap Allocator (new operator wrapping?), other?

How would you see this implementation as an official API? are more changes necessary?

Giovanni

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

Re: The new C++ wrapper thread

Postby Giovanni » Thu Jan 03, 2013 11:03 am

Hi,

Small update, many more subsystems added, I updated the list in the previous post.

I am going to write interfaces for file systems, my idea is to make file system access C++ only. Streams would be wrapped in C++ interfaces, chfiles.h is going to disappear and be reimplemented directly in C++, any objection to this?

Giovanni

Xamusk
Posts: 81
Joined: Sat Mar 05, 2011 10:47 pm

Re: The new C++ wrapper thread

Postby Xamusk » Sat Jan 12, 2013 12:40 am

I'm not sure this is the case when using no-rtti, no-exceptions and newlib, but last time I tried on x86 (not extensively and not with the knowledge I have now), using cout streams took much more space than using printf.

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: The new C++ wrapper thread

Postby mabl » Wed Jan 30, 2013 4:51 pm

Hi Giovanni,

Code: Select all

  /*------------------------------------------------------------------------*
   * chibios_rt::ObjectsPool                                                *
   *------------------------------------------------------------------------*/
  /**
   * @brief   Template class encapsulating a memory pool and its elements.
   */
  template<class T, size_t N>
  class ObjectsPool : public MemoryPool {
  private:
    /* The buffer is declared as an array of pointers to void for two
       reasons:
       1) The objects must be properly aligned to hold a pointer as
          first field.
       2) There is no need to invoke constructors for object that are
          into the pool.*/
    void *pool_buf[(N * sizeof (T)) / sizeof (void *)];

  public:
    /**
     * @brief   ObjectsPool constructor.
     *
     * @init
     */
    ObjectsPool(void) : MemoryPool(sizeof (T), NULL) {

      loadArray(pool_buf, N);
    }
  };


Wouldn't it be nice to also provide functions like T& alloc()?

Xamusk
Posts: 81
Joined: Sat Mar 05, 2011 10:47 pm

Re: The new C++ wrapper thread

Postby Xamusk » Wed Jan 30, 2013 5:10 pm


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

Re: The new C++ wrapper thread

Postby Giovanni » Wed Jan 30, 2013 9:59 pm

Hi Mabl,

Exactly what you suggest?

Giovanni

Xamusk
Posts: 81
Joined: Sat Mar 05, 2011 10:47 pm

Re: The new C++ wrapper thread

Postby Xamusk » Wed Jan 30, 2013 11:06 pm

Sorry, I think I misinterpreted. That's specifically for the C++ wrapper.

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: The new C++ wrapper thread

Postby mabl » Sun Feb 03, 2013 1:10 pm

Hi Giovanni,

I was thinking about such an extension:

Code: Select all

  /*------------------------------------------------------------------------*
   * chibios_rt::ObjectsPool                                                *
   *------------------------------------------------------------------------*/
  /**
   * @brief   Template class encapsulating a memory pool and its elements.
   */
  template<class T, size_t N>
  class ObjectsPool : protected MemoryPool {
  private:
    /* The buffer is declared as an array of pointers to void for two
       reasons:
       1) The objects must be properly aligned to hold a pointer as
          first field.
       2) There is no need to invoke constructors for object that are
          in the pool.*/
    void *pool_buf[(N * sizeof (T)) / sizeof (void *)];

  public:
    /**
     * @brief   ObjectsPool constructor.
     *
     * @init
     */
    ObjectsPool(void) : MemoryPool(sizeof (T), NULL) {
      loadArray(pool_buf, N);
    }

    T *alloc(void) {
      const void *p = MemoryPool::alloc();

      // §5.3.4/13:
      // If the allocation function returns null, initialization shall not be
      // done, the deallocation function shall not be called, and the value of
      // the new-expression shall be null.
      //
      // So we might be able to skip this check...
      if (!p)
        return 0;

      return new(p) T();
    }
   
    void free(T *objp) {
      objp->~T();
      MemoryPool::free(objp);
    }

  };


(I class api is missing and code should move to C file, this is just a proof on concept.) Which basically hides all void* stuff behind initialized pointers of the T class - and calls all constructors and destructors.

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

Re: The new C++ wrapper thread

Postby Giovanni » Sun Feb 03, 2013 1:29 pm

I think it is a good idea, I am still not very used to C++, haven't used it for a while.

If you have other ideas or suggestions please go ahead, patches are welcome too.

Giovanni

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: The new C++ wrapper thread

Postby mabl » Sun Feb 03, 2013 2:32 pm

Ok, I'm beginning to use the wrapper for a new project and will make changes to it as I go. I'll post the patches here.

One other thing. I think the foo(void) stuff should be changed to foo() only. This code is not going to be used as C code and both are the same in C++. See also http://stackoverflow.com/questions/5103 ... -in-c-or-c


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 7 guests