3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdarg.h>
11 #include <ac/stdlib.h>
12 #include <ac/string.h>
17 #include "ldap_pvt_thread.h"
18 #include "ldap_queue.h"
24 ldap_pvt_runqueue_insert(
25 struct runqueue_s* rq,
27 ldap_pvt_thread_start_t *routine,
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;
40 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
44 ldap_pvt_runqueue_remove(
45 struct runqueue_s* rq,
51 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
56 assert ( e == entry );
58 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
65 ldap_pvt_runqueue_next_sched(
66 struct runqueue_s* rq,
67 struct timeval** next_run
72 entry = LDAP_STAILQ_FIRST( &rq->task_list );
73 if ( entry == NULL ) {
76 } else if ( entry->next_sched.tv_sec == 0 ) {
80 *next_run = &entry->next_sched;
86 ldap_pvt_runqueue_runtask(
87 struct runqueue_s* rq,
91 LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
95 ldap_pvt_runqueue_stoptask(
96 struct runqueue_s* rq,
100 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
104 ldap_pvt_runqueue_isrunning(
105 struct runqueue_s* rq,
111 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
120 ldap_pvt_runqueue_resched(
121 struct runqueue_s* rq,
128 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
133 assert ( e == entry );
135 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
137 if ( entry->interval.tv_sec ) {
138 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
140 entry->next_sched.tv_sec = 0;
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 );
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 );
154 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
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 );
161 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
171 ldap_pvt_runqueue_persistent_backload(
172 struct runqueue_s* rq
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 )