summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/replication/repl_objset.c
blob: 519cb69aab76275fa42d0b81f00a8e09652c54df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/* repl_objset.c */
/*
 *  Support for lifetime management of sets of objects.
 *  Objects are refcounted. NOTE: this API is deprecated.
 *  Use the object/objset API provided by libslapd.
 */

#include "slapi-plugin.h"
#include "slapi-private.h"
#include "repl_objset.h"
#include <prlock.h>

#define REPL_OBJSET_OBJ_FLAG_DELETED	0x1


typedef struct repl_objset_object
{
	void *data; /* pointer to actual node data */
	char *key; /* key for this object. null-terminated string */
	int refcnt; /* reference count for this object */
	unsigned long flags; /* state of this object */
} Repl_Objset_object;

typedef struct repl_objset
{
	LList *objects;
	FNFree destructor; /* destructor for objects - provided by caller */
	PRLock *lock;
} repl_objset;


/* Forward declarations */
static void removeObjectNolock(Repl_Objset *o, Repl_Objset_object *co);
static Repl_Objset_object *removeCurrentObjectAndGetNextNolock (Repl_Objset *o, 
	Repl_Objset_object *co, void *iterator);

/*
 * Create a new set.
 *
 * Arguments:
 *  destructor: a function to be called when an object is to be destroyed
 *
 * Returns:
 *  A pointer to the object set, or NULL if an error occured.
 */
Repl_Objset *
repl_objset_new(FNFree destructor)
{
	Repl_Objset *p;

	p = (Repl_Objset *)slapi_ch_malloc(sizeof(Repl_Objset));
	p->lock = PR_NewLock();
	if (NULL == p->lock)
	{
		free(p); p = NULL;
	}
	p->objects = llistNew();
	p->destructor = destructor;
	return p;
}


/*
 * Destroy a Repl_Objset.
 * Arguments:
 *  o: the object set to be destroyed
 *  maxwait: the maximum time to wait for all object refcnts to 
 *           go to zero.
 *  panic_fn: a function to  be called if, after waiting "maxwait"
 *            seconds, not all object refcnts are zero.
 * The caller must ensure that no one else holds references to the
 * set or any objects it contains.
 */
void
repl_objset_destroy(Repl_Objset **o, time_t maxwait, FNFree panic_fn)
{
	Repl_Objset_object *co = NULL;
	time_t now, stop_time;
	int really_gone;
	int loopcount;
	void *cookie;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != *o);

	time(&now);
	stop_time = now + maxwait;

	/*
	 * Loop over the objects until they all are actually gone,
	 * or until maxwait seconds have passed.
	 */
	really_gone = 0;
	loopcount = 0;

	while (now < stop_time)
	{
		void *cookie;

		PR_Lock((*o)->lock);

		if ((co = llistGetFirst((*o)->objects, &cookie)) == NULL)
		{
			really_gone = 1;
			PR_Unlock((*o)->lock);
			break;
		}
		while (NULL != co)
		{
			/* Set the deleted flag so the object isn't returned by iterator */
			co->flags |= REPL_OBJSET_OBJ_FLAG_DELETED;
			if (0 == co->refcnt)
			{
				/* Remove the object */
				co = removeCurrentObjectAndGetNextNolock ((*o), co, cookie);
					
			}
			else
				co = llistGetNext((*o)->objects, &cookie);
		}
		PR_Unlock((*o)->lock);
		time(&now);
		if (loopcount > 0)
		{
			DS_Sleep(PR_TicksPerSecond());
		}
		loopcount++;
	}

	if (!really_gone)
	{
		if (NULL != panic_fn)
		{
			/*
			 * Call the "aargh, this thing won't go away" panic
			 * function for each remaining object.
			 */
			PR_Lock((*o)->lock);
			if ((co = llistGetFirst((*o)->objects, &cookie)) == NULL)
			{
				panic_fn(co->data);
				while (NULL != co)
				{
					panic_fn(co->data);
					co = llistGetNext((*o)->objects, &cookie);
				}
			}
			PR_Unlock((*o)->lock);
		}
	}
	else
	{
		/* Free the linked list */
		llistDestroy(&(*o)->objects, (*o)->destructor);
		PR_DestroyLock((*o)->lock);
		free(*o); *o = NULL;
	}
}



