Skip to content

Commit

Permalink
fix incorrect assert in JqGroupBegin. Make NextHandle start at JQ_JOB…
Browse files Browse the repository at this point in the history
…_BUFFER_SIZE+1, so bugs like these occur immediately. add a test of this in demo_simple
  • Loading branch information
jonasmr committed Nov 23, 2023
1 parent 95adca8 commit e6f77ef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
69 changes: 69 additions & 0 deletions demo/demo_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> c0;
std::atomic<int> c1;
c0 = 0;
c1 = 0;
std::atomic<int>* pc0 = &c0;
std::atomic<int>* 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<int> c0;
std::atomic<int> c1;
c0 = 0;
c1 = 0;
std::atomic<int>* pc0 = &c0;
std::atomic<int>* 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();
Expand Down
4 changes: 2 additions & 2 deletions jq2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e6f77ef

Please sign in to comment.