]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/rq.c
e7ba0486929c41d7733757892904c32caf53c8b2
[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         int defer
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 && !defer ) {
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         ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
179         if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
180                 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
181                         if ( e->next_sched.tv_sec == 0 )
182                                 count++;
183                 }
184         }
185         ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );
186         return count;
187 }
188