diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-13 18:20:57 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-14 15:52:58 -0400 |
| commit | 9f498969d9001eaf5eacdebd10ae5248e1e9e289 (patch) | |
| tree | 87082eb14277050deb655a966c9a8f8bd5d47c60 | |
| parent | 6a6eb909b88ae0e27b99f7b2921f4e9584a77f7d (diff) | |
pkev: CHECKPOINT: fixing broken tests
| -rw-r--r-- | pkev.h | 37 | ||||
| -rw-r--r-- | test/pkev.cpp | 18 |
2 files changed, 35 insertions, 20 deletions
@@ -123,6 +123,9 @@ pk_ev_inner_ev_mgr_create(uint64_t ev_count, uint64_t cb_count) assert(cb_count < 0x100); uint64_t i; struct pk_ev *ev; + // TODO refactor this. + // Consider moving this to a function or set of functions. + // Want to ensure anywhere we're doing this type of math is consistent. size_t sz = sizeof(struct pk_ev_mgr) + ((sizeof(struct pk_ev) * ev_count)) + (sizeof (struct pk_ev_cb) * ev_count * cb_count); size_t sz_ev = (sizeof(struct pk_ev_cb) * cb_count); size_t sz_evs = sizeof(struct pk_ev) * ev_count; @@ -152,10 +155,16 @@ pk_ev_inner_ev_mgr_clone(struct pk_ev_mgr *old, struct pk_ev_mgr *mgr) struct pk_ev *ev_old; struct pk_ev *ev; atomic_store(&mgr->n_ev, atomic_load(&old->n_ev)); + atomic_store(&mgr->rn_ev, atomic_load(&old->rn_ev)); + atomic_store(&mgr->rn_cb, atomic_load(&old->rn_cb)); for (i = 0; i < old->n_ev; ++i) { ev_old = &old->ev[i]; ev = &mgr->ev[i]; + ev->user_ev_data = ev_old->user_ev_data; + // TODO store size in variable so it can be debuggable. + // Also make sure this is even right. memcpy(ev->ev_cbs, ev_old->ev_cbs, sizeof(struct pk_ev_cb) * atomic_load(&ev_old->right_ev_cbs)); + atomic_store(&ev->left_ev_cbs, atomic_load(&ev_old->left_ev_cbs)); atomic_store(&ev->right_ev_cbs, atomic_load(&ev_old->right_ev_cbs)); } } @@ -235,6 +244,7 @@ pk_ev_register_cb(pk_ev_mgr_id_T evmgr, pk_ev_id_T evid, pk_ev_cb_fn *cb, void * bool found = false; uint64_t new_size, i; struct pk_ev_mgr *mgr; + struct pk_ev *ev; pk_ev_cb_id_T cb_index; mgr = pk_ev_mstr.mgrs[evmgr]; @@ -243,38 +253,39 @@ pk_ev_register_cb(pk_ev_mgr_id_T evmgr, pk_ev_id_T evid, pk_ev_cb_fn *cb, void * exit(1); } mtx_lock(&pk_ev_mstr.mtxs[evmgr]); - for (i = mgr->ev[evid].left_ev_cbs; i < mgr->ev[evid].right_ev_cbs; ++i) { - if (found == false && mgr->ev[evid].ev_cbs[i].cb != nullptr) { + ev = &mgr->ev[evid]; + for (i = ev->left_ev_cbs; i < ev->right_ev_cbs; ++i) { + if (found == false && ev->ev_cbs[i].cb != nullptr) { found = true; cb_index = i; continue; } if (found == false) continue; - if (mgr->ev[evid].ev_cbs[i].cb == nullptr) { - mgr->ev[evid].left_ev_cbs = i; + if (ev->ev_cbs[i].cb == nullptr) { + ev->left_ev_cbs = i; break; } } if (found == false) { - if (pk_ev_mstr.mgrs[evmgr]->ev[evid].right_ev_cbs == pk_ev_mstr.mgrs[evmgr]->rn_cb) { - new_size = PK_MAX(2, PK_MIN(255, pk_ev_mstr.mgrs[evmgr]->rn_cb * PK_EV_GROW_RATIO)); - if (new_size == pk_ev_mstr.mgrs[evmgr]->rn_cb) { + if (ev->right_ev_cbs > mgr->rn_cb) { + new_size = PK_MAX(2, PK_MIN(255, mgr->rn_cb * PK_EV_GROW_RATIO)); + if (new_size == mgr->rn_cb) { PK_LOG_ERR("[pkev.h] need more room, but failed to grow cb count.\n"); mtx_unlock(&pk_ev_mstr.mtxs[evmgr]); exit(1); } - mgr = pk_ev_inner_ev_mgr_create(pk_ev_mstr.mgrs[evmgr]->rn_ev, new_size); + mgr = pk_ev_inner_ev_mgr_create(mgr->rn_ev, new_size); pk_ev_inner_ev_mgr_clone(pk_ev_mstr.mgrs[evmgr], mgr); free(pk_ev_mstr.mgrs[evmgr]); pk_ev_mstr.mgrs[evmgr] = mgr; } - cb_index = pk_ev_mstr.mgrs[evmgr]->ev[evid].right_ev_cbs++; - if (cb_index == pk_ev_mstr.mgrs[evmgr]->ev[evid].left_ev_cbs) { - pk_ev_mstr.mgrs[evmgr]->ev[evid].left_ev_cbs++; + cb_index = ev->right_ev_cbs++; + if (cb_index == ev->left_ev_cbs) { + ev->left_ev_cbs++; } } - pk_ev_mstr.mgrs[evmgr]->ev[evid].ev_cbs[cb_index].cb = cb; - pk_ev_mstr.mgrs[evmgr]->ev[evid].ev_cbs[cb_index].user_cb_data = user_cb_data; + ev->ev_cbs[cb_index].cb = cb; + ev->ev_cbs[cb_index].user_cb_data = user_cb_data; mtx_unlock(&pk_ev_mstr.mtxs[evmgr]); return cb_index; } diff --git a/test/pkev.cpp b/test/pkev.cpp index 6258e02..a34b7d5 100644 --- a/test/pkev.cpp +++ b/test/pkev.cpp @@ -27,8 +27,8 @@ exit(int code) struct ev { pk_ev_mgr_id_T evmgr; pk_ev_id_T evid; - int count; - bool handled; + atomic_int count; + atomic_bool handled; }; struct ev ev_one = {}; @@ -58,7 +58,7 @@ typedef struct cb_data { void stress_cb(void *, void *, void *user_data) { cb_data *data = reinterpret_cast<cb_data*>(user_data); data->ev[data->i].handled = true; - data->ev[data->i].count += 1; + data->ev[data->i].count++; } void invoke_packged_task(void *, void *, void* ptr) { @@ -134,7 +134,7 @@ int main(int argc, char *argv[]) bool any_false = false; for (i = 0; i < ev_count; ++i) { - PK_LOGV_INF("%s: ev# %.2zu: %s, called count: %i\n", __FILE__, i, evs[i].handled ? "true" : "false", evs[i].count); + PK_LOGV_INF("%s: ev# %.2zu: %s, called count: %i\n", __FILE__, i, evs[i].handled ? "true" : "false", atomic_load(&evs[i].count)); any_false = any_false || !evs[i].handled; } pk_ev_teardown(); @@ -158,19 +158,23 @@ int main(int argc, char *argv[]) } for (i = 0; i < ev_count; ++i) { - cb_data *d = new cb_data{}; - d->ev = evs; + cb_data *d = new cb_data; d->i = i; + d->ev = &evs[0]; pk_ev_emit(evs[i].evmgr, evs[i].evid, d); } bool any_false = false; uint64_t valid_count = 0; + uint64_t partial_valid_count = 0; for (i = 0; i < ev_count; ++i) { if (evs[i].handled == true && evs[i].count == 0xFF) valid_count += 1; + if (evs[i].handled == true && evs[i].count != 0xFF) { + partial_valid_count += 1; + } any_false = any_false || !evs[i].handled; } - PK_LOGV_INF("%s: #valid: %lu, called count: %i\n", __FILE__, valid_count, evs[0].count); + PK_LOGV_INF("%s: #valid: %lu, #partial_valid: %lu, called count: %i\n", __FILE__, valid_count, partial_valid_count, atomic_load(&evs[0].count)); pk_ev_teardown(); fflush(stdout); fflush(stderr); |
