2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/stdarg.h>
22 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 #include "ldap_pvt_thread.h"
30 #include "ldap_queue.h"
34 ldap_pvt_runqueue_insert(
35 struct runqueue_s* rq,
37 ldap_pvt_thread_start_t *routine,
43 entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
44 entry->interval.tv_sec = interval;
45 entry->interval.tv_usec = 0;
46 entry->next_sched.tv_sec = time( NULL );
47 entry->next_sched.tv_usec = 0;
48 entry->routine = routine;
50 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
54 ldap_pvt_runqueue_remove(
55 struct runqueue_s* rq,
61 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
66 assert ( e == entry );
68 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
75 ldap_pvt_runqueue_next_sched(
76 struct runqueue_s* rq,
77 struct timeval** next_run
82 entry = LDAP_STAILQ_FIRST( &rq->task_list );
83 if ( entry == NULL ) {
86 } else if ( entry->next_sched.tv_sec == 0 ) {
90 *next_run = &entry->next_sched;
96 ldap_pvt_runqueue_runtask(
97 struct runqueue_s* rq,
101 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
105 ldap_pvt_runqueue_stoptask(
106 struct runqueue_s* rq,
110 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
114 ldap_pvt_runqueue_isrunning(
115 struct runqueue_s* rq,
121 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
130 ldap_pvt_runqueue_resched(
131 struct runqueue_s* rq,
139 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
144 assert ( e == entry );
146 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
148 if ( entry->interval.tv_sec && !defer ) {
149 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
151 entry->next_sched.tv_sec = 0;
154 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
155 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
156 } else if ( entry->next_sched.tv_sec == 0 ) {
157 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
160 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
161 if ( e->next_sched.tv_sec == 0 ) {
162 if ( prev == NULL ) {
163 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
165 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
168 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
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 );
178 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
183 ldap_pvt_runqueue_persistent_backload(
184 struct runqueue_s* rq
190 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
191 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
192 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
193 if ( e->next_sched.tv_sec == 0 )
197 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );