fixed a at-least-one-year-old bug which caused crashed in the scheduler
[strongswan.git] / src / charon / threads / scheduler.c
index 9bbe5c9..ba86c1b 100644 (file)
@@ -6,7 +6,8 @@
  */
 
 /*
- * Copyright (C) 2005 Jan Hutter, Martin Willi
+ * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -41,15 +42,6 @@ struct private_scheduler_t {
         * Public part of a scheduler_t object.
         */
         scheduler_t public;
-        
-       /**
-        * @brief Get events from the event queue and add them to to job queue.
-        *
-        * Thread function started at creation of the scheduler object.
-        *
-        * @param this          calling object
-        */
-       void (*get_events) (private_scheduler_t *this);
 
        /**
         * Assigned thread.
@@ -72,17 +64,17 @@ static void get_events(private_scheduler_t * this)
        /* cancellation disabled by default */
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
        
-       this->logger->log(this->logger, CONTROL, "scheduler thread running, thread_ID: %06d", (int)pthread_self());
+       this->logger->log(this->logger, CONTROL, "scheduler thread running, thread_ID: %06u", (int)pthread_self());
 
-       for (;;)
+       while (TRUE)
        {
-               this->logger->log(this->logger, CONTROL|LEVEL2, "Waiting for next event...");
+               this->logger->log(this->logger, CONTROL|LEVEL2, "waiting for next event...");
                /* get a job, this block until one is available */
                current_job = charon->event_queue->get(charon->event_queue);
                /* queue the job in the job queue, workers will eat them */
-               charon->job_queue->add(charon->job_queue, current_job);
-               this->logger->log(this->logger, CONTROL | LEVEL1, "Got event, added job %s to job-queue.", 
+               this->logger->log(this->logger, CONTROL | LEVEL1, "got event, adding job %s to job-queue.", 
                                                  mapping_find(job_type_m, current_job->get_type(current_job)));
+               charon->job_queue->add(charon->job_queue, current_job);
        }
 }
 
@@ -91,11 +83,11 @@ static void get_events(private_scheduler_t * this)
  */
 static void destroy(private_scheduler_t *this)
 {
-       this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate scheduler thread");
+       this->logger->log(this->logger, CONTROL | LEVEL1, "going to terminate scheduler thread");
        pthread_cancel(this->assigned_thread);
 
        pthread_join(this->assigned_thread, NULL);
-       this->logger->log(this->logger, CONTROL | LEVEL1, "Scheduler thread terminated");
+       this->logger->log(this->logger, CONTROL | LEVEL1, "scheduler thread terminated");
 
        free(this);
 }
@@ -108,16 +100,15 @@ scheduler_t * scheduler_create()
        private_scheduler_t *this = malloc_thing(private_scheduler_t);
 
        this->public.destroy = (void(*)(scheduler_t*)) destroy;
-       this->get_events = get_events;
        
        this->logger = logger_manager->get_logger(logger_manager, SCHEDULER);
        
-       if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->get_events, this) != 0)
+       if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))get_events, this) != 0)
        {
                /* thread could not be created  */
-               this->logger->log(this->logger, ERROR, "Scheduler thread could not be created!");
+               this->logger->log(this->logger, ERROR, "scheduler thread could not be created!");
                free(this);
-               charon->kill(charon, "Unable to create scheduler thread");
+               charon->kill(charon, "unable to create scheduler thread");
        }
 
        return &(this->public);