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