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>
13 #include <ac/socket.h>
14 #include <ac/string.h>
18 #include "ldap_pvt_thread.h"
19 #include "ldap_queue.h"
23 ldap_pvt_runqueue_insert(
24 struct runqueue_s* rq,
26 ldap_pvt_thread_start_t *routine,
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;
39 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
43 ldap_pvt_runqueue_remove(
44 struct runqueue_s* rq,
50 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
55 assert ( e == entry );
57 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
64 ldap_pvt_runqueue_next_sched(
65 struct runqueue_s* rq,
66 struct timeval** next_run
71 entry = LDAP_STAILQ_FIRST( &rq->task_list );
72 if ( entry == NULL ) {
75 } else if ( entry->next_sched.tv_sec == 0 ) {
79 *next_run = &entry->next_sched;
85 ldap_pvt_runqueue_runtask(
86 struct runqueue_s* rq,
90 LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
94 ldap_pvt_runqueue_stoptask(
95 struct runqueue_s* rq,
99 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
103 ldap_pvt_runqueue_isrunning(
104 struct runqueue_s* rq,
110 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
119 ldap_pvt_runqueue_resched(
120 struct runqueue_s* rq,
127 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
132 assert ( e == entry );
134 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
136 if ( entry->interval.tv_sec ) {
137 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
139 entry->next_sched.tv_sec = 0;
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 );
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 );
153 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
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 );
160 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
170 ldap_pvt_runqueue_persistent_backload(
171 struct runqueue_s* rq
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 )