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"
22 ldap_pvt_runqueue_insert(
23 struct runqueue_s* rq,
25 ldap_pvt_thread_start_t *routine,
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;
38 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
42 ldap_pvt_runqueue_remove(
43 struct runqueue_s* rq,
49 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
54 assert ( e == entry );
56 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
63 ldap_pvt_runqueue_next_sched(
64 struct runqueue_s* rq,
65 struct timeval** next_run
70 entry = LDAP_STAILQ_FIRST( &rq->task_list );
71 if ( entry == NULL ) {
74 } else if ( entry->next_sched.tv_sec == 0 ) {
78 *next_run = &entry->next_sched;
84 ldap_pvt_runqueue_runtask(
85 struct runqueue_s* rq,
89 LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
93 ldap_pvt_runqueue_stoptask(
94 struct runqueue_s* rq,
98 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
102 ldap_pvt_runqueue_isrunning(
103 struct runqueue_s* rq,
109 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
118 ldap_pvt_runqueue_resched(
119 struct runqueue_s* rq,
126 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
131 assert ( e == entry );
133 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
135 if ( entry->interval.tv_sec ) {
136 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
138 entry->next_sched.tv_sec = 0;
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 );
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 );
152 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
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 );
159 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
169 ldap_pvt_runqueue_persistent_backload(
170 struct runqueue_s* rq
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 )