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>.
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_remove(
58 struct runqueue_s* rq,
64 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
69 assert ( e == entry );
71 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
78 ldap_pvt_runqueue_next_sched(
79 struct runqueue_s* rq,
80 struct timeval** next_run
85 entry = LDAP_STAILQ_FIRST( &rq->task_list );
86 if ( entry == NULL ) {
89 } else if ( entry->next_sched.tv_sec == 0 ) {
93 *next_run = &entry->next_sched;
99 ldap_pvt_runqueue_runtask(
100 struct runqueue_s* rq,
104 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
108 ldap_pvt_runqueue_stoptask(
109 struct runqueue_s* rq,
113 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
117 ldap_pvt_runqueue_isrunning(
118 struct runqueue_s* rq,
124 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
133 ldap_pvt_runqueue_resched(
134 struct runqueue_s* rq,
142 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
147 assert ( e == entry );
149 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
151 if ( entry->interval.tv_sec && !defer ) {
152 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
154 entry->next_sched.tv_sec = 0;
157 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
158 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
159 } else if ( entry->next_sched.tv_sec == 0 ) {
160 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
163 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
164 if ( e->next_sched.tv_sec == 0 ) {
165 if ( prev == NULL ) {
166 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
168 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
171 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
172 if ( prev == NULL ) {
173 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
175 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
181 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
186 ldap_pvt_runqueue_persistent_backload(
187 struct runqueue_s* rq
193 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
194 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
195 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
196 if ( e->next_sched.tv_sec == 0 )
200 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );