2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2005 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>.
16 /* This work was initially developed by Jong Hyuk Choi for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdarg.h>
25 #include <ac/stdlib.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
32 #include "ldap_pvt_thread.h"
33 #include "ldap_queue.h"
37 ldap_pvt_runqueue_insert(
38 struct runqueue_s* rq,
40 ldap_pvt_thread_start_t *routine,
46 entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
48 entry->interval.tv_sec = interval;
49 entry->interval.tv_usec = 0;
50 entry->next_sched.tv_sec = time( NULL );
51 entry->next_sched.tv_usec = 0;
52 entry->routine = routine;
54 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
60 ldap_pvt_runqueue_find(
61 struct runqueue_s *rq,
62 ldap_pvt_thread_start_t *routine,
68 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
69 if ( e->routine == routine && e->arg == arg )
76 ldap_pvt_runqueue_remove(
77 struct runqueue_s* rq,
83 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
90 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
96 ldap_pvt_runqueue_next_sched(
97 struct runqueue_s* rq,
98 struct timeval** next_run
103 entry = LDAP_STAILQ_FIRST( &rq->task_list );
104 if ( entry == NULL ) {
107 } else if ( entry->next_sched.tv_sec == 0 ) {
111 *next_run = &entry->next_sched;
117 ldap_pvt_runqueue_runtask(
118 struct runqueue_s* rq,
122 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
126 ldap_pvt_runqueue_stoptask(
127 struct runqueue_s* rq,
131 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
135 ldap_pvt_runqueue_isrunning(
136 struct runqueue_s* rq,
142 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
151 ldap_pvt_runqueue_resched(
152 struct runqueue_s* rq,
160 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
165 assert ( e == entry );
167 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
170 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
172 entry->next_sched.tv_sec = 0;
175 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
176 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
177 } else if ( entry->next_sched.tv_sec == 0 ) {
178 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
181 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
182 if ( e->next_sched.tv_sec == 0 ) {
183 if ( prev == NULL ) {
184 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
186 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
189 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
190 if ( prev == NULL ) {
191 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
193 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
199 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
204 ldap_pvt_runqueue_persistent_backload(
205 struct runqueue_s* rq
211 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
212 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
213 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
214 if ( e->next_sched.tv_sec == 0 )
218 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );