]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/rq.c
6977405fe4700d6155c534449fc455e2a8718206
[openldap] / libraries / libldap_r / rq.c
1 /* $OpenLDAP$ */
2 #include "portable.h"
3
4 #include <stdio.h>
5
6 #include <ac/stdarg.h>
7 #include <ac/stdlib.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10 #include <ac/errno.h>
11
12 #include "ldap-int.h"
13 #include "ldap_pvt_thread.h"
14 #include "ldap_queue.h"
15 #include "ldap_rq.h"
16
17 #ifdef LDAP_SYNCREPL
18
19 void
20 ldap_pvt_runqueue_insert(
21         struct runqueue_s* rq,
22         time_t interval,
23         ldap_pvt_thread_start_t *routine,
24         void *arg
25 )
26 {
27         struct re_s* entry;
28
29         entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
30         entry->interval.tv_sec = interval;
31         entry->interval.tv_usec = 0;
32         entry->next_sched.tv_sec = time( NULL );
33         entry->next_sched.tv_usec = 0;
34         entry->routine = routine;
35         entry->arg = arg;
36         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
37 }
38
39 void
40 ldap_pvt_runqueue_remove(
41         struct runqueue_s* rq,
42         struct re_s* entry
43 )
44 {
45         struct re_s* e;
46
47         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
48                 if ( e == entry)
49                         break;
50         }
51
52         assert ( e == entry );
53
54         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
55
56         LDAP_FREE( entry );
57
58 }
59
60 struct re_s*
61 ldap_pvt_runqueue_next_sched(
62         struct runqueue_s* rq,
63         struct timeval** next_run
64 )
65 {
66         struct re_s* entry;
67
68         entry = LDAP_STAILQ_FIRST( &rq->task_list );
69         if ( entry == NULL ) {
70                 *next_run = NULL;
71                 return NULL;
72         } else {
73                 *next_run = &entry->next_sched;
74                 return entry;
75         }
76 }
77
78 void
79 ldap_pvt_runqueue_runtask(
80         struct runqueue_s* rq,
81         struct re_s* entry
82 )
83 {
84         LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
85 }
86
87 void
88 ldap_pvt_runqueue_stoptask(
89         struct runqueue_s* rq,
90         struct re_s* entry
91 )
92 {
93         LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
94 }
95
96 int
97 ldap_pvt_runqueue_isrunning(
98         struct runqueue_s* rq,
99         struct re_s* entry
100 )
101 {
102         struct re_s* e;
103
104         LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
105                 if ( e == entry ) {
106                         return 1;
107                 }
108         }
109         return 0;
110 }
111
112 void 
113 ldap_pvt_runqueue_resched(
114         struct runqueue_s* rq,
115         struct re_s* entry
116 )
117 {
118         struct re_s* prev;
119         struct re_s* e;
120
121         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
122                 if ( e == entry )
123                         break;
124         }
125
126         assert ( e == entry );
127
128         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
129
130         entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
131         if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
132                 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
133         } else {
134                 prev = NULL;
135                 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
136                         if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
137                                 if ( prev == NULL ) {
138                                         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
139                                 } else {
140                                         LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
141                                 }
142                         }
143                         prev = e;
144                 }
145         }
146 }
147
148 #endif