No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap_r / rq.c
blob8ce405478470fbdc7e06e3537c160efcdf94cd33
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.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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.
20 #include "portable.h"
22 #include <stdio.h>
24 #include <ac/stdarg.h>
25 #include <ac/stdlib.h>
26 #include <ac/errno.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
31 #include "ldap-int.h"
32 #include "ldap_pvt_thread.h"
33 #include "ldap_queue.h"
34 #include "ldap_rq.h"
36 struct re_s *
37 ldap_pvt_runqueue_insert(
38 struct runqueue_s* rq,
39 time_t interval,
40 ldap_pvt_thread_start_t *routine,
41 void *arg,
42 char *tname,
43 char *tspec
46 struct re_s* entry;
48 entry = (struct re_s *) LDAP_CALLOC( 1, sizeof( struct re_s ));
49 if ( entry ) {
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;
55 entry->arg = arg;
56 entry->tname = tname;
57 entry->tspec = tspec;
58 LDAP_STAILQ_INSERT_HEAD( &rq->task_list, entry, tnext );
60 return entry;
63 struct re_s *
64 ldap_pvt_runqueue_find(
65 struct runqueue_s *rq,
66 ldap_pvt_thread_start_t *routine,
67 void *arg
70 struct re_s* e;
72 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
73 if ( e->routine == routine && e->arg == arg )
74 return e;
76 return NULL;
79 void
80 ldap_pvt_runqueue_remove(
81 struct runqueue_s* rq,
82 struct re_s* entry
85 struct re_s* e;
87 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
88 if ( e == entry)
89 break;
92 assert( e == entry );
94 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
96 LDAP_FREE( entry );
99 struct re_s*
100 ldap_pvt_runqueue_next_sched(
101 struct runqueue_s* rq,
102 struct timeval* next_run
105 struct re_s* entry;
107 entry = LDAP_STAILQ_FIRST( &rq->task_list );
108 if ( entry == NULL || entry->next_sched.tv_sec == 0 ) {
109 return NULL;
110 } else {
111 *next_run = entry->next_sched;
112 return entry;
116 void
117 ldap_pvt_runqueue_runtask(
118 struct runqueue_s* rq,
119 struct re_s* entry
122 LDAP_STAILQ_INSERT_TAIL( &rq->run_list, entry, rnext );
125 void
126 ldap_pvt_runqueue_stoptask(
127 struct runqueue_s* rq,
128 struct re_s* entry
131 LDAP_STAILQ_REMOVE( &rq->run_list, entry, re_s, rnext );
135 ldap_pvt_runqueue_isrunning(
136 struct runqueue_s* rq,
137 struct re_s* entry
140 struct re_s* e;
142 LDAP_STAILQ_FOREACH( e, &rq->run_list, rnext ) {
143 if ( e == entry ) {
144 return 1;
147 return 0;
150 void
151 ldap_pvt_runqueue_resched(
152 struct runqueue_s* rq,
153 struct re_s* entry,
154 int defer
157 struct re_s* prev;
158 struct re_s* e;
160 LDAP_STAILQ_FOREACH( e, &rq->task_list, tnext ) {
161 if ( e == entry )
162 break;
165 assert ( e == entry );
167 LDAP_STAILQ_REMOVE( &rq->task_list, entry, re_s, tnext );
169 if ( !defer ) {
170 entry->next_sched.tv_sec = time( NULL ) + entry->interval.tv_sec;
171 } else {
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 );
179 } else {
180 prev = NULL;
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 );
185 } else {
186 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
188 return;
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 );
192 } else {
193 LDAP_STAILQ_INSERT_AFTER( &rq->task_list, prev, entry, tnext );
195 return;
197 prev = e;
199 LDAP_STAILQ_INSERT_TAIL( &rq->task_list, entry, tnext );
204 ldap_pvt_runqueue_persistent_backload(
205 struct runqueue_s* rq
208 struct re_s* e;
209 int count = 0;
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 )
215 count++;
218 ldap_pvt_thread_mutex_unlock( &rq->rq_mutex );
219 return count;