Beispiel #1
0
isc_result_t
dns_timer_setidle(isc_timer_t *timer, unsigned int maxtime,
		  unsigned int idletime, isc_boolean_t purge)
{
	isc_result_t result;
	isc_interval_t maxinterval, idleinterval;
	isc_time_t expires;

	/* Compute the time of expiry. */
	isc_interval_set(&maxinterval, maxtime, 0);
	CHECK(isc_time_nowplusinterval(&expires, &maxinterval));

	/*
	 * Compute the idle interval, and add a spare nanosecond to
	 * work around the silly limitation of the ISC timer interface
	 * that you cannot specify an idle interval of zero.
	 */
	isc_interval_set(&idleinterval, idletime, 1);

	CHECK(isc_timer_reset(timer, isc_timertype_once,
			      &expires, &idleinterval,
			      purge));
 failure:
	return (result);
}
Beispiel #2
0
static void tick (isc_task_t * task, isc_event_t * event)
{
    char *name = event->ev_arg;

    INSIST (event->ev_type == ISC_TIMEREVENT_TICK);

    printf ("task %s (%p) tick\n", name, task);

    tick_count++;
    if (ti3 != NULL && tick_count % 3 == 0)
        isc_timer_touch (ti3);

    if (ti3 != NULL && tick_count == 7)
    {
        isc_time_t expires;

        isc_interval_t interval;

        isc_interval_set (&interval, 5, 0);
        (void) isc_time_nowplusinterval (&expires, &interval);
        isc_interval_set (&interval, 4, 0);
        printf ("*** resetting ti3 ***\n");
        RUNTIME_CHECK (isc_timer_reset (ti3, isc_timertype_once, &expires, &interval, ISC_TRUE) == ISC_R_SUCCESS);
    }

    isc_event_free (&event);
}
Beispiel #3
0
int
main(int argc, char *argv[]) {
	isc_interval_t linterval;
	int i;

	UNUSED(argc);
	UNUSED(argv);

	isc__mem_register();
	isc__task_register();
	isc__timer_register();
	isc__socket_register();
	isc_app_start();
	isc_interval_set(&linterval, 1, 0);

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_taskmgr_create(mctx, 3, 0, &taskmgr) ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_create(taskmgr, 0, &g_task) ==
		      ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_ratelimiter_create(mctx, timermgr, g_task,
					     &rlim) == ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_ratelimiter_setinterval(rlim, &linterval) ==
		      ISC_R_SUCCESS);

	for (i = 0; i < NEVENTS; i++) {
		isc_interval_t uinterval;
		int ms = schedule[i].milliseconds;
		isc_interval_set(&uinterval,  ms / 1000,
				 (ms % 1000) * 1000000);
		timers[i] = NULL;
		RUNTIME_CHECK(isc_timer_create(timermgr,
					       isc_timertype_once, NULL,
					       &uinterval,
					       g_task, schedule[i].fun, NULL,
					       &timers[i]) == ISC_R_SUCCESS);
	}

	isc_app_run();

	isc_task_destroy(&g_task);

	isc_ratelimiter_detach(&rlim);

	isc_timermgr_destroy(&timermgr);
	isc_taskmgr_destroy(&taskmgr);

	isc_mem_stats(mctx, stdout);

	isc_app_finish();
	return (0);
}
Beispiel #4
0
static t_info *
new_task(isc_mem_t *mctx, const char *name) {
	t_info *ti;
	isc_time_t expires;
	isc_interval_t interval;

	RUNTIME_CHECK(task_count < MAX_TASKS);
	ti = &tasks[task_count];
	ti->mctx = mctx;
	ti->task = NULL;
	ti->timer = NULL;
	ti->ticks = 0;
	if (name != NULL) {
		INSIST(strlen(name) < sizeof(ti->name));
		strcpy(ti->name, name);
	} else
		sprintf(ti->name, "%d", task_count);
	RUNTIME_CHECK(isc_task_create(task_manager, 0, &ti->task) ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(ti->task, shutdown_action, ti) ==
		      ISC_R_SUCCESS);

	isc_time_settoepoch(&expires);
	isc_interval_set(&interval, 1, 0);
	RUNTIME_CHECK(isc_timer_create(timer_manager, isc_timertype_ticker,
				       &expires, &interval, ti->task,
				       tick, ti, &ti->timer) ==
		      ISC_R_SUCCESS);

	task_count++;

	return (ti);
}
Beispiel #5
0
static void
t5_tick_event(isc_task_t *task, isc_event_t *event) {
	isc_result_t	isc_result;
	isc_time_t	expires;
	isc_interval_t	interval;

	UNUSED(task);

	++T5_eventcnt;
	t_info("t5_tick_event %d\n", T5_eventcnt);

	/*
	 * On the first tick, purge all remaining tick events
	 * and then shut down the task.
	 */
	if (T5_eventcnt == 1) {
		isc_time_settoepoch(&expires);
		isc_interval_set(&interval, T5_SECONDS, 0);
		isc_result = isc_timer_reset(T5_tickertimer,
					     isc_timertype_ticker, &expires,
					     &interval, ISC_TRUE);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_timer_reset failed %s\n",
			       isc_result_totext(isc_result));
			++T5_nfails;
		}
		isc_task_shutdown(task);
	}
	isc_event_free(&event);
}
Beispiel #6
0
static void
t4(void) {
	int		result;
	isc_time_t	expires;
	isc_interval_t	interval;

	t_assert("isc_timer_reset", 4, T_REQUIRED, "%s", a4);

	if (threaded) {
		Tx_nfails = 0;
		Tx_nprobs = 0;
		Tx_nevents = 3;
		Tx_seconds = T4_SECONDS;
		Tx_nanoseconds = T4_NANOSECONDS;

		isc_interval_set(&interval, T4_SECONDS, T4_NANOSECONDS);
		isc_time_settoepoch(&expires);
		t_timers_x(isc_timertype_ticker, &expires, &interval, t4_te);

		result = T_UNRESOLVED;

		if ((Tx_nfails == 0) && (Tx_nprobs == 0))
			result = T_PASS;
		else if (Tx_nfails)
			result = T_FAIL;

		t_result(result);
	} else
		require_threads();
}
Beispiel #7
0
static void
t3(void) {
	int		result;
	int		isc_result;
	isc_time_t	expires;
	isc_interval_t	interval;

	t_assert("isc_timer_create", 3, T_REQUIRED, "%s", a3);

	if (threaded) {
		Tx_nfails	= 0;
		Tx_nprobs	= 0;
		Tx_nevents	= 1;
		Tx_seconds	= T3_SECONDS;
		Tx_nanoseconds	= T3_NANOSECONDS;

		isc_interval_set(&interval, Tx_seconds + 1, Tx_nanoseconds);

		isc_result = isc_time_nowplusinterval(&expires, &interval);
		if (isc_result == ISC_R_SUCCESS) {
			isc_interval_set(&interval, Tx_seconds,
					 Tx_nanoseconds);
			t_timers_x(isc_timertype_once, &expires, &interval,
				   t3_te);
		} else {
			t_info("isc_time_nowplusinterval failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}

		result = T_UNRESOLVED;

		if ((Tx_nfails == 0) && (Tx_nprobs == 0))
			result = T_PASS;
		else if (Tx_nfails)
			result = T_FAIL;

		t_result(result);
	} else
		require_threads();
}
Beispiel #8
0
isc_result_t
isc_ratelimiter_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
		       isc_task_t *task, isc_ratelimiter_t **ratelimiterp)
{
	isc_result_t result;
	isc_ratelimiter_t *rl;
	INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);

	rl = isc_mem_get(mctx, sizeof(*rl));
	if (rl == NULL)
		return ISC_R_NOMEMORY;
	rl->mctx = mctx;
	rl->refs = 1;
	rl->task = task;
	isc_interval_set(&rl->interval, 0, 0);
	rl->timer = NULL;
	rl->pertic = 1;
	rl->state = isc_ratelimiter_idle;
	ISC_LIST_INIT(rl->pending);

	result = isc_mutex_init(&rl->lock);
	if (result != ISC_R_SUCCESS)
		goto free_mem;

	result = isc_timer_create(timermgr, isc_timertype_inactive,
				  NULL, NULL, rl->task, ratelimiter_tick,
				  rl, &rl->timer);
	if (result != ISC_R_SUCCESS)
		goto free_mutex;

	/*
	 * Increment the reference count to indicate that we may
	 * (soon) have events outstanding.
	 */
	rl->refs++;

	ISC_EVENT_INIT(&rl->shutdownevent,
		       sizeof(isc_event_t),
		       0, NULL, ISC_RATELIMITEREVENT_SHUTDOWN,
		       ratelimiter_shutdowncomplete, rl, rl, NULL, NULL);

	*ratelimiterp = rl;
	return (ISC_R_SUCCESS);

free_mutex:
	DESTROYLOCK(&rl->lock);
free_mem:
	isc_mem_put(mctx, rl, sizeof(*rl));
	return (result);
}
Beispiel #9
0
static isc_result_t
newconnection(controllistener_t *listener, isc_socket_t *sock) {
	controlconnection_t *conn;
	isc_interval_t interval;
	isc_result_t result;

	conn = isc_mem_get(listener->mctx, sizeof(*conn));
	if (conn == NULL)
		return (ISC_R_NOMEMORY);

	conn->sock = sock;
	isccc_ccmsg_init(listener->mctx, sock, &conn->ccmsg);
	conn->ccmsg_valid = ISC_TRUE;
	conn->sending = ISC_FALSE;
	conn->buffer = NULL;
	conn->timer = NULL;
	isc_interval_set(&interval, 60, 0);
	result = isc_timer_create(ns_g_timermgr, isc_timertype_once,
				  NULL, &interval, listener->task,
				  control_timeout, conn, &conn->timer);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	conn->listener = listener;
	conn->nonce = 0;
	ISC_LINK_INIT(conn, link);

	result = isccc_ccmsg_readmessage(&conn->ccmsg, listener->task,
					 control_recvmessage, conn);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	ISC_LIST_APPEND(listener->connections, conn, link);
	return (ISC_R_SUCCESS);

 cleanup:
	if (conn->buffer != NULL)
		isc_buffer_free(&conn->buffer);
	isccc_ccmsg_invalidate(&conn->ccmsg);
	if (conn->timer != NULL)
		isc_timer_detach(&conn->timer);
	isc_mem_put(listener->mctx, conn, sizeof(*conn));
	return (result);
}
Beispiel #10
0
isc_result_t
new_ldap_cache(isc_mem_t *mctx, ldap_cache_t **cachep,
	       const char * const *argv)
{
	isc_result_t result;
	ldap_cache_t *cache = NULL;
	unsigned int cache_ttl;
	setting_t cache_settings[] = {
		{ "cache_ttl", default_uint(120) },
		end_of_settings
	};

	REQUIRE(cachep != NULL && *cachep == NULL);

	cache_settings[0].target = &cache_ttl;
	CHECK(set_settings(cache_settings, argv));

	CHECKED_MEM_GET_PTR(mctx, cache);
	ZERO_PTR(cache);
	isc_mem_attach(mctx, &cache->mctx);

	isc_interval_set(&cache->cache_ttl, cache_ttl, 0);

	if (cache_ttl) {
		CHECK(dns_rbt_create(mctx, cache_node_deleter, NULL,
				     &cache->rbt));
		CHECK(isc_mutex_init(&cache->mutex));
	}

	*cachep = cache;
	return ISC_R_SUCCESS;

cleanup:
	if (cache != NULL)
		destroy_ldap_cache(&cache);

	return result;
}
Beispiel #11
0
static inline isc_result_t
schedule(isc__timer_t *timer, isc_time_t *now, isc_boolean_t signal_ok) {
	isc_result_t result;
	isc__timermgr_t *manager;
	isc_time_t due;
	int cmp;
#ifdef USE_TIMER_THREAD
	isc_boolean_t timedwait;
#endif

	/*!
	 * Note: the caller must ensure locking.
	 */

	REQUIRE(timer->type != isc_timertype_inactive);

#ifndef USE_TIMER_THREAD
	UNUSED(signal_ok);
#endif /* USE_TIMER_THREAD */

	manager = timer->manager;

#ifdef USE_TIMER_THREAD
	/*!
	 * If the manager was timed wait, we may need to signal the
	 * manager to force a wakeup.
	 */
	timedwait = ISC_TF(manager->nscheduled > 0 &&
			   isc_time_seconds(&manager->due) != 0);
#endif

	/*
	 * Compute the new due time.
	 */
	if (timer->type != isc_timertype_once) {
		result = isc_time_add(now, &timer->interval, &due);
		if (result != ISC_R_SUCCESS)
			return (result);
		if (timer->type == isc_timertype_limited &&
		    isc_time_compare(&timer->expires, &due) < 0)
			due = timer->expires;
	} else {
		if (isc_time_isepoch(&timer->idle))
			due = timer->expires;
		else if (isc_time_isepoch(&timer->expires))
			due = timer->idle;
		else if (isc_time_compare(&timer->idle, &timer->expires) < 0)
			due = timer->idle;
		else
			due = timer->expires;
	}

	/*
	 * Schedule the timer.
	 */

	if (timer->index > 0) {
		/*
		 * Already scheduled.
		 */
		cmp = isc_time_compare(&due, &timer->due);
		timer->due = due;
		switch (cmp) {
		case -1:
			isc_heap_increased(manager->heap, timer->index);
			break;
		case 1:
			isc_heap_decreased(manager->heap, timer->index);
			break;
		case 0:
			/* Nothing to do. */
			break;
		}
	} else {
		timer->due = due;
		result = isc_heap_insert(manager->heap, timer);
		if (result != ISC_R_SUCCESS) {
			INSIST(result == ISC_R_NOMEMORY);
			return (ISC_R_NOMEMORY);
		}
		manager->nscheduled++;
	}

	XTRACETIMER(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
				   ISC_MSG_SCHEDULE, "schedule"), timer, due);

	/*
	 * If this timer is at the head of the queue, we need to ensure
	 * that we won't miss it if it has a more recent due time than
	 * the current "next" timer.  We do this either by waking up the
	 * run thread, or explicitly setting the value in the manager.
	 */
