Page 1 of 1

The new C++ wrapper thread

Posted: Wed Jan 02, 2013 12:32 pm
by Giovanni
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

Re: The new C++ wrapper thread

Posted: Thu Jan 03, 2013 11:03 am
by Giovanni
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

Re: The new C++ wrapper thread

Posted: Sat Jan 12, 2013 12:40 am
by Xamusk
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.

Re: The new C++ wrapper thread

Posted: Wed Jan 30, 2013 4:51 pm
by mabl
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()?

Re: The new C++ wrapper thread

Posted: Wed Jan 30, 2013 5:10 pm
by Xamusk

Re: The new C++ wrapper thread

Posted: Wed Jan 30, 2013 9:59 pm
by Giovanni
Hi Mabl,

Exactly what you suggest?

Giovanni

Re: The new C++ wrapper thread

Posted: Wed Jan 30, 2013 11:06 pm
by Xamusk
Sorry, I think I misinterpreted. That's specifically for the C++ wrapper.

Re: The new C++ wrapper thread

Posted: Sun Feb 03, 2013 1:10 pm
by mabl
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.

Re: The new C++ wrapper thread

Posted: Sun Feb 03, 2013 1:29 pm
by Giovanni
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

Re: The new C++ wrapper thread

Posted: Sun Feb 03, 2013 2:32 pm
by mabl
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