Skip to content

Commit c77b824

Browse files
authored
Merge pull request #11782 from kjbracey-arm/equeue_shift
equeue: avoid non-standard bit shifts
2 parents e4164be + 3d7bff6 commit c77b824

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

events/source/equeue.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ static inline int equeue_tickdiff(unsigned a, unsigned b)
3636
static inline int equeue_clampdiff(unsigned a, unsigned b)
3737
{
3838
int diff = equeue_tickdiff(a, b);
39-
return ~(diff >> (8 * sizeof(int) -1)) & diff;
39+
return diff > 0 ? diff : 0;
4040
}
4141

4242
// Increment the unique id in an event, hiding the event from cancel
4343
static inline void equeue_incid(equeue_t *q, struct equeue_event *e)
4444
{
4545
e->id += 1;
46-
if ((e->id << q->npw2) == 0) {
46+
if (((unsigned)e->id << q->npw2) == 0) {
4747
e->id = 1;
4848
}
4949
}
@@ -280,7 +280,7 @@ void equeue_enqueue(equeue_t *q, struct equeue_event *e, unsigned tick)
280280
static int equeue_event_id(equeue_t *q, struct equeue_event *e)
281281
{
282282
// setup event and hash local id with buffer offset for unique id
283-
return ((e->id << q->npw2) | ((unsigned char *)e - q->buffer));
283+
return ((unsigned)e->id << q->npw2) | ((unsigned char *)e - q->buffer);
284284
}
285285

286286
static struct equeue_event *equeue_unqueue_by_address(equeue_t *q, struct equeue_event *e)
@@ -319,10 +319,10 @@ static struct equeue_event *equeue_unqueue_by_id(equeue_t *q, int id)
319319
{
320320
// decode event from unique id and check that the local id matches
321321
struct equeue_event *e = (struct equeue_event *)
322-
&q->buffer[id & ((1 << q->npw2) - 1)];
322+
&q->buffer[id & ((1u << q->npw2) - 1u)];
323323

324324
equeue_mutex_lock(&q->queuelock);
325-
if (e->id != id >> q->npw2) {
325+
if (e->id != (unsigned)id >> q->npw2) {
326326
equeue_mutex_unlock(&q->queuelock);
327327
return 0;
328328
}
@@ -447,10 +447,10 @@ int equeue_timeleft(equeue_t *q, int id)
447447

448448
// decode event from unique id and check that the local id matches
449449
struct equeue_event *e = (struct equeue_event *)
450-
&q->buffer[id & ((1 << q->npw2) - 1)];
450+
&q->buffer[id & ((1u << q->npw2) - 1u)];
451451

452452
equeue_mutex_lock(&q->queuelock);
453-
if (e->id == id >> q->npw2) {
453+
if (e->id == (unsigned)id >> q->npw2) {
454454
ret = equeue_clampdiff(e->target, equeue_tick());
455455
}
456456
equeue_mutex_unlock(&q->queuelock);

events/source/tests/tests.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void cancel_test(int N)
297297
}
298298

299299
for (int i = N - 1; i >= 0; i--) {
300-
equeue_cancel(&q, ids[i]);
300+
test_assert(equeue_cancel(&q, ids[i]));
301301
}
302302

303303
free(ids);
@@ -317,13 +317,13 @@ void cancel_inflight_test(void)
317317
bool touched = false;
318318

319319
int id = equeue_call(&q, simple_func, &touched);
320-
equeue_cancel(&q, id);
320+
test_assert(equeue_cancel(&q, id));
321321

322322
equeue_dispatch(&q, 0);
323323
test_assert(!touched);
324324

325325
id = equeue_call(&q, simple_func, &touched);
326-
equeue_cancel(&q, id);
326+
test_assert(equeue_cancel(&q, id));
327327

328328
equeue_dispatch(&q, 0);
329329
test_assert(!touched);
@@ -352,19 +352,19 @@ void cancel_unnecessarily_test(void)
352352

353353
int id = equeue_call(&q, pass_func, 0);
354354
for (int i = 0; i < 5; i++) {
355-
equeue_cancel(&q, id);
355+
test_assert(equeue_cancel(&q, id) == (i == 0));
356356
}
357357

358358
id = equeue_call(&q, pass_func, 0);
359359
equeue_dispatch(&q, 0);
360360
for (int i = 0; i < 5; i++) {
361-
equeue_cancel(&q, id);
361+
test_assert(!equeue_cancel(&q, id));
362362
}
363363

364364
bool touched = false;
365365
equeue_call(&q, simple_func, &touched);
366366
for (int i = 0; i < 5; i++) {
367-
equeue_cancel(&q, id);
367+
test_assert(!equeue_cancel(&q, id));
368368
}
369369

370370
equeue_dispatch(&q, 0);
@@ -595,8 +595,8 @@ void chain_test(void)
595595
id2 = equeue_call_in(&q2, 5, simple_func, &touched);
596596
test_assert(id1 && id2);
597597

598-
equeue_cancel(&q1, id1);
599-
equeue_cancel(&q2, id2);
598+
test_assert(equeue_cancel(&q1, id1));
599+
test_assert(equeue_cancel(&q2, id2));
600600

601601
id1 = equeue_call_in(&q1, 10, simple_func, &touched);
602602
id2 = equeue_call_in(&q2, 10, simple_func, &touched);
@@ -768,7 +768,7 @@ void break_request_cleared_on_timeout(void)
768768
equeue_dispatch(&q, 10);
769769
test_assert(pq.p == 1);
770770

771-
equeue_cancel(&q, id);
771+
test_assert(equeue_cancel(&q, id));
772772

773773
int count = 0;
774774
equeue_call_every(&q, 10, simple_func, &count);
@@ -796,9 +796,9 @@ void sibling_test(void)
796796
test_assert(!s->next);
797797
}
798798
}
799-
equeue_cancel(&q, id0);
800-
equeue_cancel(&q, id1);
801-
equeue_cancel(&q, id2);
799+
test_assert(equeue_cancel(&q, id0));
800+
test_assert(equeue_cancel(&q, id1));
801+
test_assert(equeue_cancel(&q, id2));
802802
equeue_destroy(&q);
803803
}
804804

@@ -829,7 +829,7 @@ void user_allocated_event_test()
829829
equeue_post_user_allocated(&q, simple_func, &e3.e);
830830
equeue_post_user_allocated(&q, simple_func, &e4.e);
831831
equeue_post_user_allocated(&q, simple_func, &e5.e);
832-
equeue_cancel_user_allocated(&q, &e3.e);
832+
test_assert(equeue_cancel_user_allocated(&q, &e3.e));
833833

834834
equeue_dispatch(&q, 1);
835835

@@ -852,6 +852,21 @@ void user_allocated_event_test()
852852
equeue_destroy(&q);
853853
}
854854

855+
void id_cycle()
856+
{
857+
equeue_t q;
858+
int err = equeue_create(&q, 10000000);
859+
test_assert(!err);
860+
861+
for (int i = 0; i < 300; i++) {
862+
int id = equeue_call(&q, pass_func, 0);
863+
test_assert(id != 0);
864+
test_assert(equeue_cancel(&q, id));
865+
}
866+
867+
equeue_destroy(&q);
868+
}
869+
855870
int main()
856871
{
857872
printf("beginning tests...\n");
@@ -881,6 +896,7 @@ int main()
881896
test_run(break_request_cleared_on_timeout);
882897
test_run(sibling_test);
883898
test_run(user_allocated_event_test);
899+
test_run(id_cycle);
884900
printf("done!\n");
885901
return test_failure;
886902
}

0 commit comments

Comments
 (0)