#ifdef USE_TIMER_THREAD

	/*
	 * This is a temporary (probably) hack to fix a bug on tru64 5.1
	 * and 5.1a.  Sometimes, pthread_cond_timedwait() doesn't actually
	 * return when the time expires, so here, we check to see if
	 * we're 15 seconds or more behind, and if we are, we signal
	 * the dispatcher.  This isn't such a bad idea as a general purpose
	 * watchdog, so perhaps we should just leave it in here.
	 */
	if (signal_ok && timedwait) {
		isc_interval_t fifteen;
		isc_time_t then;

		isc_interval_set(&fifteen, 15, 0);
		result = isc_time_add(&manager->due, &fifteen, &then);

		if (result == ISC_R_SUCCESS &&
		    isc_time_compare(&then, now) < 0) {
			SIGNAL(&manager->wakeup);
			signal_ok = ISC_FALSE;
			isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
				      ISC_LOGMODULE_TIMER, ISC_LOG_WARNING,
				      "*** POKED TIMER ***");
		}
	}

	if (timer->index == 1 && signal_ok) {
		XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
				      ISC_MSG_SIGNALSCHED,
				      "signal (schedule)"));
		SIGNAL(&manager->wakeup);
	}
#else /* USE_TIMER_THREAD */
	if (timer->index == 1 &&
	    isc_time_compare(&timer->due, &manager->due) < 0)
		manager->due = timer->due;
