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