projects
/
crypto.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added asynchronous event tasks to SILC Scheduler. Added
[crypto.git]
/
lib
/
silcutil
/
silcfsm.c
diff --git
a/lib/silcutil/silcfsm.c
b/lib/silcutil/silcfsm.c
index 5139c0cd7f7be2f2c0f7b45f1e76dd1d7752da8d..2ded983253aa77906cd439210b69306afe92116c 100644
(file)
--- a/
lib/silcutil/silcfsm.c
+++ b/
lib/silcutil/silcfsm.c
@@
-58,8
+58,13
@@
SilcBool silc_fsm_init(SilcFSM fsm,
void *destructor_context,
SilcSchedule schedule)
{
void *destructor_context,
SilcSchedule schedule)
{
- if (!schedule)
- return FALSE;
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ return FALSE;
+ }
+ }
fsm->fsm_context = fsm_context;
fsm->state_context = NULL;
fsm->fsm_context = fsm_context;
fsm->state_context = NULL;
@@
-106,7
+111,7
@@
void silc_fsm_thread_init(SilcFSMThread thread,
SILC_LOG_DEBUG(("Initializing new thread %p (%s)",
thread, real_thread ? "real" : "FSM"));
SILC_LOG_DEBUG(("Initializing new thread %p (%s)",
thread, real_thread ? "real" : "FSM"));
- SILC_
ASSERT
(!fsm->thread);
+ SILC_
VERIFY
(!fsm->thread);
thread->fsm_context = thread_context;
thread->state_context = NULL;
thread->fsm_context = thread_context;
thread->state_context = NULL;
@@
-151,6
+156,9
@@
SILC_TASK_CALLBACK(silc_fsm_free_final)
if (f->thread && f->u.t.event)
silc_fsm_event_free(f->u.t.event);
if (f->thread && f->u.t.event)
silc_fsm_event_free(f->u.t.event);
+ if (!f->thread)
+ silc_atomic_uninit32(&f->u.m.threads);
+
silc_free(f);
}
silc_free(f);
}
@@
-312,11
+320,7
@@
void silc_fsm_finish(void *fsm)
{
SilcFSM f = fsm;
{
SilcFSM f = fsm;
- SILC_ASSERT(!f->finished);
-
- /* Machine must not have active threads */
- if (!f->thread && silc_atomic_get_int32(&f->u.m.threads))
- assert(silc_atomic_get_int32(&f->u.m.threads) == 0);
+ SILC_VERIFY(!f->finished);
f->started = FALSE;
f->finished = TRUE;
f->started = FALSE;
f->finished = TRUE;
@@
-355,7
+359,7
@@
SilcSchedule silc_fsm_get_schedule(void *fsm)
SilcFSM silc_fsm_get_machine(SilcFSMThread thread)
{
SilcFSM silc_fsm_get_machine(SilcFSMThread thread)
{
- SILC_
ASSERT
(thread->thread);
+ SILC_
VERIFY
(thread->thread);
return (SilcFSM)thread->u.t.fsm;
}
return (SilcFSM)thread->u.t.fsm;
}
@@
-405,7
+409,7
@@
SilcBool silc_fsm_thread_wait(void *fsm, void *thread)
{
SilcFSM t = thread;
{
SilcFSM t = thread;
- SILC_
ASSERT
(t->thread);
+ SILC_
VERIFY
(t->thread);
t->u.t.event = silc_fsm_event_alloc(t->u.t.fsm);
if (!t->u.t.event)
t->u.t.event = silc_fsm_event_alloc(t->u.t.fsm);
if (!t->u.t.event)
@@
-481,11
+485,13
@@
SILC_TASK_CALLBACK(silc_fsm_finish_fsm)
fsm->destructor(fsm, fsm->fsm_context, fsm->destructor_context);
} else {
fsm->destructor(fsm, fsm->fsm_context, fsm->destructor_context);
} else {
+ /* Machine must not have active threads */
+ SILC_VERIFY(silc_atomic_get_int32(&fsm->u.m.threads) == 0);
+
if (fsm->u.m.lock) {
silc_mutex_free(fsm->u.m.lock);
fsm->u.m.lock = NULL;
}
if (fsm->u.m.lock) {
silc_mutex_free(fsm->u.m.lock);
fsm->u.m.lock = NULL;
}
- silc_atomic_uninit32(&fsm->u.m.threads);
/* Call the destructor callback. */
if (fsm->destructor)
/* Call the destructor callback. */
if (fsm->destructor)
@@
-514,7
+520,7
@@
SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm)
void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm)
{
SILC_LOG_DEBUG(("Initializing event %p", event));
void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm)
{
SILC_LOG_DEBUG(("Initializing event %p", event));
- SILC_
ASSERT
(!fsm->thread);
+ SILC_
VERIFY
(!fsm->thread);
memset(event, 0, sizeof(*event));
event->fsm = fsm;
event->refcnt = 0;
memset(event, 0, sizeof(*event));
event->fsm = fsm;
event->refcnt = 0;
@@
-764,18
+770,29
@@
void *silc_fsm_thread(void *context)
/* We allocate new SilcSchedule for the FSM, as the old SilcSchedule
cannot be used in this thread. Application may still use it if it
wants but we use our own. */
/* We allocate new SilcSchedule for the FSM, as the old SilcSchedule
cannot be used in this thread. Application may still use it if it
wants but we use our own. */
- fsm->schedule = silc_schedule_init(0, old);
- if (silc_unlikely(!fsm->schedule))
+ fsm->schedule = silc_schedule_init(0, old, silc_schedule_get_stack(old), old);
+ if (silc_unlikely(!fsm->schedule)) {
+ fsm->schedule = old;
return NULL;
return NULL;
+ }
+
+ /* The new scheduler is a global scheduler in this thread */
+ silc_schedule_set_global(fsm->schedule);
/* Start the FSM thread */
if (silc_unlikely(!silc_schedule_task_add_timeout(fsm->schedule,
/* Start the FSM thread */
if (silc_unlikely(!silc_schedule_task_add_timeout(fsm->schedule,
- silc_fsm_run, fsm, 0, 0)))
+ silc_fsm_run, fsm, 0, 0))) {
+ silc_schedule_uninit(fsm->schedule);
+ fsm->schedule = old;
return NULL;
return NULL;
+ }
/* Run the scheduler */
silc_schedule(fsm->schedule);
/* Run the scheduler */
silc_schedule(fsm->schedule);
+ /* Reset global scheduler */
+ silc_schedule_set_global(NULL);
+
/* Free resources */
silc_schedule_uninit(fsm->schedule);
/* Free resources */
silc_schedule_uninit(fsm->schedule);