2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2004 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 ) {
70 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
77 ldap_pvt_runqueue_next_sched(
78 struct runqueue_s* rq,
79 struct timeval** next_run
84 entry = LDAP_STAILQ_FIRST( &rq->task_list );
85 if ( entry == NULL ) {
88 } else if ( entry->next_sched.tv_sec == 0 ) {
92 *next_run = &entry->next_sched;
98 ldap_pvt_runqueue_runtask(
99 struct runqueue_s* rq,
103 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
107 ldap_pvt_runqueue_stoptask(
108 struct runqueue_s* rq,
112 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
116 ldap_pvt_runqueue_isrunning(
117 struct runqueue_s* rq,
123 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
132 ldap_pvt_runqueue_resched(
133 struct runqueue_s* rq,
141 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
146 assert ( e == entry );
148 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
151 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
153 entry->next_sched.tv_sec = 0;
156 if ( LDAP_STAILQ_EMPTY( &rq->task_list )) {
157 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
158 } else if ( entry->next_sched.tv_sec == 0 ) {
159 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
162 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
163 if ( e->next_sched.tv_sec == 0 ) {
164 if ( prev == NULL ) {
165 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
167 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
170 } else if ( e->next_sched.tv_sec > entry->next_sched.tv_sec ) {
171 if ( prev == NULL ) {
172 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
174 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
180 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
185 ldap_pvt_runqueue_persistent_backload(
186 struct runqueue_s* rq
192 ldap_pvt_thread_mutex_lock( &rq->rq_mutex );
193 if ( !LDAP_STAILQ_EMPTY( &rq->task_list )) {
194 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
195 if ( e->next_sched.tv_sec == 0 )
199 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );