Add macros SHARED_PTR_DEF_AS, SHARED_PTR_RELAXED_DEF_AS, SHARED_RESOURCE_DEF_AS
This commit is contained in:
1 parent
379e0be211
commit
a4cb6e3681
2 files changed
+85
-4
No files matched your search
+39
-3
@@ -49,6 +49,17 @@ M_BEGIN_PROTECTED_CODE
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
/* Define shared pointer and its function
|
||||
as the given name name_t
|
||||
USAGE: SHARED_PTR_DEF_AS(name, name_t, type, [, oplist]) */
|
||||
#define SHARED_PTR_DEF_AS(name, name_t, ...) \
|
||||
M_BEGIN_PROTECTED_CODE \
|
||||
SHAREDI_PTR_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \
|
||||
((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), SHAREDI_ATOMIC_OPLIST, name_t ), \
|
||||
(name, __VA_ARGS__ , SHAREDI_ATOMIC_OPLIST, name_t ))) \
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
/* Define relaxed shared pointer and its function (thread unsafe).
|
||||
USAGE: SHARED_PTR_RELAXED_DEF(name, type, [, oplist]) */
|
||||
#define SHARED_PTR_RELAXED_DEF(name, ...) \
|
||||
@@ -59,13 +70,38 @@ M_BEGIN_PROTECTED_CODE
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
/* Define relaxed shared pointer and its function (thread unsafe)
|
||||
as the given name name_t
|
||||
USAGE: SHARED_PTR_RELAXED_DEF(name, type, [, oplist]) */
|
||||
#define SHARED_PTR_RELAXED_DEF_AS(name, name_t, ...) \
|
||||
M_BEGIN_PROTECTED_CODE \
|
||||
SHAREDI_PTR_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \
|
||||
((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), SHAREDI_INTEGER_OPLIST, name_t ), \
|
||||
(name, __VA_ARGS__, SHAREDI_INTEGER_OPLIST, name_t ))) \
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
/* Define shared resource and its function.
|
||||
This is a bounded pool of resource shared by multiple owners.
|
||||
USAGE: SHARED_RESOURCE_DEF(name, type, [, oplist]) */
|
||||
#define SHARED_RESOURCE_DEF(name, ...) \
|
||||
#define SHARED_RESOURCE_DEF(name, ...) \
|
||||
M_BEGIN_PROTECTED_CODE \
|
||||
SHAREDI_RESOURCE_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \
|
||||
((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__), M_C(name, _t), M_C(name, _it_t) ), \
|
||||
(name, __VA_ARGS__, M_C(name, _t), M_C(name, _it_t) )))
|
||||
((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), M_C(name, _t), M_C(name, _it_t) ), \
|
||||
(name, __VA_ARGS__, M_C(name, _t), M_C(name, _it_t) ))) \
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
/* Define shared resource and its function
|
||||
as the given name named_t and the iterator it_t
|
||||
This is a bounded pool of resource shared by multiple owners.
|
||||
USAGE: SHARED_RESOURCE_DEF_AS(name, name_t, it_t, type, [, oplist]) */
|
||||
#define SHARED_RESOURCE_DEF_AS(name, name_t, it_t, ...) \
|
||||
M_BEGIN_PROTECTED_CODE \
|
||||
SHAREDI_RESOURCE_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \
|
||||
((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t ), \
|
||||
(name, __VA_ARGS__, name_t, it_t ))) \
|
||||
M_END_PROTECTED_CODE
|
||||
|
||||
|
||||
|
||||
|
||||
+46
-1
@@ -30,9 +30,14 @@ START_COVERAGE
|
||||
SHARED_PTR_DEF(shared_int, int)
|
||||
SHARED_RESOURCE_DEF(shared_ressource, testobj_t, TESTOBJ_OPLIST)
|
||||
END_COVERAGE
|
||||
|
||||
SHARED_PTR_DEF(shared_mpz, testobj_t, TESTOBJ_OPLIST)
|
||||
SHARED_PTR_RELAXED_DEF(shared_relaxed_int, int)
|
||||
|
||||
SHARED_PTR_DEF_AS(SharedDouble, SharedDouble, double)
|
||||
SHARED_PTR_RELAXED_DEF_AS(SharedDouble2, SharedDouble2, double, M_DEFAULT_OPLIST)
|
||||
SHARED_RESOURCE_DEF_AS(SharedRessource, SharedRessource, SharedRessourceIt, double)
|
||||
|
||||
static int f(const shared_int_t p)
|
||||
{
|
||||
return *shared_int_cref(p);
|
||||
@@ -177,12 +182,52 @@ static void test_ressource(int n)
|
||||
shared_ressource_clear(ressource);
|
||||
}
|
||||
|
||||
static void test_double(void){
|
||||
SharedDouble p;
|
||||
SharedDouble_init_new(p);
|
||||
assert(*SharedDouble_ref(p) == 0.0);
|
||||
*SharedDouble_ref(p) = 5678.0;
|
||||
SharedDouble q;
|
||||
SharedDouble_init_set(q, p);
|
||||
assert(*SharedDouble_ref(q) == 5678.0);
|
||||
SharedDouble_clear(p);
|
||||
SharedDouble_clear(q);
|
||||
}
|
||||
|
||||
static void test_double2(void){
|
||||
SharedDouble2 p;
|
||||
SharedDouble2_init_new(p);
|
||||
assert(*SharedDouble2_ref(p) == 0.0);
|
||||
*SharedDouble2_ref(p) = 5678.0;
|
||||
SharedDouble2 q;
|
||||
SharedDouble2_init_set(q, p);
|
||||
assert(*SharedDouble2_ref(q) == 5678.0);
|
||||
SharedDouble2_clear(p);
|
||||
SharedDouble2_clear(q);
|
||||
}
|
||||
|
||||
static void test_double_res(void){
|
||||
SharedRessource r;
|
||||
SharedRessource_init(r, 4);
|
||||
SharedRessourceIt i;
|
||||
SharedRessource_it(i, r);
|
||||
assert(*SharedRessource_ref(i) == 0);
|
||||
*SharedRessource_ref(i) = 3456.0;
|
||||
SharedRessource_end(i, r);
|
||||
SharedRessource_it(i, r);
|
||||
assert(*SharedRessource_ref(i) == 3456.0);
|
||||
SharedRessource_clear(r);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
for(int i = 1; i < MAX_RESSOURCE; i++)
|
||||
for(int i = 1; i < MAX_RESSOURCE; i++) {
|
||||
test_ressource(i);
|
||||
}
|
||||
test_double();
|
||||
test_double2();
|
||||
test_double_res();
|
||||
exit(0);
|
||||
}
|
||||
Reference in new issue
Block a user