diff --git a/demo/demo_simple.cpp b/demo/demo_simple.cpp index bdb1a65..2b1e615 100644 --- a/demo/demo_simple.cpp +++ b/demo/demo_simple.cpp @@ -348,6 +348,75 @@ int main(int argc, char* argv[]) JqWait(Cancel); printf("Cancel ran %d times\n", Count.load()); } +#define COUNT0 4000 +#define COUNT1 2000 +#define COUNT_GROUP 30 + + { + std::atomic c0; + std::atomic c1; + c0 = 0; + c1 = 0; + std::atomic* pc0 = &c0; + std::atomic* pc1 = &c1; + JqHandle group1 = JqGroupBegin("group1"); + JqAdd( + "add0", + [pc0]() { + pc0->fetch_add(1); + }, + 0, COUNT0); + JqAdd( + "add1", + [pc1]() { + pc1->fetch_add(1); + }, + 0, COUNT1); + JqGroupEnd(); + + JqWait(group1); + printf("c0=%d, c1=%d\n", c0.load(), c1.load()); + JQ_ASSERT(c0 == COUNT0); + JQ_ASSERT(c1 == COUNT1); + } + { + // test groups, and groups added from other jobs work. + std::atomic c0; + std::atomic c1; + c0 = 0; + c1 = 0; + std::atomic* pc0 = &c0; + std::atomic* pc1 = &c1; + + // groups in groups. + JqHandle group1 = JqGroupBegin("group_in_group_l1"); + JqAdd( + "job0", + [pc0, pc1]() { + JqHandle group_inner = JqGroupBegin("group_in_groupl_l2"); + JqAdd( + "add0", + [pc0]() { + pc0->fetch_add(1); + }, + 0, COUNT0); + JqAdd( + "add1", + [pc1]() { + pc1->fetch_add(1); + }, + 0, COUNT1); + JqGroupEnd(); + }, + 0, COUNT_GROUP); + JqGroupEnd(); + + JqWait(group1); + + printf("c0=%d, c1=%d\n", c0.load(), c1.load()); + JQ_ASSERT(c0.load() == COUNT0 * COUNT_GROUP); + JQ_ASSERT(c1.load() == COUNT1 * COUNT_GROUP); + } // Stop Jq. JqStop(); diff --git a/jq2.cpp b/jq2.cpp index c2583f0..d6d259e 100644 --- a/jq2.cpp +++ b/jq2.cpp @@ -732,7 +732,7 @@ void JqStart(JqAttributes* pAttr) } JqState.ActiveJobs = 0; - JqState.NextHandle = 1; + JqState.NextHandle = JQ_JOB_BUFFER_SIZE + 1; JqState.Stats.Clear(); for(uint16_t i = 0; i < JQ_JOB_BUFFER_SIZE; ++i) @@ -2109,7 +2109,7 @@ JqHandle JqGroupBegin(const char* Name) { JqAttachChild(Parent, H); } - JQ_ASSERT(Job.Parent == Parent); + JQ_ASSERT(Job.Parent == (Parent % JQ_JOB_BUFFER_SIZE)); JQ_CLEAR_FUNCTION(Job.Function); Job.Queue = 0xff; JqSelfPush(H, 0);