/*
 * Add an object to an object set.
 *
 * Arguments:
 *  o: The object set to which the object is to be added.
 *  name: a null-terminated string that names the object. Must
 *  be unique.
 *  obj: pointer to the object to be added.
 * 
 * Return codes:
 *  REPL_OBJSET_SUCCESS: the item was added to the object set
 *  REPL_OBJSET_DUPLICATE_KEY: an item with the same key is already
 *  in the object set.
 *  REPL_OBJSET_INTERNAL_ERROR: something bad happened.
 */
int
repl_objset_add(Repl_Objset *o, const char *name, void *obj)
{
	Repl_Objset_object *co = NULL;
	Repl_Objset_object *tmp = NULL;
	int rc = REPL_OBJSET_SUCCESS;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != name);
	PR_ASSERT(NULL != obj);

	PR_Lock(o->lock);
	tmp = llistGet(o->objects, name);
	if (NULL != tmp)
	{
		rc = REPL_OBJSET_DUPLICATE_KEY;
		goto loser;
	}
	co = (Repl_Objset_object *)slapi_ch_malloc(sizeof(Repl_Objset_object));
	co->data = obj;
	co->key = slapi_ch_strdup(name);
	co->refcnt = 0;
	co->flags = 0UL;
	if (llistInsertHead(o->objects, name, co) != 0)
	{
		rc = REPL_OBJSET_INTERNAL_ERROR;
		goto loser;
	}
	PR_Unlock(o->lock);
	return rc;

loser:
	PR_Unlock(o->lock);
	if (NULL != co)
	{
		if (NULL != co->key)
		{
			slapi_ch_free((void **)&co->key);
		}
		slapi_ch_free((void **)&co);
	}
	return rc;
}


/* Must be called with the repl_objset locked */
static void
removeObjectNolock(Repl_Objset *o, Repl_Objset_object *co)
{
	/* Remove from list */
	llistRemove(o->objects, co->key);
	/* Destroy the object */
	o->destructor(&(co->data));
	free(co->key);
	/* Deallocate the container */
	free(co);
}

static Repl_Objset_object *
removeCurrentObjectAndGetNextNolock (Repl_Objset *o, Repl_Objset_object *co, void *iterator)
{
	Repl_Objset_object *ro;

	PR_ASSERT (o);
	PR_ASSERT (co);
	PR_ASSERT (iterator);

	ro = llistRemoveCurrentAndGetNext (o->objects, &iterator);

	o->destructor(&(co->data));
	free(co->key);
	/* Deallocate the container */
	free(co);

	return ro;	
}

/* Must be called with the repl_objset locked */
static void
acquireNoLock(Repl_Objset_object *co)
{
	co->refcnt++;
}


/* Must be called with the repl_objset locked */
static void
releaseNoLock(Repl_Objset *o, Repl_Objset_object *co)
{
	PR_ASSERT(co->refcnt >= 1);
	if (--co->refcnt == 0)
	{
		if (co->flags & REPL_OBJSET_OBJ_FLAG_DELETED)
		{
			/* Remove the object */
			removeObjectNolock(o, co);
		}
	}
}

/*
 * Retrieve an object from the object set. If an object with
 * the given key is found, its reference count is incremented,
 * a pointer to the object is returned, and a handle to use
 * to refer to the object is returned.
 *
 * Arguments:
 *  o: The object set to be searched.
 *  key: key of the object to be retrieved
 *  obj: pointer to void * that will be set to point to the
 *       object, if found.
 *  handle: pointer to void * that will be set to point to a
 *       handle, used to refer to the object, if found.
 *
 * Returns:
 *  REPL_OBJSET_SUCCESS: an item was found.
 *  REPL_OBJSET_KEY_NOT_FOUND: no item with the given key was found.
 */
int
repl_objset_acquire(Repl_Objset *o, const char *key, void **obj, void **handle)
{
	Repl_Objset_object *co = NULL;
	int rc = REPL_OBJSET_KEY_NOT_FOUND;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != key);
	PR_ASSERT(NULL != obj);
	PR_ASSERT(NULL != handle);

	PR_Lock(o->lock);
	co = llistGet(o->objects, key);
	if (NULL != co && !(co->flags & REPL_OBJSET_OBJ_FLAG_DELETED))
	{
		acquireNoLock(co);
		*obj = (void *)co->data;
		*handle = (void *)co;
		rc = REPL_OBJSET_SUCCESS;
	}
	PR_Unlock(o->lock);
	return rc;
}


/*
 * Return an object to the object set.
 *
 * Arguments:
 *  o: The object set containing the objct
 *  handle: reference to the object.
 *
 */
