#include <processing/jobs/callback_job.h>
#include <threading/mutex.h>
+/**
+ * Threshold in milliseconds up to which the default scheduler is used.
+ * This includes the roaming events (100 ms) and initial retransmits.
+ */
+#define DEFAULT_SCHEDULER_THRESHOLD 3000
+
typedef struct private_scheduler_t private_scheduler_t;
/**
* Mutex to safely access the scheduled jobs.
*/
mutex_t *mutex;
+
+ /**
+ * Default scheduler used for short-term events.
+ */
+ scheduler_t *default_scheduler;
};
/**
entry_t *entry = NULL;
jstring jid;
+ /* use the default scheduler for short-term events */
+ if (ms <= DEFAULT_SCHEDULER_THRESHOLD)
+ {
+ this->default_scheduler->schedule_job_ms(this->default_scheduler,
+ job, ms);
+ return;
+ }
+
androidjni_attach_thread(&env);
jid = allocate_id(this, env);
if (!jid)
JNIEnv *env;
jmethodID method_id;
+ this->default_scheduler->flush(this->default_scheduler);
+
this->mutex->lock(this->mutex);
this->jobs->destroy_function(this->jobs, destroy_entry);
this->jobs = hashtable_create(hashtable_hash_str, hashtable_equals_str, 16);
(*env)->DeleteGlobalRef(env, this->cls);
}
androidjni_detach_thread();
+ this->default_scheduler->destroy(this->default_scheduler);
this->mutex->destroy(this->mutex);
this->jobs->destroy(this->jobs);
free(this);
/*
* Described in header
*/
-scheduler_t *android_scheduler_create(jobject context)
+scheduler_t *android_scheduler_create(jobject context, scheduler_t *scheduler)
{
private_scheduler_t *this;
JNIEnv *env;
.flush = _flush,
.destroy = _destroy,
},
+ .default_scheduler = scheduler,
.jobs = hashtable_create(hashtable_hash_str, hashtable_equals_str, 16),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
);
/**
* Create an Android-specific scheduler_t implementation.
*
+ * The given scheduler is used for short-term events. We can't destroy it anyway
+ * because of the scheduler job operating on it, and this way we can use it to
+ * avoid the overhead of broadcasts for some events.
+ *
* @param context Context object
+ * @param scheduler the default scheduler used as fallback
* @return scheduler_t instance
*/
-scheduler_t *android_scheduler_create(jobject context);
+scheduler_t *android_scheduler_create(jobject context, scheduler_t *scheduler);
#endif /** ANDROID_SCHEDULER_H_ @}*/
* Sockets that were bypassed and we keep track for
*/
linked_list_t *sockets;
-
- /**
- * Default scheduler if we don't use it
- */
- scheduler_t *default_scheduler;
};
/**
);
charonservice = &this->public;
- if (android_sdk_version >= ANDROID_MARSHMALLOW)
- {
- /* use a custom scheduler so the app is woken when jobs have to run.
- * we can't destroy the default scheduler here due to the scheduler
- * job that's operating on it, so we stash it away until later */
- this->default_scheduler = lib->scheduler;
- lib->scheduler = android_scheduler_create(service);
- }
-
lib->plugins->add_static_features(lib->plugins, "androidbridge", features,
countof(features), TRUE, NULL, NULL);
{
private_charonservice_t *this = (private_charonservice_t*)charonservice;
- DESTROY_IF(this->default_scheduler);
this->network_manager->destroy(this->network_manager);
this->sockets->destroy(this->sockets);
this->builder->destroy(this->builder);
return FALSE;
}
+ if (android_sdk_version >= ANDROID_MARSHMALLOW)
+ {
+ /* use a custom scheduler so the app is woken when jobs have to run */
+ lib->scheduler = android_scheduler_create(this, lib->scheduler);
+ }
+
/* set options before initializing other libraries that might read them */
logfile = androidjni_convert_jstring(env, jlogfile);