]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/rq.c
Add/Update various copyright notices
[openldap] / libraries / libldap_r / rq.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdarg.h>
11 #include <ac/stdlib.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14 #include <ac/errno.h>
15
16 #include "ldap-int.h"
17 #include "ldap_pvt_thread.h"
18 #include "ldap_queue.h"
19 #include "ldap_rq.h"
20
21 #ifdef LDAP_SYNCREPL
22
23 void
24 ldap_pvt_runqueue_insert(
25         struct runqueue_s* rq,
26         time_t interval,
27         ldap_pvt_thread_start_t *routine,
28         void *arg
29 )
30 {
31         struct re_s* entry;
32
33         entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
34         entry->interval.tv_sec = interval;
35         entry->interval.tv_usec = 0;
36         entry->next_sched.tv_sec = time( NULL );
37         entry->next_sched.tv_usec = 0;
38         entry->routine = routine;
39         entry->arg = arg;
40         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
41 }
42
43 void
44 ldap_pvt_runqueue_remove(
45         struct runqueue_s* rq,
46         struct re_s* entry
47 )
48 {
49         struct re_s* e;
50
51         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
52                 if ( e == entry)
53                         break;
54         }
55
56         assert ( e == entry );
57
58         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
59
60         LDAP_FREE( entry );
61
62 }
63
64 struct re_s*
65 ldap_pvt_runqueue_next_sched(
66         struct runqueue_s* rq,
67         struct timeval** next_run
68 )
69 {
70         struct re_s* entry;
71
72         entry = LDAP_STAILQ_FIRST( &rq->task_list );
73         if ( entry == NULL ) {
74                 *next_run = NULL;
75                 return NULL;
76         } else if ( entry->next_sched.tv_sec == 0 ) {
77                 *next_run = NULL;
78                 return NULL;
79         } else {
80                 *next_run = &entry->next_sched;
81                 return entry;
82         }
83 }
84
85 void
86 ldap_pvt_runqueue_runtask(
87         struct runqueue_s* rq,
88         struct re_s* entry
89 )
90 {
91         LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
92 }
93
94 void
95 ldap_pvt_runqueue_stoptask(
96         struct runqueue_s* rq,
97         struct re_s* entry
98 )
99 {
100         LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
101 }
102
103 int
104 ldap_pvt_runqueue_isrunning(
105         struct runqueue_s* rq,
106         struct re_s* entry
107 )
108 {
109         struct re_s* e;
110
111         LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
112                 if ( e == entry ) {
113                         return 1;
114                 }
115         }
116         return 0;
117 }
118
119 void 
120 ldap_pvt_runqueue_resched(
121         struct runqueue_s* rq,
122         struct re_s* entry
123 )
124 {
125         struct re_s* prev;
126         struct re_s* e;
127
128         LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
129                 if ( e == entry )
130                         break;
131         }
132
133         assert ( e == entry );
134
135         LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
136
137         if ( entry->interval.tv_sec ) {
138                 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
139         } else {
140                 entry->next_sched.tv_sec = 0;
141         }
142
143         if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
144                 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
145         } else if ( entry->next_sched.tv_sec == 0 ) {
146                 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
147         } else {
148                 prev = NULL;
149                 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
150                         if ( e->next_sched.tv_sec == 0 ) {
151                                 if ( prev == NULL ) {
152                                         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
153                                 } else {
154                                         LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
155                                 }
156                                 break;
157                         } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
158                                 if ( prev == NULL ) {
159                                         LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
160                                 } else {
161                                         LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
162                                 }
163                                 break;
164                         }
165                         prev = e;
166                 }
167         }
168 }
169
170 int
171 ldap_pvt_runqueue_persistent_backload(
172         struct runqueue_s* rq
173 )
174 {
175         struct re_s* e;
176         int count = 0;
177
178         if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
179                 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
180                         if ( e->next_sched.tv_sec == 0 )
181                                 count++;
182                 }
183         }
184         return count;
185 }
186
187 #endif