2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2018-08-19 15:31:26 +08:00
|
|
|
|
#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*);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ʾ<EFBFBD><EFBFBD>:
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
/**
|
2019-07-28 10:31:56 +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)
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
|
|
|
|
mbox_ = mbox_create();
|
|
|
|
|
assert(mbox_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~mbox(void)
|
|
|
|
|
{
|
2019-01-02 11:25:17 +08:00
|
|
|
|
mbox_free(mbox_, free_obj_ ? mbox_free_fn : NULL);
|
2018-08-19 15:31:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2021-01-06 14:35:12 +08:00
|
|
|
|
* @param dummy {bool} Ŀǰ<EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> tbox <EFBFBD>ӿ<EFBFBD>һ<EFBFBD><EFBFBD>
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
2021-01-06 14:35:12 +08:00
|
|
|
|
bool push(T* t, bool dummy = false)
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
2021-01-06 14:35:12 +08:00
|
|
|
|
(void) dummy;
|
2018-08-19 15:31:26 +08:00
|
|
|
|
return mbox_send(mbox_, t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
|
|
|
|
T* pop(int timeout = -1, bool* success = NULL)
|
|
|
|
|
{
|
|
|
|
|
return (T*) mbox_read(mbox_, timeout, success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t push_count(void) const
|
|
|
|
|
{
|
|
|
|
|
return mbox_nsend(mbox_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
|
2018-08-19 15:31:26 +08:00
|
|
|
|
* @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_;
|
2018-08-19 15:31:26 +08:00
|
|
|
|
|
|
|
|
|
static void mbox_free_fn(void* o)
|
|
|
|
|
{
|
|
|
|
|
T* t = (T*) o;
|
|
|
|
|
delete t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|