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 ));
47 entry->interval.tv_sec = interval;
48 entry->interval.tv_usec = 0;
49 entry->next_sched.tv_sec = time( NULL );
50 entry->next_sched.tv_usec = 0;
51 entry->routine = routine;
53 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
57 ldap_pvt_runqueue_find(
58 struct runqueue_s *rq,
59 ldap_pvt_thread_start_t *routine,
65 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
66 if ( e->routine == routine && e->arg == arg )
73 ldap_pvt_runqueue_remove(
74 struct runqueue_s* rq,
80 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
87 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
93 ldap_pvt_runqueue_next_sched(
94 struct runqueue_s* rq,
95 struct timeval** next_run
100 entry = LDAP_STAILQ_FIRST( &rq->task_list );
101 if ( entry == NULL ) {
104 } else if ( entry->next_sched.tv_sec == 0 ) {
108 *next_run = &entry->next_sched;
114 ldap_pvt_runqueue_runtask(
115 struct runqueue_s* rq,
119 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
123 ldap_pvt_runqueue_stoptask(
124 struct runqueue_s* rq,
128 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
132 ldap_pvt_runqueue_isrunning(
133 struct runqueue_s* rq,
139 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
148 ldap_pvt_runqueue_resched(
149 struct runqueue_s* rq,
157 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
162 assert ( e == entry );
164 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
167 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
169 entry->next_sched.tv_sec = 0;
172 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
173 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
174 } else if ( entry->next_sched.tv_sec == 0 ) {
175 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
178 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
179 if ( e->next_sched.tv_sec == 0 ) {
180 if ( prev == NULL ) {
181 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
183 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
186 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
187 if ( prev == NULL ) {
188 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
190 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
196 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
201 ldap_pvt_runqueue_persistent_backload(
202 struct runqueue_s* rq
208 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
209 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
210 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
211 if ( e->next_sched.tv_sec == 0 )
215 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );