root/src/eventer/eventer_impl.c

Revision 171d8ed2238b758bd302a0bb23cd0aeeabc50b33, 3.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 10 years ago)

make eventer.h one-stop-shopping

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include "eventer/eventer.h"
8 #include "utils/noit_log.h"
9 #include <pthread.h>
10 #include <assert.h>
11
12 #ifdef HAVE_KQUEUE
13 extern struct _eventer_impl eventer_kqueue_impl;
14 #endif
15
16 eventer_impl_t registered_eventers[] = {
17 #ifdef HAVE_KQUEUE
18   &eventer_kqueue_impl,
19 #endif
20   NULL
21 };
22
23 eventer_impl_t __eventer = NULL;
24
25
26 static int __default_queue_threads = 5;
27 static eventer_jobq_t __global_backq, __default_jobq;
28 static pthread_mutex_t recurrent_lock = PTHREAD_MUTEX_INITIALIZER;
29 struct recurrent_events {
30   eventer_t e;
31   struct recurrent_events *next;
32 } *recurrent_events = NULL;
33
34
35 int eventer_impl_propset(const char *key, const char *value) {
36   if(!strcasecmp(key, "default_queue_threads")) {
37     __default_queue_threads = atoi(value);
38     if(__default_queue_threads < 1) {
39       noitL(noit_error, "default_queue_threads must be >= 1\n");
40       return -1;
41     }
42     return 0;
43   }
44   return -1;
45 }
46
47 int eventer_impl_init() {
48   int i;
49   eventer_t e;
50   eventer_ssl_init();
51   eventer_jobq_init(&__global_backq);
52   e = eventer_alloc();
53   e->mask = EVENTER_RECURRENT;
54   e->closure = &__global_backq;
55   e->callback = eventer_jobq_consume_available;
56
57   /* We call directly here as we may not be completely initialized */
58   eventer_add_recurrent(e);
59
60   eventer_jobq_init(&__default_jobq);
61   __default_jobq.backq = &__global_backq;
62   for(i=0; i<__default_queue_threads; i++)
63     eventer_jobq_increase_concurrency(&__default_jobq);
64   return 0;
65 }
66
67 void eventer_add_asynch(eventer_jobq_t *q, eventer_t e) {
68   eventer_job_t *job;
69   job = calloc(1, sizeof(*job));
70   job->fd_event = e;
71   gettimeofday(&job->create_time, NULL);
72   if(e->whence.tv_sec) {
73     job->timeout_event = eventer_alloc();
74     memcpy(&job->timeout_event->whence, &e->whence, sizeof(e->whence));
75     job->timeout_event->mask = EVENTER_TIMER;
76     job->timeout_event->closure = job;
77     job->timeout_event->callback = eventer_jobq_execute_timeout;
78     eventer_add(job->timeout_event);
79   }
80   eventer_jobq_enqueue(q ? q : &__default_jobq, job);
81 }
82
83 void eventer_dispatch_recurrent(struct timeval *now) {
84   struct recurrent_events *node;
85   struct timeval __now;
86   if(!now) {
87     gettimeofday(&__now, NULL);
88     now = &__now;
89   }
90   pthread_mutex_lock(&recurrent_lock);
91   for(node = recurrent_events; node; node = node->next) {
92     node->e->callback(node->e, EVENTER_RECURRENT, node->e->closure, now);
93   }
94   pthread_mutex_unlock(&recurrent_lock);
95 }
96 eventer_t eventer_remove_recurrent(eventer_t e) {
97   struct recurrent_events *node, *prev = NULL;
98   pthread_mutex_lock(&recurrent_lock);
99   for(node = recurrent_events; node; node = node->next) {
100     if(node->e == e) {
101       if(prev) prev->next = node->next;
102       else recurrent_events = node->next;
103       free(node);
104       pthread_mutex_unlock(&recurrent_lock);
105       return e;
106     }
107     prev = node;
108   }
109   pthread_mutex_unlock(&recurrent_lock);
110   return NULL;
111 }
112 void eventer_add_recurrent(eventer_t e) {
113   struct recurrent_events *node;
114   assert(e->mask & EVENTER_RECURRENT);
115   pthread_mutex_lock(&recurrent_lock);
116   for(node = recurrent_events; node; node = node->next)
117     if(node->e == e) {
118       pthread_mutex_unlock(&recurrent_lock);
119       return;
120     }
121   node = calloc(1, sizeof(*node));
122   node->e = e;
123   node->next = recurrent_events;
124   recurrent_events = node;
125   pthread_mutex_unlock(&recurrent_lock);
126 }
127
Note: See TracBrowser for help on using the browser.