void
repl_objset_release(Repl_Objset *o, void *handle)
{
	Repl_Objset_object *co;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != handle);

	co = (Repl_Objset_object *)handle;
	PR_Lock(o->lock);
	releaseNoLock(o, co);
	PR_Unlock(o->lock);
}



/*
 * Delete an object from the object set
 *
 * Arguments:
 *  o: The object set containing the object.
 *  handle: reference to the object.
 */
void
repl_objset_delete(Repl_Objset *o, void *handle)
{
	Repl_Objset_object *co = (Repl_Objset_object *)handle;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != co);

	PR_Lock(o->lock);
	if (co->refcnt == 0)
	{
		removeObjectNolock(o, co);
	}
	else
	{
		/* Set deleted flag, clean up later */
		co->flags |= REPL_OBJSET_OBJ_FLAG_DELETED;
	}
	PR_Unlock(o->lock);
}


typedef struct _iterator
{
	Repl_Objset *o;			/* set for which iterator was created */
	void *cookie;			/* for linked list */
	Repl_Objset_object	*co;	/* our wrapper */
} iterator;

/*
 * Get the first object in an object set.
 * Used when enumerating all of the objects in a set.
 * Arguments:
 *  o: The object set being enumerated
 *  itcontext: an iteration context, to be passed back to subsequent calls
 *          to repl_objset_next_object.
 *  handle: a pointer to pointer to void. This will be filled in with
 *          a reference to the object's enclosing object.
 * Returns:
 *  A pointer to the next object in the set, or NULL if there are no
 *  objects in the set.
 * 
 */
void *
repl_objset_first_object(Repl_Objset *o, void **itcontext, void **handle)
{
	Repl_Objset_object *co = NULL;
	void *cookie;
	void *retptr = NULL;
	iterator *it;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != itcontext);

	*itcontext = NULL;

	if (NULL == o->objects) {
		return(NULL);
	}

	/* Find the first non-deleted object */
	PR_Lock(o->lock);
	co = llistGetFirst(o->objects, &cookie);
	while (NULL != co && (co->flags & REPL_OBJSET_OBJ_FLAG_DELETED))
	{
		co = llistGetNext(o->objects, &cookie);
	}

	if (NULL != co)
	{
		/* Increment refcnt until item given back to us */
		acquireNoLock(co);

		/* Save away context */
		it = (iterator *)slapi_ch_malloc(sizeof(iterator));
		*itcontext = it;
		it->o = o;
		it->cookie = cookie;
		it->co = co;
		retptr = co->data;
	}

	PR_Unlock(o->lock);
	if (NULL != handle)
	{
		*handle = co;
	}

	return retptr;
}



/*
 * Get the next object in the set.
 * Arguments:
 *  o: The object set being enumerated
 *  itcontext: an iteration context, to be passed back to subsequent calls
 *          to repl_objset_next_object.
 *  handle: a pointer to pointer to void. This will be filled in with
 *          a reference to the object's enclosing object.
 *
 * Returns:
 *  A pointer to the next object in the set, or NULL if there are no more
 *  objects.
 */
void *
repl_objset_next_object(Repl_Objset *o, void *itcontext, void **handle)
{
	Repl_Objset_object *co = NULL;
	Repl_Objset_object *tmp_co;
	void *retptr = NULL;
	iterator *it = (iterator *)itcontext;

	PR_ASSERT(NULL != o);
	PR_ASSERT(NULL != it);
	PR_ASSERT(NULL != it->co);

	PR_Lock(o->lock);
	tmp_co = it->co;

	/* Find the next non-deleted object */
	while ((co = llistGetNext(o->objects, &it->cookie)) != NULL &&
			!(co->flags & REPL_OBJSET_OBJ_FLAG_DELETED));

	if (NULL != co)
	{
		acquireNoLock(co);
		it->co = co;
		retptr = co->data;
	}
	else
	{
		/*
		 * No more non-deleted objects - erase context (freeing
		 * it is responsibility of caller.
		 */ 
		it->cookie = NULL;
		it->co = NULL;
	}
	releaseNoLock(o, tmp_co);
	PR_Unlock(o->lock);
	if (NULL != handle)
	{
		*handle = co;
	}
	return retptr;
}



/*
 * Destroy an itcontext iterator
 */
void
repl_objset_iterator_destroy(void **itcontext)
{	
	if (NULL != itcontext && NULL != *itcontext)
	{
		/* check if we did not iterate through the entire list
		   and need to release last accessed element */
		iterator *it = *(iterator**)itcontext;
		if (it->co)
			repl_objset_release (it->o, it->co);
		
		slapi_ch_free((void **)itcontext);
	}
}