Add blocking methods for concurrent container.
This commit is contained in:
1 parent
02d4bf0e09
commit
4691f95b73
1 file changed
+49
@@ -47,6 +47,7 @@
|
||||
\
|
||||
typedef struct M_C(name, _s) { \
|
||||
m_mutex_t lock; \
|
||||
m_cond_t there_is_data; /* condition raised when there is data */ \
|
||||
type data; \
|
||||
} concurrent_t[1]; \
|
||||
\
|
||||
@@ -60,6 +61,7 @@
|
||||
M_C(name, _init)(concurrent_t out) \
|
||||
{ \
|
||||
m_mutex_init(out->lock); \
|
||||
m_cond_init(out->there_is_data); \
|
||||
M_CALL_INIT(oplist, out->data); \
|
||||
} \
|
||||
,) \
|
||||
@@ -69,6 +71,7 @@
|
||||
M_C(name, _init_set)(concurrent_t out, concurrent_t src) \
|
||||
{ \
|
||||
m_mutex_init(out->lock); \
|
||||
m_cond_init(out->there_is_data); \
|
||||
m_mutex_lock (src->lock); \
|
||||
M_CALL_INIT_SET(oplist, out->data, src->data); \
|
||||
m_mutex_unlock(src->lock); \
|
||||
@@ -104,6 +107,7 @@
|
||||
/* No need to lock */ \
|
||||
M_CALL_CLEAR(oplist, out->data); \
|
||||
m_mutex_clear (out->lock); \
|
||||
m_cond_clear(out->there_is_data); \
|
||||
} \
|
||||
,) \
|
||||
\
|
||||
@@ -113,8 +117,10 @@
|
||||
{ \
|
||||
/* No need to lock 'src' ? */ \
|
||||
m_mutex_init (out->lock); \
|
||||
m_cond_init (out->there_is_data); \
|
||||
M_CALL_INIT_MOVE(oplist, out->data, src->data); \
|
||||
m_mutex_clear (src->lock); \
|
||||
m_cond_clear (src->there_is_data); \
|
||||
} \
|
||||
,) \
|
||||
\
|
||||
@@ -127,6 +133,7 @@
|
||||
M_CALL_MOVE(oplist, out->data, src->data); \
|
||||
m_mutex_unlock (out->lock); \
|
||||
m_mutex_clear (src->lock); \
|
||||
m_cond_clear (src->there_is_data); \
|
||||
} \
|
||||
,) \
|
||||
\
|
||||
@@ -168,6 +175,7 @@
|
||||
{ \
|
||||
m_mutex_lock (out->lock); \
|
||||
M_CALL_SET_KEY(oplist, out->data, key, data); \
|
||||
m_cond_broadcast(out->there_is_data); \
|
||||
m_mutex_unlock (out->lock); \
|
||||
} \
|
||||
,) \
|
||||
@@ -215,6 +223,7 @@
|
||||
{ \
|
||||
m_mutex_lock (out->lock); \
|
||||
M_CALL_PUSH(oplist, out->data, data); \
|
||||
m_cond_broadcast(out->there_is_data); \
|
||||
m_mutex_unlock (out->lock); \
|
||||
} \
|
||||
,) \
|
||||
@@ -235,6 +244,7 @@
|
||||
{ \
|
||||
m_mutex_lock (out->lock); \
|
||||
M_CALL_PUSH_MOVE(oplist, out->data, data); \
|
||||
m_cond_broadcast(out->there_is_data); \
|
||||
m_mutex_unlock (out->lock); \
|
||||
} \
|
||||
,) \
|
||||
@@ -312,6 +322,45 @@
|
||||
} \
|
||||
return b; \
|
||||
} \
|
||||
,) \
|
||||
\
|
||||
M_IF_METHOD(GET_KEY, oplist)( \
|
||||
static inline bool \
|
||||
M_C(name, _get_blocking)(M_GET_VALUE_TYPE oplist *out_data, concurrent_t out, M_GET_KEY_TYPE oplist key, bool blocking) \
|
||||
{ \
|
||||
bool ret = false; \
|
||||
m_mutex_lock (out->lock); \
|
||||
while (blocking) { \
|
||||
M_GET_VALUE_TYPE oplist *p = M_CALL_GET_KEY(oplist, out->data, key); \
|
||||
if (p != NULL) { \
|
||||
M_CALL_SET(M_GET_VALUE_OPLIST oplist, *out_data, *p); \
|
||||
ret = true; \
|
||||
break; \
|
||||
} \
|
||||
m_cond_wait(out->there_is_data, out->lock); \
|
||||
} \
|
||||
m_mutex_unlock (out->lock); \
|
||||
return ret; \
|
||||
} \
|
||||
,) \
|
||||
\
|
||||
M_IF_METHOD2(POP, TEST_EMPTY, oplist)( \
|
||||
static inline bool \
|
||||
M_C(name, _pop_blocking)(M_GET_SUBTYPE oplist *p, concurrent_t out, bool blocking) \
|
||||
{ \
|
||||
bool ret = false; \
|
||||
m_mutex_lock (out->lock); \
|
||||
while (blocking) { \
|
||||
if (!M_CALL_TEST_EMPTY(oplist, out->data)) { \
|
||||
M_CALL_POP(oplist, p, out->data); \
|
||||
ret = true; \
|
||||
break; \
|
||||
} \
|
||||
m_cond_wait(out->there_is_data, out->lock); \
|
||||
} \
|
||||
m_mutex_unlock (out->lock); \
|
||||
return ret; \
|
||||
} \
|
||||
,) \
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user