3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* Copyright (c) 2003 by International Business Machines, Inc.
8 * International Business Machines, Inc. (hereinafter called IBM) grants
9 * permission under its copyrights to use, copy, modify, and distribute this
10 * Software with or without fee, provided that the above copyright notice and
11 * all paragraphs of this notice appear in all copies, and that the name of IBM
12 * not be used in connection with the marketing of any product incorporating
13 * the Software or modifications thereof, without specific, written prior
16 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
19 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
20 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
21 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
28 #include <ac/stdarg.h>
29 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
36 #include "ldap_pvt_thread.h"
37 #include "ldap_queue.h"
41 ldap_pvt_runqueue_insert(
42 struct runqueue_s* rq,
44 ldap_pvt_thread_start_t *routine,
50 entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
51 entry->interval.tv_sec = interval;
52 entry->interval.tv_usec = 0;
53 entry->next_sched.tv_sec = time( NULL );
54 entry->next_sched.tv_usec = 0;
55 entry->routine = routine;
57 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
61 ldap_pvt_runqueue_remove(
62 struct runqueue_s* rq,
68 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
73 assert ( e == entry );
75 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
82 ldap_pvt_runqueue_next_sched(
83 struct runqueue_s* rq,
84 struct timeval** next_run
89 entry = LDAP_STAILQ_FIRST( &rq->task_list );
90 if ( entry == NULL ) {
93 } else if ( entry->next_sched.tv_sec == 0 ) {
97 *next_run = &entry->next_sched;
103 ldap_pvt_runqueue_runtask(
104 struct runqueue_s* rq,
108 LDAP_STAILQ_INSERT_HEAD( &rq->run_list, entry, rnext );
112 ldap_pvt_runqueue_stoptask(
113 struct runqueue_s* rq,
117 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
121 ldap_pvt_runqueue_isrunning(
122 struct runqueue_s* rq,
128 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
137 ldap_pvt_runqueue_resched(
138 struct runqueue_s* rq,
146 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
151 assert ( e == entry );
153 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
155 if ( entry->interval.tv_sec && !defer ) {
156 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
158 entry->next_sched.tv_sec = 0;
161 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
162 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
163 } else if ( entry->next_sched.tv_sec == 0 ) {
164 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
167 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
168 if ( e->next_sched.tv_sec == 0 ) {
169 if ( prev == NULL ) {
170 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
172 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
175 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
176 if ( prev == NULL ) {
177 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
179 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
189 ldap_pvt_runqueue_persistent_backload(
190 struct runqueue_s* rq
196 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
197 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
198 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
199 if ( e->next_sched.tv_sec == 0 )
203 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );