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,
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 && !defer ) {
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 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 )
185 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );