int dt_start(dt_unit_t *unit) { // Check input if (unit == 0) { return KNOT_EINVAL; } // Lock unit pthread_mutex_lock(&unit->_notify_mx); dt_unit_lock(unit); for (int i = 0; i < unit->size; ++i) { dthread_t *thread = unit->threads[i]; int res = dt_start_id(thread); if (res != 0) { dbg_dt("dthreads: failed to create thread '%d'.", i); dt_unit_unlock(unit); pthread_mutex_unlock(&unit->_notify_mx); return res; } dbg_dt("dthreads: [%p] %s: thread started\n", thread, __func__); } // Unlock unit dt_unit_unlock(unit); pthread_cond_broadcast(&unit->_notify); pthread_mutex_unlock(&unit->_notify_mx); return KNOT_EOK; }
/*! API: run tests. */ static int dt_tests_run(int argc, char *argv[]) { // Register service and signal handler struct sigaction sa; sa.sa_handler = interrupt_handle; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGALRM, &sa, NULL); // Interrupt /* Initialize */ srand(time(NULL)); struct timeval tv; pthread_mutex_init(&_runnable_mx, NULL); /* Test 1: Create unit */ dt_unit_t *unit = dt_test_create(2); ok(unit != 0, "dthreads: create unit (optimal size %d)", unit->size); skip(unit == 0, DT_TEST_COUNT - 1); /* Test 2: Assign a single task. */ ok(dt_test_single(unit), "dthreads: assign single task"); /* Test 3: Start tasks. */ _runnable_i = 0; ok(dt_test_start(unit), "dthreads: start single task"); /* Test 4: Wait for tasks. */ ok(dt_test_join(unit), "dthreads: join threads"); /* Test 5: Compare counter. */ int expected = _runnable_cycles * 1; cmp_ok(_runnable_i, "==", expected, "dthreads: result ok"); /* Test 6: Repurpose threads. */ _runnable_i = 0; ok(dt_test_coherent(unit), "dthreads: repurpose to coherent"); /* Test 7: Restart threads. */ ok(dt_test_start(unit), "dthreads: start coherent unit"); /* Test 8: Repurpose single thread. */ tv.tv_sec = 0; tv.tv_usec = 4000 + rand() % 1000; // 4-5ms note("waiting for %dus to let thread do some work ...", tv.tv_usec); select(0, 0, 0, 0, &tv); ok(dt_test_repurpose(unit, 0), "dthreads: repurpose on-the-fly"); /* Test 9: Cancel blocking thread. */ tv.tv_sec = 0; tv.tv_usec = (250 + rand() % 500) * 1000; // 250-750ms note("waiting for %dms to let thread pretend blocking I/O ...", tv.tv_usec / 1000); select(0, 0, 0, 0, &tv); ok(dt_test_cancel(unit, 0), "dthreads: cancel blocking thread"); /* Test 10: Wait for tasks. */ ok(dt_test_join(unit), "dthreads: join threads"); /* Test 11: Compare counter. */ int expected_lo = _runnable_cycles * (unit->size - 1); cmp_ok(_runnable_i, ">=", expected_lo, "dthreads: result %d is => %d", _runnable_i, expected_lo); /* Test 12: Compare counter #2. */ /*! \note repurpose could trigger next run of the unit if both finished */ int expected_hi = _runnable_cycles * (unit->size + unit->size - 1); cmp_ok(_runnable_i, "<=", expected_hi, "dthreads: result %d is <= %d", _runnable_i, expected_hi); /* Test 13: Reanimate dead threads. */ ok(dt_test_reanimate(unit), "dthreads: reanimate dead threads"); /* Test 14: Deinitialize */ dt_delete(&unit); ok(unit == 0, "dthreads: delete unit"); endskip; /* Test 15: Wrong values. */ unit = dt_create(-1); ok(unit == 0, "dthreads: create with negative count"); unit = dt_create_coherent(dt_optimal_size(), 0, 0); /* Test 16: NULL runnable. */ cmp_ok(dt_start(unit), "==", 0, "dthreads: start with NULL runnable"); /* Test 17: NULL operations crashing. */ int op_count = 14; int expected_min = op_count * -1; // All functions must return -1 at least int ret = 0; lives_ok( { ret += dt_activate(0); // -1 ret += dt_cancel(0); // -1 ret += dt_compact(0); // -1 dt_delete(0); // ret += dt_is_cancelled(0); // 0 ret += dt_join(0); // -1 ret += dt_repurpose(0, 0, 0); // -1 ret += dt_signalize(0, SIGALRM); // -1 ret += dt_start(0); // -1 ret += dt_start_id(0); // -1 ret += dt_stop(0); // -1 ret += dt_stop_id(0); // -1 ret += dt_unit_lock(0); // -1 ret += dt_unit_unlock(0); // -1 }, "dthreads: not crashed while executing functions on NULL context");