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
The new C++ wrapper thread
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: The new C++ wrapper thread
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
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
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
Hi Giovanni,
Wouldn't it be nice to also provide functions like T& alloc()?
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
alloc() is (sort of) supported.
Check:
http://chibios.sourceforge.net/html/group__heaps.html
http://www.chibios.org/dokuwiki/doku.ph ... age_memory
Check:
http://chibios.sourceforge.net/html/group__heaps.html
http://www.chibios.org/dokuwiki/doku.ph ... age_memory
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: The new C++ wrapper thread
Sorry, I think I misinterpreted. That's specifically for the C++ wrapper.
Re: The new C++ wrapper thread
Hi Giovanni,
I was thinking about such an extension:
(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.
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.
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: The new C++ wrapper thread
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
If you have other ideas or suggestions please go ahead, patches are welcome too.
Giovanni
Re: The new C++ wrapper thread
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
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