2017-04-07 20:13:08 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
|
#include "noncopyable.hpp"
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
2017-09-01 18:59:42 +08:00
|
|
|
|
template<typename T>
|
|
|
|
|
class mbox
|
2017-04-07 20:13:08 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2017-09-01 23:14:20 +08:00
|
|
|
|
mbox(void)
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
mbox_ = mbox_create();
|
2017-09-01 23:14:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~mbox(void)
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
mbox_free(mbox_, mbox_free_fn);
|
2017-09-01 23:14:20 +08:00
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-09-01 18:59:42 +08:00
|
|
|
|
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-04-07 20:13:08 +08:00
|
|
|
|
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
*/
|
2017-09-01 18:59:42 +08:00
|
|
|
|
bool push(T* t)
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
return mbox_send(mbox_, t);
|
2017-09-01 18:59:42 +08:00
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-29 11:32:07 +08:00
|
|
|
|
* @param timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 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>
|
2017-04-07 20:13:08 +08:00
|
|
|
|
* @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>
|
2017-09-01 18:59:42 +08:00
|
|
|
|
* @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>
|
2017-04-07 20:13:08 +08:00
|
|
|
|
* success <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|
|
|
|
*/
|
2017-09-01 18:59:42 +08:00
|
|
|
|
T* pop(int timeout = 0, bool* success = NULL)
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
return (T*) mbox_read(mbox_, timeout, success);
|
2017-09-01 18:59:42 +08:00
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
|
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
2017-09-01 23:14:20 +08:00
|
|
|
|
size_t push_count(void) const
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
return mbox_nsend(mbox_);
|
2017-09-01 23:14:20 +08:00
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
|
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
2017-09-01 23:14:20 +08:00
|
|
|
|
size_t pop_count(void) const
|
|
|
|
|
{
|
2017-09-02 09:42:11 +08:00
|
|
|
|
return mbox_nread(mbox_);
|
2017-09-01 23:14:20 +08:00
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2017-09-02 09:42:11 +08:00
|
|
|
|
void* mbox_;
|
2017-09-01 18:59:42 +08:00
|
|
|
|
|
2017-09-01 23:14:20 +08:00
|
|
|
|
static void mbox_free_fn(void* o)
|
|
|
|
|
{
|
|
|
|
|
T* t = (T*) o;
|
|
|
|
|
delete t;
|
|
|
|
|
}
|
2017-04-07 20:13:08 +08:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-02 09:42:11 +08:00
|
|
|
|
// 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*);
|
|
|
|
|
|
2017-04-07 20:13:08 +08:00
|
|
|
|
} // namespace acl
|