"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/src/knot/common/evsched.c" (16 Jul 2019, 5890 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "evsched.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <sys/time.h>
   18 #include <stdlib.h>
   19 #include <stdio.h>
   20 #include <string.h>
   21 #include <assert.h>
   22 
   23 #include "libknot/libknot.h"
   24 #include "knot/server/dthreads.h"
   25 #include "knot/common/evsched.h"
   26 
   27 /*! \brief Some implementations of timercmp >= are broken, this is for compat.*/
   28 static inline int timercmp_ge(struct timeval *a, struct timeval *b) {
   29     return timercmp(a, b, >) || timercmp(a, b, ==);
   30 }
   31 
   32 static int compare_event_heap_nodes(void *e1, void *e2)
   33 {
   34     if (timercmp(&((event_t *)e1)->tv, &((event_t *)e2)->tv, <)) return -1;
   35     if (timercmp(&((event_t *)e1)->tv, &((event_t *)e2)->tv, >)) return 1;
   36     return 0;
   37 }
   38 
   39 /*!
   40  * \brief Get time T (now) + dt miliseconds.
   41  */
   42 static struct timeval timeval_in(uint32_t dt)
   43 {
   44     struct timeval tv = { 0 };
   45     gettimeofday(&tv, NULL);
   46 
   47     /* Add number of seconds. */
   48     tv.tv_sec += dt / 1000;
   49 
   50     /* Add the number of microseconds. */
   51     tv.tv_usec += (dt % 1000) * 1000;
   52 
   53     /* Check for overflow. */
   54     while (tv.tv_usec > 999999) {
   55         tv.tv_sec += 1;
   56         tv.tv_usec -= 1 * 1000 * 1000;
   57     }
   58 
   59     return tv;
   60 }
   61 
   62 /*! \brief Event scheduler loop. */
   63 static int evsched_run(dthread_t *thread)
   64 {
   65     evsched_t *sched = (evsched_t*)thread->data;
   66     if (sched == NULL) {
   67         return KNOT_EINVAL;
   68     }
   69 
   70     /* Run event loop. */
   71     pthread_mutex_lock(&sched->heap_lock);
   72     while (!dt_is_cancelled(thread)) {
   73         if (!!EMPTY_HEAP(&sched->heap) || sched->paused) {
   74             pthread_cond_wait(&sched->notify, &sched->heap_lock);
   75             continue;
   76         }
   77 
   78         /* Get current time. */
   79         struct timeval dt;
   80         gettimeofday(&dt, 0);
   81 
   82         /* Get next event. */
   83         event_t *ev = *((event_t**)HHEAD(&sched->heap));
   84         assert(ev != NULL);
   85 
   86         if (timercmp_ge(&dt, &ev->tv)) {
   87             heap_delmin(&sched->heap);
   88             ev->cb(ev);
   89         } else {
   90             /* Wait for next event or interrupt. Unlock calendar. */
   91             struct timespec ts;
   92             ts.tv_sec = ev->tv.tv_sec;
   93             ts.tv_nsec = ev->tv.tv_usec * 1000L;
   94             pthread_cond_timedwait(&sched->notify, &sched->heap_lock, &ts);
   95         }
   96     }
   97     pthread_mutex_unlock(&sched->heap_lock);
   98 
   99     return KNOT_EOK;
  100 }
  101 
  102 int evsched_init(evsched_t *sched, void *ctx)
  103 {
  104     memset(sched, 0, sizeof(evsched_t));
  105     sched->ctx = ctx;
  106 
  107     /* Initialize event calendar. */
  108     pthread_mutex_init(&sched->heap_lock, 0);
  109     pthread_cond_init(&sched->notify, 0);
  110     heap_init(&sched->heap, compare_event_heap_nodes, 0);
  111 
  112     sched->thread = dt_create(1, evsched_run, NULL, sched);
  113 
  114     if (sched->thread == NULL) {
  115         evsched_deinit(sched);
  116         return KNOT_ENOMEM;
  117     }
  118 
  119     return KNOT_EOK;
  120 }
  121 
  122 void evsched_deinit(evsched_t *sched)
  123 {
  124     if (sched == NULL) {
  125         return;
  126     }
  127 
  128     /* Deinitialize event calendar. */
  129     pthread_mutex_destroy(&sched->heap_lock);
  130     pthread_cond_destroy(&sched->notify);
  131 
  132     while (!EMPTY_HEAP(&sched->heap)) {
  133         event_t *e = (event_t *)*HHEAD(&sched->heap);
  134         heap_delmin(&sched->heap);
  135         evsched_event_free(e);
  136     }
  137 
  138     heap_deinit(&sched->heap);
  139 
  140     if (sched->thread != NULL) {
  141         dt_delete(&sched->thread);
  142     }
  143 
  144     /* Clear the structure. */
  145     memset(sched, 0, sizeof(evsched_t));
  146 }
  147 
  148 event_t *evsched_event_create(evsched_t *sched, event_cb_t cb, void *data)
  149 {
  150     /* Create event. */
  151     if (sched == NULL) {
  152         return NULL;
  153     }
  154 
  155     /* Allocate. */
  156     event_t *e = malloc(sizeof(event_t));
  157     if (e == NULL) {
  158         return NULL;
  159     }
  160 
  161     /* Initialize. */
  162     memset(e, 0, sizeof(event_t));
  163     e->sched = sched;
  164     e->cb = cb;
  165     e->data = data;
  166     e->hpos.pos=0;
  167 
  168     return e;
  169 }
  170 
  171 void evsched_event_free(event_t *ev)
  172 {
  173     if (ev == NULL) {
  174         return;
  175     }
  176 
  177     free(ev);
  178 }
  179 
  180 int evsched_schedule(event_t *ev, uint32_t dt)
  181 {
  182     if (ev == NULL || ev->sched == NULL) {
  183         return KNOT_EINVAL;
  184     }
  185 
  186     struct timeval new_time = timeval_in(dt);
  187 
  188     evsched_t *sched = ev->sched;
  189 
  190     /* Lock calendar. */
  191     pthread_mutex_lock(&sched->heap_lock);
  192 
  193     ev->tv = new_time;
  194 
  195     /* Make sure it's not already enqueued. */
  196     int found = heap_find(&sched->heap, (heap_val_t *)ev);
  197     if (found > 0) {
  198         heap_replace(&sched->heap, found, (heap_val_t *)ev);
  199     } else {
  200         heap_insert(&sched->heap, (heap_val_t *)ev);
  201     }
  202 
  203     /* Unlock calendar. */
  204     pthread_cond_signal(&sched->notify);
  205     pthread_mutex_unlock(&sched->heap_lock);
  206 
  207     return KNOT_EOK;
  208 }
  209 
  210 int evsched_cancel(event_t *ev)
  211 {
  212     if (ev == NULL || ev->sched == NULL) {
  213         return KNOT_EINVAL;
  214     }
  215 
  216     evsched_t *sched = ev->sched;
  217 
  218     /* Lock calendar. */
  219     pthread_mutex_lock(&sched->heap_lock);
  220 
  221     int found = heap_find(&sched->heap, (heap_val_t *)ev);
  222     if (found > 0) {
  223         heap_delete(&sched->heap, found);
  224     }
  225 
  226     /* Unlock calendar. */
  227     pthread_cond_signal(&sched->notify);
  228     pthread_mutex_unlock(&sched->heap_lock);
  229 
  230     /* Reset event timer. */
  231     memset(&ev->tv, 0, sizeof(struct timeval));
  232 
  233     return KNOT_EOK;
  234 }
  235 
  236 void evsched_start(evsched_t *sched)
  237 {
  238     dt_start(sched->thread);
  239 }
  240 
  241 void evsched_stop(evsched_t *sched)
  242 {
  243     pthread_mutex_lock(&sched->heap_lock);
  244     dt_stop(sched->thread);
  245     pthread_cond_signal(&sched->notify);
  246     pthread_mutex_unlock(&sched->heap_lock);
  247 }
  248 
  249 void evsched_join(evsched_t *sched)
  250 {
  251     dt_join(sched->thread);
  252 }
  253 
  254 void evsched_pause(evsched_t *sched)
  255 {
  256     pthread_mutex_lock(&sched->heap_lock);
  257     sched->paused = true;
  258     pthread_mutex_unlock(&sched->heap_lock);
  259 }
  260 
  261 void evsched_resume(evsched_t *sched)
  262 {
  263     pthread_mutex_lock(&sched->heap_lock);
  264     sched->paused = false;
  265     pthread_cond_signal(&sched->notify);
  266     pthread_mutex_unlock(&sched->heap_lock);
  267 }