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>
|
2022-11-21 20:27:09 +08:00
|
|
|
|
#include "box.hpp"
|
2018-08-19 15:31:26 +08:00
|
|
|
|
|
2022-11-21 20:27:09 +08:00
|
|
|
|
namespace acl {
|
2018-08-19 15:31:26 +08:00
|
|
|
|
|
|
|
|
|
// internal functions being used
|
2021-01-10 13:51:49 +08:00
|
|
|
|
void* mbox_create(bool mpsc);
|
2018-08-19 15:31:26 +08:00
|
|
|
|
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
|
|
|
|
*
|
2022-11-21 20:27:09 +08:00
|
|
|
|
* class myobj {
|
2018-08-19 15:31:26 +08:00
|
|
|
|
* 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>
|
2022-11-21 20:27:09 +08:00
|
|
|
|
class mbox : public box<T> {
|
2018-08-19 15:31:26 +08:00
|
|
|
|
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>
|
2021-01-10 13:51:49 +08:00
|
|
|
|
* @param mpsc {bool} <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
|
|
|
|
*/
|
2021-01-10 13:51:49 +08:00
|
|
|
|
mbox(bool free_obj = true, bool mpsc = true)
|
2019-01-02 11:25:17 +08:00
|
|
|
|
: free_obj_(free_obj)
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
2021-01-10 13:51:49 +08:00
|
|
|
|
mbox_ = mbox_create(mpsc);
|
2018-08-19 15:31:26 +08:00
|
|
|
|
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>
|
2022-11-21 20:27:09 +08:00
|
|
|
|
* @override
|
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>
|
2022-11-21 20:27:09 +08:00
|
|
|
|
* @param found {bool*} <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>
|
|
|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D><EFBFBD><EFBFBD> wait_ms <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* Ϊ -1 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <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> wait_ms <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD> found <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ true <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD>ж<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @override
|
2018-08-19 15:31:26 +08:00
|
|
|
|
*/
|
2022-11-21 20:27:09 +08:00
|
|
|
|
T* pop(int timeout = -1, bool* found = NULL)
|
2018-08-19 15:31:26 +08:00
|
|
|
|
{
|
2022-11-21 20:27:09 +08:00
|
|
|
|
return (T*) mbox_read(mbox_, timeout, found);
|
2018-08-19 15:31:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 22:58:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* mbox û<EFBFBD>п<EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
* @return {bool}
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
bool has_null(void) const {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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>Ϣ<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
|