acl/lib_acl_cpp/include/acl_cpp/stdlib/mbox.hpp

125 lines
2.2 KiB
C++
Raw Normal View History

#pragma once
#include "../acl_cpp_define.hpp"
#include <assert.h>
#include "noncopyable.hpp"
namespace acl
{
// internal functions being used
void* mbox_create(void);
void mbox_free(void*, void (*free_fn)(void*));
bool mbox_send(void*, void*);
void* mbox_read(void*, int, bool*);
size_t mbox_nsend(void*);
size_t mbox_nread(void*);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>֮<EFBFBD>Э<EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ʵ<EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> + IO ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD>ʽʵ<EFBFBD><EFBFBD>
*
* ʾ<EFBFBD><EFBFBD>:
*
* class myobj
* {
* public:
* myobj(void) {}
* ~myobj(void) {}
*
* void run(void)
* {
* printf("hello world!\r\n");
* }
* };
*
* acl::mbox<myobj> mbox;
*
* void thread_producer(void)
* {
* myobj* o = new myobj;
* mbox.push(o);
* }
*
* void thread_consumer(void)
* {
* myobj* o = mbox.pop();
* o->run();
* delete o;
* }
*/
template<typename T>
class mbox : public noncopyable
{
public:
2019-01-02 11:25:17 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param free_obj {bool} <EFBFBD><EFBFBD> tbox <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>
* δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵĶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-01-02 11:25:17 +08:00
*/
mbox(bool free_obj = true)
: free_obj_(free_obj)
{
mbox_ = mbox_create();
assert(mbox_);
}
~mbox(void)
{
2019-01-02 11:25:17 +08:00
mbox_free(mbox_, free_obj_ ? mbox_free_fn : NULL);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
bool push(T* t)
{
return mbox_send(mbox_, t);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout {int} >= 0 ʱ<EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>Զ<EFBFBD>ȴ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param success {bool*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
* @return {T*} <EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
* success <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
T* pop(int timeout = -1, bool* success = NULL)
{
return (T*) mbox_read(mbox_, timeout, success);
}
/**
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
* @return {size_t}
*/
size_t push_count(void) const
{
return mbox_nsend(mbox_);
}
/**
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
* @return {size_t}
*/
size_t pop_count(void) const
{
return mbox_nread(mbox_);
}
private:
void* mbox_;
2019-01-02 11:25:17 +08:00
bool free_obj_;
static void mbox_free_fn(void* o)
{
T* t = (T*) o;
delete t;
}
};
} // namespace acl