#endif /* USE_TIMER_THREAD */

	return (ISC_R_SUCCESS);
}
Beispiel #12
0
static int
t_timers5(void) {
	char		*p;
	int		result;
	isc_mem_t	*mctx;
	isc_taskmgr_t	*tmgr;
	unsigned int	workers;
	isc_result_t	isc_result;
	isc_timermgr_t	*timermgr;
	isc_event_t	*event;
	isc_time_t	expires;
	isc_interval_t	interval;

	T5_startflag = 0;
	T5_shutdownflag = 0;
	T5_eventcnt = 0;

	workers = 2;
	p = t_getenv("ISC_TASK_WORKERS");
	if (p != NULL)
		workers = atoi(p);

	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
		return(T_UNRESOLVED);
	}

	isc_result = isc_mutex_init(&T5_mx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mutex_init failed %s\n",
		       isc_result_totext(isc_result));
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	isc_result = isc_condition_init(&T5_cv);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_condition_init failed %s\n",
		       isc_result_totext(isc_result));
		DESTROYLOCK(&T5_mx);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	tmgr = NULL;
	isc_result = isc_taskmgr_create(mctx, workers, 0, &tmgr);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_taskmgr_create failed %s\n",
		       isc_result_totext(isc_result));
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	timermgr = NULL;
	isc_result = isc_timermgr_create(mctx, &timermgr);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_timermgr_create failed %s\n",
		       isc_result_totext(isc_result));
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	T5_task1 = NULL;
	isc_result = isc_task_create(tmgr, 0, &T5_task1);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_task_create failed %s\n",
		       isc_result_totext(isc_result));
		isc_timermgr_destroy(&timermgr);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	isc_result = isc_task_onshutdown(T5_task1, t5_shutdown_event, NULL);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_task_onshutdown failed %s\n",
		       isc_result_totext(isc_result));
		isc_timermgr_destroy(&timermgr);
		isc_task_destroy(&T5_task1);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	T5_task2 = NULL;
	isc_result = isc_task_create(tmgr, 0, &T5_task2);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_task_create failed %s\n",
		       isc_result_totext(isc_result));
		isc_timermgr_destroy(&timermgr);
		isc_task_destroy(&T5_task1);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	isc_result = isc_mutex_lock(&T5_mx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mutex_lock failed %s\n",
		       isc_result_totext(isc_result));
		isc_timermgr_destroy(&timermgr);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	event = isc_event_allocate(mctx, (void *)1 , (isc_eventtype_t)1,
				   t5_start_event, NULL, sizeof(*event));
	isc_task_send(T5_task1, &event);

	isc_time_settoepoch(&expires);
	isc_interval_set(&interval, T5_SECONDS, 0);

	T5_tickertimer = NULL;
	isc_result = isc_timer_create(timermgr, isc_timertype_ticker,
				      &expires, &interval, T5_task1,
				      t5_tick_event, NULL, &T5_tickertimer);

	if (isc_result != ISC_R_SUCCESS) {
		isc_timermgr_destroy(&timermgr);
		(void) isc_condition_signal(&T5_cv);
		(void) isc_mutex_unlock(&T5_mx);
		isc_task_destroy(&T5_task1);
		isc_task_destroy(&T5_task2);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	T5_oncetimer = NULL;
	isc_interval_set(&interval, (T5_SECONDS * T5_NTICKS) + 2, 0);
	isc_result = isc_time_nowplusinterval(&expires, &interval);
	if (isc_result != ISC_R_SUCCESS) {
		isc_timer_detach(&T5_tickertimer);
		isc_timermgr_destroy(&timermgr);
		(void)isc_condition_signal(&T5_cv);
		(void)isc_mutex_unlock(&T5_mx);
		isc_task_destroy(&T5_task1);
		isc_task_destroy(&T5_task2);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		return(T_UNRESOLVED);
	}

	isc_interval_set(&interval, 0, 0);
	isc_result = isc_timer_create(timermgr, isc_timertype_once,
				      &expires, &interval, T5_task2,
				      t5_once_event, NULL, &T5_oncetimer);

	if (isc_result != ISC_R_SUCCESS) {
		isc_timer_detach(&T5_tickertimer);
		isc_timermgr_destroy(&timermgr);
		(void) isc_condition_signal(&T5_cv);
		(void) isc_mutex_unlock(&T5_mx);
		isc_task_destroy(&T5_task1);
		isc_task_destroy(&T5_task2);
		isc_taskmgr_destroy(&tmgr);
		DESTROYLOCK(&T5_mx);
		(void) isc_condition_destroy(&T5_cv);
		isc_mem_destroy(&mctx);
		++T5_nprobs;
		return(T_UNRESOLVED);
	}

	/*
	 * Wait for shutdown processing to complete.
	 */
	while (! T5_shutdownflag) {
		isc_result = isc_condition_wait(&T5_cv, &T5_mx);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_condition_waituntil failed %s\n",
			       isc_result_totext(isc_result));
			++T5_nprobs;
		}
	}

	isc_result = isc_mutex_unlock(&T5_mx);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_mutex_unlock failed %s\n",
		       isc_result_totext(isc_result));
		++T5_nprobs;
	}

	if (T5_eventcnt != 1) {
		t_info("processed %d events\n", T5_eventcnt);
		++T5_nfails;
	}

	isc_timer_detach(&T5_tickertimer);
	isc_timer_detach(&T5_oncetimer);
	isc_timermgr_destroy(&timermgr);
	isc_task_destroy(&T5_task1);
	isc_task_destroy(&T5_task2);
	isc_taskmgr_destroy(&tmgr);
	DESTROYLOCK(&T5_mx);
	(void) isc_condition_destroy(&T5_cv);
	isc_mem_destroy(&mctx);

	result = T_UNRESOLVED;

	if ((T5_nfails == 0) && (T5_nprobs == 0))
		result = T_PASS;
	else if (T5_nfails)
		result = T_FAIL;

	return (result);
}
Beispiel #13
0
static void
t4_te(isc_task_t *task, isc_event_t *event) {

	isc_result_t	isc_result;
	isc_time_t	now;
	isc_time_t	base;
	isc_time_t	ulim;
	isc_time_t	llim;
	isc_time_t	expires;
	isc_interval_t	interval;

	++Tx_eventcnt;

	t_info("tick %d\n", Tx_eventcnt);

	/*
	 * Check expired time.
	 */

	isc_result = isc_time_now(&now);
	if (isc_result != ISC_R_SUCCESS) {
		t_info("isc_time_now failed %s\n",
		       isc_result_totext(isc_result));
		++Tx_nprobs;
	}

	if (isc_result == ISC_R_SUCCESS) {
		interval.seconds = Tx_seconds;
		interval.nanoseconds = Tx_nanoseconds;
		isc_result = isc_time_add(&Tx_lasttime, &interval, &base);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_add failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	}

	if (isc_result == ISC_R_SUCCESS) {
		interval.seconds = Tx_FUDGE_SECONDS;
		interval.nanoseconds = Tx_FUDGE_NANOSECONDS;
		isc_result = isc_time_add(&base, &interval, &ulim);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_add failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	}

	if (isc_result == ISC_R_SUCCESS) {
		isc_result = isc_time_subtract(&base, &interval, &llim);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_subtract failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	}

	if (isc_result == ISC_R_SUCCESS) {
		if (isc_time_compare(&llim, &now) > 0) {
			t_info("timer range error: early by "
			       "%lu microseconds\n",
			       (unsigned long)isc_time_microdiff(&base, &now));
			++Tx_nfails;
		} else if (isc_time_compare(&ulim, &now) < 0) {
			t_info("timer range error: late by "
			       "%lu microseconds\n",
			       (unsigned long)isc_time_microdiff(&now, &base));
			++Tx_nfails;
		}
		Tx_lasttime = now;
	}

	if (Tx_eventcnt < 3) {
		if (event->ev_type != ISC_TIMEREVENT_TICK) {
			t_info("received event type %d, expected type %d\n",
			       event->ev_type, ISC_TIMEREVENT_IDLE);
			++Tx_nfails;
		}
		if (Tx_eventcnt == 2) {
			isc_interval_set(&interval, T4_SECONDS,
					 T4_NANOSECONDS);
			isc_result = isc_time_nowplusinterval(&expires,
							      &interval);
			if (isc_result == ISC_R_SUCCESS) {
				isc_interval_set(&interval, 0, 0);
				isc_result =
					isc_timer_reset(Tx_timer,
							isc_timertype_once,
							&expires, &interval,
							ISC_FALSE);
				if (isc_result != ISC_R_SUCCESS) {
					t_info("isc_timer_reset failed %s\n",
					       isc_result_totext(isc_result));
					++Tx_nfails;
				}
			} else {
				t_info("isc_time_nowplusinterval failed %s\n",
				       isc_result_totext(isc_result));
				++Tx_nprobs;
			}
		}
	} else {
		if (event->ev_type != ISC_TIMEREVENT_LIFE) {
			t_info("received event type %d, expected type %d\n",
			       event->ev_type, ISC_TIMEREVENT_IDLE);
			++Tx_nfails;
		}

		isc_timer_detach(&Tx_timer);
		isc_task_shutdown(task);
	}

	isc_event_free(&event);
}
Beispiel #14
0
int
main(int argc, char *argv[]) {
	isc_task_t *t1, *t2;
	isc_timermgr_t *timgr;
	isc_time_t expires;
	isc_interval_t interval;
	isc_timer_t *ti1;
	unsigned int workers;
	isc_socketmgr_t *socketmgr;
	isc_socket_t *so1, *so2;
	isc_sockaddr_t sockaddr;
	struct in_addr ina;
	struct in6_addr in6a;
	isc_result_t result;
	int pf;

	if (argc > 1)
		workers = atoi(argv[1]);
	else
		workers = 2;
	printf("%d workers\n", workers);

	if (isc_net_probeipv6() == ISC_R_SUCCESS)
		pf = PF_INET6;
	else
		pf = PF_INET;

	/*
	 * EVERYTHING needs a memory context.
	 */
	mctx = NULL;
	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);

	/*
	 * The task manager is independent (other than memory context)
	 */
	manager = NULL;
	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
		      ISC_R_SUCCESS);

	/*
	 * Timer manager depends only on the memory context as well.
	 */
	timgr = NULL;
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);

	t1 = NULL;
	RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == ISC_R_SUCCESS);
	t2 = NULL;
	RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
		      ISC_R_SUCCESS);

	printf("task 1 = %p\n", t1);
	printf("task 2 = %p\n", t2);

	socketmgr = NULL;
	RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);

	/*
	 * Open up a listener socket.
	 */
	so1 = NULL;

	if (pf == PF_INET6) {
		in6a = in6addr_any;
		isc_sockaddr_fromin6(&sockaddr, &in6a, 5544);
	} else {
		ina.s_addr = INADDR_ANY;
		isc_sockaddr_fromin(&sockaddr, &ina, 5544);
	}
	RUNTIME_CHECK(isc_socket_create(socketmgr, pf, isc_sockettype_tcp,
					&so1) == ISC_R_SUCCESS);
	result = isc_socket_bind(so1, &sockaddr, ISC_SOCKET_REUSEADDRESS);
	RUNTIME_CHECK(result == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_socket_listen(so1, 0) == ISC_R_SUCCESS);

	/*
	 * Queue up the first accept event.
	 */
	RUNTIME_CHECK(isc_socket_accept(so1, t1, my_listen, "so1")
		      == ISC_R_SUCCESS);
	isc_time_settoepoch(&expires);
	isc_interval_set(&interval, 10, 0);
	ti1 = NULL;
	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
				       &interval, t1, timeout, so1, &ti1) ==
		      ISC_R_SUCCESS);

	/*
	 * Open up a socket that will connect to www.flame.org, port 80.
	 * Why not.  :)
	 */
	so2 = NULL;
	ina.s_addr = inet_addr("204.152.184.97");
	if (0 && pf == PF_INET6)
		isc_sockaddr_v6fromin(&sockaddr, &ina, 80);
	else
		isc_sockaddr_fromin(&sockaddr, &ina, 80);
	RUNTIME_CHECK(isc_socket_create(socketmgr, isc_sockaddr_pf(&sockaddr),
					isc_sockettype_tcp,
					&so2) == ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_socket_connect(so2, &sockaddr, t2,
					 my_connect, "so2") == ISC_R_SUCCESS);

	/*
	 * Detaching these is safe, since the socket will attach to the
	 * task for any outstanding requests.
	 */
	isc_task_detach(&t1);
	isc_task_detach(&t2);

	/*
	 * Wait a short while.
	 */
	sleep(10);

	fprintf(stderr, "Destroying socket manager\n");
	isc_socketmgr_destroy(&socketmgr);

	fprintf(stderr, "Destroying timer manager\n");
	isc_timermgr_destroy(&timgr);

	fprintf(stderr, "Destroying task manager\n");
	isc_taskmgr_destroy(&manager);

	isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);

	return (0);
}
Beispiel #15
0
int main (int argc, char *argv[])
{
    isc_taskmgr_t *manager = NULL;

    isc_timermgr_t *timgr = NULL;

    unsigned int workers;

    isc_time_t expires, now;

    isc_interval_t interval;

    if (argc > 1)
        workers = atoi (argv[1]);
    else
        workers = 2;
    printf ("%d workers\n", workers);

    RUNTIME_CHECK (isc_mem_create (0, 0, &mctx1) == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_taskmgr_create (mctx1, workers, 0, &manager) == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_timermgr_create (mctx1, &timgr) == ISC_R_SUCCESS);

    RUNTIME_CHECK (isc_task_create (manager, 0, &t1) == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_task_create (manager, 0, &t2) == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_task_create (manager, 0, &t3) == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_task_onshutdown (t1, shutdown_task, "1") == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_task_onshutdown (t2, shutdown_task, "2") == ISC_R_SUCCESS);
    RUNTIME_CHECK (isc_task_onshutdown (t3, shutdown_task, "3") == ISC_R_SUCCESS);

    printf ("task 1: %p\n", t1);
    printf ("task 2: %p\n", t2);
    printf ("task 3: %p\n", t3);

    TIME_NOW (&now);

    isc_interval_set (&interval, 2, 0);
    RUNTIME_CHECK (isc_timer_create (timgr, isc_timertype_once, NULL,
                                     &interval, t2, timeout, "2", &ti2) == ISC_R_SUCCESS);

    isc_interval_set (&interval, 1, 0);
    RUNTIME_CHECK (isc_timer_create (timgr, isc_timertype_ticker, NULL,
                                     &interval, t1, tick, "1", &ti1) == ISC_R_SUCCESS);

    isc_interval_set (&interval, 10, 0);
    RUNTIME_CHECK (isc_time_add (&now, &interval, &expires) == ISC_R_SUCCESS);
    isc_interval_set (&interval, 2, 0);
    RUNTIME_CHECK (isc_timer_create (timgr, isc_timertype_once, &expires,
                                     &interval, t3, timeout, "3", &ti3) == ISC_R_SUCCESS);

    isc_task_detach (&t1);
    isc_task_detach (&t2);
    isc_task_detach (&t3);

    sleep (15);
    printf ("destroy\n");
    isc_timer_detach (&ti1);
    isc_timer_detach (&ti2);
    isc_timer_detach (&ti3);
    sleep (2);
    isc_timermgr_destroy (&timgr);
    isc_taskmgr_destroy (&manager);
    printf ("destroyed\n");

    printf ("Statistics for mctx1:\n");
    isc_mem_stats (mctx1, stdout);
    isc_mem_destroy (&mctx1);

    return (0);
}
Beispiel #16
0
static void
tx_te(isc_task_t *task, isc_event_t *event) {
	isc_result_t	isc_result;
	isc_time_t	now;
	isc_time_t	base;
	isc_time_t	ulim;
	isc_time_t	llim;
	isc_interval_t	interval;
	isc_eventtype_t	expected_event_type;

	++Tx_eventcnt;

	t_info("tick %d\n", Tx_eventcnt);

	expected_event_type = ISC_TIMEREVENT_LIFE;
	if ((isc_timertype_t) event->ev_arg == isc_timertype_ticker)
		expected_event_type = ISC_TIMEREVENT_TICK;

	if (event->ev_type != expected_event_type) {
		t_info("expected event type %d, got %d\n",
			expected_event_type, (int) event->ev_type);
		++Tx_nfails;
	}

	isc_result = isc_time_now(&now);
	if (isc_result == ISC_R_SUCCESS) {
		isc_interval_set(&interval, Tx_seconds, Tx_nanoseconds);
		isc_result = isc_time_add(&Tx_lasttime, &interval, &base);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_add failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	} else {
		t_info("isc_time_now failed %s\n",
			isc_result_totext(isc_result));
		++Tx_nprobs;
	}

	if (isc_result == ISC_R_SUCCESS) {
		isc_interval_set(&interval,
				 Tx_FUDGE_SECONDS, Tx_FUDGE_NANOSECONDS);
		isc_result = isc_time_add(&base, &interval, &ulim);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_add failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	}

	if (isc_result == ISC_R_SUCCESS) {
		isc_result = isc_time_subtract(&base, &interval, &llim);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_subtract failed %s\n",
			       isc_result_totext(isc_result));
			++Tx_nprobs;
		}
	}

	if (isc_result == ISC_R_SUCCESS) {
		if (isc_time_compare(&llim, &now) > 0) {
			t_info("timer range error: early by "
			       "%lu microseconds\n",
			       (unsigned long)isc_time_microdiff(&base, &now));
			++Tx_nfails;
		} else if (isc_time_compare(&ulim, &now) < 0) {
			t_info("timer range error: late by "
			       "%lu microseconds\n",
			       (unsigned long)isc_time_microdiff(&now, &base));
			++Tx_nfails;
		}
		Tx_lasttime = now;
	}

	if (Tx_eventcnt == Tx_nevents) {
		isc_result = isc_time_now(&Tx_endtime);
		if (isc_result != ISC_R_SUCCESS) {
			t_info("isc_time_now failed %s\n",
				isc_result_totext(isc_result));
			++Tx_nprobs;
		}
		isc_timer_detach(&Tx_timer);
		isc_task_shutdown(task);
	}

	isc_event_free(&event);
}
Beispiel #17
0
int
main(int argc, char *argv[]) {
	isc_taskmgr_t *manager = NULL;
	isc_task_t *t1 = NULL, *t2 = NULL;
	isc_task_t *t3 = NULL, *t4 = NULL;
	isc_event_t *event;
	unsigned int workers;
	isc_timermgr_t *timgr;
	isc_timer_t *ti1, *ti2;
	struct isc_interval interval;

	if (argc > 1) {
		workers = atoi(argv[1]);
		if (workers < 1)
			workers = 1;
		if (workers > 8192)
			workers = 8192;
	} else
		workers = 2;
	printf("%d workers\n", workers);

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
		      ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_create(manager, 0, &t3) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_create(manager, 0, &t4) == ISC_R_SUCCESS);

	RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, "3") ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, "4") ==
		      ISC_R_SUCCESS);

	timgr = NULL;
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
	ti1 = NULL;

	isc_interval_set(&interval, 1, 0);
	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
				       &interval, t1, my_tick, "foo", &ti1) ==
		      ISC_R_SUCCESS);

	ti2 = NULL;
	isc_interval_set(&interval, 1, 0);
	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
				       &interval, t2, my_tick, "bar", &ti2) ==
		      ISC_R_SUCCESS);

	printf("task 1 = %p\n", t1);
	printf("task 2 = %p\n", t2);
	sleep(2);

	/*
	 * Note:  (void *)1 is used as a sender here, since some compilers
	 * don't like casting a function pointer to a (void *).
	 *
	 * In a real use, it is more likely the sender would be a
	 * structure (socket, timer, task, etc) but this is just a test
	 * program.
	 */
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
				   sizeof(*event));
	isc_task_send(t1, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
				   sizeof(*event));
	isc_task_send(t2, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
				   sizeof(*event));
	isc_task_send(t3, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
				   sizeof(*event));
	isc_task_send(t4, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
				   sizeof(*event));
	isc_task_send(t2, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
				   sizeof(*event));
	isc_task_send(t3, &event);
	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
				   sizeof(*event));
	isc_task_send(t4, &event);
	isc_task_purgerange(t3,
			    NULL,
			    ISC_EVENTTYPE_FIRSTEVENT,
			    ISC_EVENTTYPE_LASTEVENT, NULL);

	isc_task_detach(&t1);
	isc_task_detach(&t2);
	isc_task_detach(&t3);
	isc_task_detach(&t4);

	sleep(10);
	printf("destroy\n");
	isc_timer_detach(&ti1);
	isc_timer_detach(&ti2);
	isc_timermgr_destroy(&timgr);
	isc_taskmgr_destroy(&manager);
	printf("destroyed\n");

	isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);

	return (0);
}