1 /* $OpenLDAP: pkg/ldap/libraries/libldap_r/rq.c,v 1.23.2.4 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2003-2008 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
,
48 entry
= (struct re_s
*) LDAP_CALLOC( 1, sizeof( struct re_s
));
50 entry
->interval
.tv_sec
= interval
;
51 entry
->interval
.tv_usec
= 0;
52 entry
->next_sched
.tv_sec
= time( NULL
);
53 entry
->next_sched
.tv_usec
= 0;
54 entry
->routine
= routine
;
58 LDAP_STAILQ_INSERT_HEAD( &rq
->task_list
, entry
, tnext
);
64 ldap_pvt_runqueue_find(
65 struct runqueue_s
*rq
,
66 ldap_pvt_thread_start_t
*routine
,
72 LDAP_STAILQ_FOREACH( e
, &rq
->task_list
, tnext
) {
73 if ( e
->routine
== routine
&& e
->arg
== arg
)
80 ldap_pvt_runqueue_remove(
81 struct runqueue_s
* rq
,
87 LDAP_STAILQ_FOREACH( e
, &rq
->task_list
, tnext
) {
94 LDAP_STAILQ_REMOVE( &rq
->task_list
, entry
, re_s
, tnext
);
100 ldap_pvt_runqueue_next_sched(
101 struct runqueue_s
* rq
,
102 struct timeval
* next_run
107 entry
= LDAP_STAILQ_FIRST( &rq
->task_list
);
108 if ( entry
== NULL
|| 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
);