#include "jitterentropy-base.h"
#include "jitterentropy-timer.h"
#ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
JENT_PRIVATE_STATIC
int jent_notime_init(void **ctx)
{
struct jent_notime_ctx *thread_ctx;
long ncpu = jent_ncpu();
if (ncpu < 0)
return (int)ncpu;
if (ncpu < 2)
return -EOPNOTSUPP;
thread_ctx = calloc(1, sizeof(struct jent_notime_ctx));
if (!thread_ctx)
return -errno;
*ctx = thread_ctx;
return 0;
}
JENT_PRIVATE_STATIC
void jent_notime_fini(void *ctx)
{
struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
if (thread_ctx)
free(thread_ctx);
}
static int jent_notime_start(void *ctx,
void *(*start_routine) (void *), void *arg)
{
struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
int ret;
if (!thread_ctx)
return -EINVAL;
ret = -pthread_attr_init(&thread_ctx->notime_pthread_attr);
if (ret)
return ret;
return -pthread_create(&thread_ctx->notime_thread_id,
&thread_ctx->notime_pthread_attr,
start_routine, arg);
}
static void jent_notime_stop(void *ctx)
{
struct jent_notime_ctx *thread_ctx = (struct jent_notime_ctx *)ctx;
pthread_join(thread_ctx->notime_thread_id, NULL);
pthread_attr_destroy(&thread_ctx->notime_pthread_attr);
}
static struct jent_notime_thread jent_notime_thread_builtin = {
.jent_notime_init = jent_notime_init,
.jent_notime_fini = jent_notime_fini,
.jent_notime_start = jent_notime_start,
.jent_notime_stop = jent_notime_stop
};
static int jent_force_internal_timer = 0;
static int jent_notime_switch_blocked = 0;
void jent_notime_block_switch(void)
{
jent_notime_switch_blocked = 1;
}
static struct jent_notime_thread *notime_thread = &jent_notime_thread_builtin;
static void *jent_notime_sample_timer(void *arg)
{
struct rand_data *ec = (struct rand_data *)arg;
ec->notime_timer = 0;
while (1) {
if (ec->notime_interrupt)
return NULL;
ec->notime_timer++;
}
return NULL;
}
int jent_notime_settick(struct rand_data *ec)
{
if (!ec->enable_notime || !notime_thread)
return 0;
ec->notime_interrupt = 0;
ec->notime_prev_timer = 0;
ec->notime_timer = 0;
return notime_thread->jent_notime_start(ec->notime_thread_ctx,
jent_notime_sample_timer, ec);
}
void jent_notime_unsettick(struct rand_data *ec)
{
if (!ec->enable_notime || !notime_thread)
return;
ec->notime_interrupt = 1;
notime_thread->jent_notime_stop(ec->notime_thread_ctx);
}
void jent_get_nstime_internal(struct rand_data *ec, uint64_t *out)
{
if (ec->enable_notime) {
while (ec->notime_timer == ec->notime_prev_timer)
jent_yield();
ec->notime_prev_timer = ec->notime_timer;
*out = ec->notime_prev_timer;
} else {
jent_get_nstime(out);
}
}
static inline int jent_notime_enable_thread(struct rand_data *ec)
{
if (notime_thread)
return notime_thread->jent_notime_init(&ec->notime_thread_ctx);
return 0;
}
void jent_notime_disable(struct rand_data *ec)
{
if (notime_thread)
notime_thread->jent_notime_fini(ec->notime_thread_ctx);
}
int jent_notime_enable(struct rand_data *ec, unsigned int flags)
{
if (jent_force_internal_timer || (flags & JENT_FORCE_INTERNAL_TIMER)) {
if (!jent_force_internal_timer &&
jent_time_entropy_init(ec->osr,
flags | JENT_FORCE_INTERNAL_TIMER))
return EHEALTH;
ec->enable_notime = 1;
return jent_notime_enable_thread(ec);
}
return 0;
}
int jent_notime_switch(struct jent_notime_thread *new_thread)
{
if (jent_notime_switch_blocked)
return -EAGAIN;
notime_thread = new_thread;
return 0;
}
void jent_notime_force(void)
{
jent_force_internal_timer = 1;
}
int jent_notime_forced(void)
{
return jent_force_internal_timer;
}
#endif