]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/rq.c
4cf59c199252a20795790fa796f77d153af9bee3
[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         void *private
24 )
25 {
26         struct re_s* entry;
27
28         entry = (struct re_s *) ch_calloc( 1, sizeof( struct re_s ));
29         entry->interval.tv_sec = interval;
30         entry->interval.tv_usec = 0;
31         entry->next_sched.tv_sec = time( NULL );
32         entry->next_sched.tv_usec = 0;
33         entry->private = private;
34         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
35 }
36
37 void
38 ldap_pvt_runqueue_remove(
39         struct runqueue_s* rq,
40         struct re_s* entry
41 )
42 {
43         struct re_s* e;
44
45         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
46                 if ( e == entry)
47                         break;
48         }
49
50         assert ( e == entry );
51
52         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
53
54         ch_free( entry );
55
56 }
57
58 struct re_s*
59 ldap_pvt_runqueue_next_sched(
60         struct runqueue_s* rq,
61         struct timeval** next_run
62 )
63 {
64         struct re_s* entry;
65
66         entry = LDAP_STAILQ_FIRST( &rq->task_list );
67         if ( entry == NULL ) {
68                 *next_run = NULL;
69                 return NULL;
70         } else {
71                 *next_run = &entry->next_sched;
72                 return entry;
73         }
74 }
75
76 void
77 ldap_pvt_runqueue_runtask(
78         struct runqueue_s* rq,
79         struct re_s* entry
80 )
81 {
82         LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
83 }
84
85 void
86 ldap_pvt_runqueue_stoptask(
87         struct runqueue_s* rq,
88         struct re_s* entry
89 )
90 {
91         LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
92 }
93
94 int
95 ldap_pvt_runqueue_isrunning(
96         struct runqueue_s* rq,
97         struct re_s* entry
98 )
99 {
100         struct re_s* e;
101
102         LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
103                 if ( e == entry ) {
104                         return 1;
105                 }
106         }
107         return 0;
108 }
109
110 void 
111 ldap_pvt_runqueue_resched(
112         struct runqueue_s* rq,
113         struct re_s* entry
114 )
115 {
116         struct re_s* prev;
117         struct re_s* e;
118
119         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
120                 if ( e == entry )
121                         break;
122         }
123
124         assert ( e == entry );
125
126         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
127
128         entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
129         if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
130                 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
131         } else {
132                 prev = NULL;
133                 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
134                         if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
135                                 if ( prev == NULL ) {
136                                         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
137                                 } else {
138                                         LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
139                                 }
140                         }
141                         prev = e;
142                 }
143         }
144 }
145
146 #endif