예제 #1
0
/**
 * @see pomp_timer_destroy.
 */
static int pomp_timer_win32_destroy(struct pomp_timer *timer)
{
	struct pomp_fd *pfd = NULL;
	POMP_RETURN_ERR_IF_FAILED(timer != NULL, -EINVAL);

	/* Free resources */
	if (timer->htimer != NULL) {
		/* Wait for cancellation */
		DeleteTimerQueueTimer(NULL, timer->htimer,
				INVALID_HANDLE_VALUE);
	}
	if (timer->hevt != NULL) {
		pfd = pomp_loop_win32_find_pfd_by_hevt(
				timer->loop, timer->hevt);
		if (pfd == NULL) {
			POMP_LOGW("hevt %p not found in loop %p",
					timer->hevt, timer->loop);
		} else {
			pomp_loop_remove_pfd(timer->loop, pfd);
			free(pfd);
		}
		CloseHandle(timer->hevt);
	}
	free(timer);
	return 0;
}
예제 #2
0
static void test_loop(void)
{
	int res = 0;
	HANDLE htimer1 = NULL, htimer2 = NULL, htimer3 = NULL;
	struct test_data data;
	struct pomp_loop *loop = NULL;
	struct pomp_fd *pfd = NULL;

	memset(&data, 0, sizeof(data));

	/* Create loop */
	loop = pomp_loop_new();
	CU_ASSERT_PTR_NOT_NULL_FATAL(loop);

	/* Create timers for testing */
	htimer1 = setup_timer_win32(100, 500);
	CU_ASSERT_PTR_NOT_NULL_FATAL(htimer1);
	htimer2 = setup_timer_win32(50, 500);
	CU_ASSERT_PTR_NOT_NULL_FATAL(htimer2);
	htimer3 = setup_timer_win32(150, 500);
	CU_ASSERT_PTR_NOT_NULL_FATAL(htimer3);

	/* Add timer in loop */
	pfd = pomp_loop_win32_add_pfd_with_hevt(loop, htimer1, &timer_win32_cb, &data);
	CU_ASSERT_PTR_NOT_NULL(pfd);

	/* Add 2nd and 3rd timer in loop */
	pfd = pomp_loop_win32_add_pfd_with_hevt(loop, htimer2, &timer_win32_cb, &data);
	CU_ASSERT_PTR_NOT_NULL(pfd);
	pfd = pomp_loop_win32_add_pfd_with_hevt(loop, htimer3, &timer_win32_cb, &data);
	CU_ASSERT_PTR_NOT_NULL(pfd);

	/* Run loop with different timeout first one should have all timers) */
	res = pomp_loop_wait_and_process(loop, 500);
	CU_ASSERT_EQUAL(res, 0);
	res = pomp_loop_wait_and_process(loop, 0);
	CU_ASSERT_TRUE(res == -ETIMEDOUT || res == 0);
	res = pomp_loop_wait_and_process(loop, -1);
	CU_ASSERT_EQUAL(res, 0);

	/* Invalid run (NULL param) */
	res = pomp_loop_wait_and_process(NULL, 0);
	CU_ASSERT_EQUAL(res, -EINVAL);

	/* Invalid destroy (NULL param) */
	res = pomp_loop_destroy(NULL);
	CU_ASSERT_EQUAL(res, -EINVAL);

	/* Invalid destroy (busy) */
	res = pomp_loop_destroy(loop);
	CU_ASSERT_EQUAL(res, -EBUSY);

	/* Remove timers */
	pfd = pomp_loop_win32_find_pfd_by_hevt(loop, htimer1);
	CU_ASSERT_PTR_NOT_NULL(pfd);
	res = pomp_loop_remove_pfd(loop, pfd);
	CU_ASSERT_EQUAL(res, 0);
	free(pfd);
	pfd = pomp_loop_win32_find_pfd_by_hevt(loop, htimer2);
	CU_ASSERT_PTR_NOT_NULL(pfd);
	res = pomp_loop_remove_pfd(loop, pfd);
	CU_ASSERT_EQUAL(res, 0);
	free(pfd);
	pfd = pomp_loop_win32_find_pfd_by_hevt(loop, htimer3);
	CU_ASSERT_PTR_NOT_NULL(pfd);
	res = pomp_loop_remove_pfd(loop, pfd);
	CU_ASSERT_EQUAL(res, 0);
	free(pfd);

	/* Close timers */
	res = CloseHandle(htimer1);
	CU_ASSERT_EQUAL(res, 1);
	res = CloseHandle(htimer2);
	CU_ASSERT_EQUAL(res, 1);
	res = CloseHandle(htimer3);
	CU_ASSERT_EQUAL(res, 1);

	/* Destroy loop */
	res = pomp_loop_destroy(loop);
	CU_ASSERT_EQUAL(res, 0);
}