File: msgqueue.c

package info (click to toggle)
gross 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,664 kB
  • sloc: sh: 8,835; ansic: 7,330; makefile: 53; xml: 45
file content (751 lines) | stat: -rw-r--r-- 14,797 bytes parent folder | download
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* $Id$ */

/*
 * Copyright (c) 2006, 2007, 2008
 *               Eino Tuominen <eino@utu.fi>
 *               Antti Siira <antti@utu.fi>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This file implements a message queue environment. First, you must initialize
 * queues by calling queue_init(). Then, use get_queue() to get a message queue id,
 * and then put_msg() to add messages to the created queue and get_msg() to
 * receive messages from the queue. All functions are re-entrant and thread safe.
 */

#include "common.h"
#include "msgqueue.h"
#include "srvutils.h"
#include "utils.h"

#define GLOBAL_QUEUE_LOCK { assert(pthread_mutex_lock(&global_queue_lk) == 0); }
#define GLOBAL_QUEUE_UNLOCK { pthread_mutex_unlock(&global_queue_lk); }

/* prototypes of internals */
msgqueue_t *queuebyid(int msqid);
void *delay(void *arg);
int put_msg_raw(msgqueue_t *mq, msg_t *msg);
msg_t *get_msg_raw(msgqueue_t *mq, mseconds_t timeout);
int set_delay_status(int msqid, int state);
void queue_realloc(void);
msgqueue_t *try_available(void);
struct timespec *peek_msg_timestamp(msgqueue_t *mq);

/* array of queues */
msgqueue_t **queues;
msgqueue_t *metaqueue;
int queuespace = 1;
int numqueues = 0;
bool initialized = false;

pthread_mutex_t global_queue_lk = PTHREAD_MUTEX_INITIALIZER;

/*
 *  queue_realloc	- doubles the space reservation for message queues
 *  			  not thread safe, the caller MUST hold GLOBAL_QUEUE_LOCK
 */
void
queue_realloc(void)
{
	size_t queuesize;

	/* queues must be initialized first */
	if (queuespace == 0)
		queuespace = 1;

	logstr(GLOG_DEBUG, "doubling the space for message queues from %d to %d", queuespace, queuespace * 2);

	queuesize = queuespace * sizeof(msgqueue_t *);

	/* double the size of the array */
	queues = realloc(queues, queuesize * 2);
	queuespace *= 2;
}


/*
 * queuebyid	- returns pointer to the queue referred by queue id
 */
msgqueue_t *
queuebyid(int msqid)
{
	msgqueue_t *mq;

	/*
	 * we must lock the mutex because queues array might be replaced 
	 */
	GLOBAL_QUEUE_LOCK;

	mq = queues[msqid];

	GLOBAL_QUEUE_UNLOCK;

	return mq;
}

/* 
 * get_delay_queue	- Builds up a virtual message queue that
 * imposes a constant delay to message deliveries. Delay queue 
 * consists of two message queues and a handler thread 
 */
int
get_delay_queue(const struct timespec *ts)
{
	int putqid, getqid;
	queue_info_t *queue_info;
	int *impose_delay;

	if (!ts) {
		errno = EINVAL;
		return -1;
	}

	queue_info = Malloc(sizeof(queue_info_t));

	putqid = get_queue();	/* for put_msg() */
	getqid = get_queue();	/* for get_msg() */

	impose_delay = Malloc(sizeof(int));

	*impose_delay = 1;

	queue_info->inq = queuebyid(putqid);
	assert(queue_info->inq != NULL);
	queue_info->inq->delay_ts = ts;
	queue_info->inq->impose_delay = impose_delay;
	queue_info->outq = queuebyid(getqid);
	assert(queue_info->outq != NULL);
	queue_info->outq->delay_ts = ts;
	queue_info->outq->impose_delay = impose_delay;

	queue_info->inq->delaypair = queue_info->outq;
	queue_info->outq->delaypair = queue_info->inq;

	create_thread(NULL, DETACH, &delay, (void *)queue_info);

	return putqid;
}

void *
delay(void *arg)
{
	queue_info_t *queue_info;
	msg_t *msg;
	struct timespec sleeptime, reftime, sleepleft, now, *msgtimestamp;
	int ret;

	logstr(GLOG_DEBUG, "delay queue manager thread starting");

	queue_info = (queue_info_t *)arg;

	for (;;) {
		logstr(GLOG_INSANE, "waiting for messages");

		msgtimestamp = peek_msg_timestamp(queue_info->inq);

		if (msgtimestamp && *queue_info->inq->impose_delay &&
		    queue_info->inq->delay_ts &&
		    (queue_info->inq->delay_ts->tv_sec || queue_info->inq->delay_ts->tv_nsec)) {
			clock_gettime(CLOCK_TYPE, &now);
			ts_sum(&reftime, msgtimestamp, queue_info->inq->delay_ts);
			ret = ts_diff(&sleeptime, &reftime, &now);

			if (ret == 0) {
				/* we enter here only if reftime is in the future */
				do {
					logstr(GLOG_INSANE, "reftime in future, sleeping for %d.%d seconds",
					    sleeptime.tv_sec, sleeptime.tv_nsec);
					ret = nanosleep(&sleeptime, &sleepleft);
					if (ret) {
						/* sleep was interrupted */
						sleeptime.tv_sec = sleepleft.tv_sec;
						sleeptime.tv_nsec = sleepleft.tv_nsec;
					}
				} while (ret);
			}
		}
		msg = get_msg_raw(queue_info->inq, 0);
		assert(msg->next == NULL);
		logstr(GLOG_INSANE, "passing message from inq to outq");
		put_msg_raw(queue_info->outq, msg);
	}
}

msgqueue_t *
create_queue(void)
{
	msgqueue_t *mq;

	mq = Malloc(sizeof(msgqueue_t));
	memset(mq, 0, sizeof(msgqueue_t));
	pthread_cond_init(&mq->cv, NULL);
	pthread_mutex_init(&mq->mx, NULL);

	return mq;
}


/*
 * get_queue    - returns a new quqeue
 * First it tries to get a free queue from the queue of the free queues (metaqueue)
 * If there are not any, we create a new one. If there are no space for new queues,
 * we call queue_realloc to double the space
 */
int
get_queue(void)
{
	int i;
	msgqueue_t *mq;

	GLOBAL_QUEUE_LOCK;
	if (initialized == false) {
		/* this is the first call */
		queues = calloc(queuespace, sizeof(msgqueue_t *));

		metaqueue = create_queue();
		metaqueue->active = true;

		initialized = true;
	}

	/* first try to get an available queue from the metaqueue */

	mq = try_available();
	if (mq) {
		/* found one, so let's use it */
		i = mq->id;
	} else {
		/* must create a new queue */

		i = numqueues;
		++numqueues;

		mq = create_queue();
		mq->id = i;
		mq->active = true;

		if (numqueues > queuespace) {
			/* there is no space left in the array */
			queue_realloc();
		}

		queues[i] = mq;
	}

	GLOBAL_QUEUE_UNLOCK;

	return i;
}

int
disable_delay(int msqid)
{
	return set_delay_status(msqid, 0);
}

int
enable_delay(int msqid)
{
	return set_delay_status(msqid, 1);
}

int
set_delay_status(int msqid, int state)
{
	msgqueue_t *mq;
	int ret;

	mq = queuebyid(msqid);
	if (!mq) {
		errno = EINVAL;
		return -1;
	}

	if (!mq->delaypair) {
		errno = EINVAL;
		return -1;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);
	*mq->impose_delay = state;
	ret = pthread_mutex_unlock(&mq->mx);
	assert(ret == 0);

	return 0;
}

/*
 * queue_thaw	- release locks from the queue
 */
int
queue_thaw(int msqid)
{
	msgqueue_t *mq;
	int ret;

	mq = queuebyid(msqid);
	if (!mq) {
		errno = EINVAL;
		return -1;
	}

	logstr(GLOG_ERROR, "thaw queue %d", msqid);

	ret = pthread_mutex_unlock(&mq->mx);
	assert(ret == 0);
	if (mq->delaypair) {
		ret = pthread_mutex_unlock(&mq->delaypair->mx);
		assert(ret == 0);
	}

	return 0;
}

/*
 * queue_freeze	- hold processing of the queue
 */
int
queue_freeze(int msqid)
{
	msgqueue_t *mq;
	int ret;

	mq = queuebyid(msqid);
	if (!mq) {
		errno = EINVAL;
		return -1;
	}

	logstr(GLOG_ERROR, "freeze queue %d", msqid);

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);
	if (mq->delaypair) {
		ret = pthread_mutex_lock(&mq->delaypair->mx);
		assert(ret == 0);
	}

	return 0;
}



int
set_delay(int msqid, const struct timespec *ts)
{
	msgqueue_t *mq;
	int ret;

	mq = queuebyid(msqid);
	if (!mq) {
		errno = EINVAL;
		return -1;
	}

	if (!mq->delaypair) {
		errno = EINVAL;
		return -1;
	}

	if (!mq->delay_ts) {
		errno = EINVAL;
		return -1;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);
	memcpy((void *)mq->delay_ts, ts, sizeof(struct timespec));
	ret = pthread_mutex_unlock(&mq->mx);
	assert(ret == 0);

	return 0;
}

int
put_msg_raw(msgqueue_t *mq, msg_t *msg)
{
	int ret;
	msg_t *tail;

	if (mq->active == false) {
		logstr(GLOG_ERROR, "message queue is marked inactive");
		return -1;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);

	/* check if there are messages already in the queue */
	if (mq->tail) {
		/* the queue is not empty */
		assert(mq->head);
		tail = mq->tail;
		tail->next = msg;
	} else {
		/* the queue is emtpy */
		assert(mq->head == NULL);
		mq->head = msg;
	}
	mq->tail = msg;
	assert(mq->tail->next == NULL);
	mq->msgcount++;

	pthread_cond_signal(&mq->cv);
	pthread_mutex_unlock(&mq->mx);

	return 0;
}

int
put_msg(int msqid, void *omsgp, size_t msgsz, int msgflg)
{
	msgqueue_t *mq;
	msg_t *new;
	void *msgp;
	int ret;
	size_t truesize;

	mq = queuebyid(msqid);
	assert(mq);

	new = Malloc(sizeof(msg_t));
	/* zero out the message structure */
	memset(new, 0, sizeof(msg_t));

	clock_gettime(CLOCK_TYPE, &new->timestamp);

	/*
	 * msgsize is the length of the message, we must add
	 * the length of the type field (long) also
	 */
	truesize = msgsz + sizeof(long);

	msgp = Malloc(truesize);
	memcpy(msgp, omsgp, truesize);

	new->msgp = msgp;
	new->msgsz = truesize;

	ret = put_msg_raw(mq, new);

	return ret;
}

int
instant_msg(int msqid, void *omsgp, size_t msgsz, int msgflg)
{
	msgqueue_t *mq;
	msg_t *new;
	void *msgp;
	int ret;
	size_t truesize;

	mq = queuebyid(msqid);
	assert(mq);

	if (mq->delaypair != NULL)
		mq = mq->delaypair;
	assert(mq);

	new = Malloc(sizeof(msg_t));
	/* zero out the message structure */
	memset(new, 0, sizeof(msg_t));

	clock_gettime(CLOCK_TYPE, &new->timestamp);

	/*
	 * msgsize is the length of the message, we must add
	 * the length of the type field (long) also
	 */
	truesize = msgsz + sizeof(long);

	msgp = Malloc(truesize);
	memcpy(msgp, omsgp, truesize);

	new->msgp = msgp;
	new->msgsz = truesize;

	ret = put_msg_raw(mq, new);

	return ret;
}

/* 
 * release_queue	- add the queue to the metaqueue. Make sure it's empty
 */
int
release_queue(int msqid)
{
	msg_t *msg;
	int ret;
	msgqueue_t *mq;

	mq = queuebyid(msqid);

	if (mq->delaypair) {
		logstr(GLOG_ERROR, "release_queue: attempt to free a delay queue");
		return -1;
	}

	if (mq->head) {
		logstr(GLOG_INSANE, "release_queue: queue not empty");
		return -1;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);
	mq->active = false;
	ret = pthread_mutex_unlock(&mq->mx);
	assert(ret == 0);

	msg = Malloc(sizeof(msg_t));
	/* zero out the message structure */
	memset(msg, 0, sizeof(msg_t));

	msg->msgp = mq;
	ret = put_msg_raw(metaqueue, msg);
	/* with metaqueue there can no be other return values */
	assert(ret == 0);

	return 0;
}

/*
 * try_available	- tries to fetch a message from the metaqueue
 */
msgqueue_t *
try_available(void)
{
	msg_t *msg;
	msgqueue_t *mq = NULL;

	msg = get_msg_raw(metaqueue, -1);

	if (msg) {
		mq = (msgqueue_t *)msg->msgp;
		mq->active = true;
		Free(msg);
	}

	return mq;
}

/*
 * peek_msg_timestamp - returns pointer to timestamp of the first message in the queue
 */
struct timespec *
peek_msg_timestamp(msgqueue_t *mq)
{
	struct timespec *timestamp;
	int ret;

	if (mq->active == false) {
		logstr(GLOG_ERROR, "get_msg_raw: message queue is marked inactive");
		return NULL;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);

	/* the queue is now empty, wait for messages */
	while (mq->head == NULL)
		pthread_cond_wait(&mq->cv, &mq->mx);

	assert(mq->head);
	assert(mq->tail);

	timestamp = &mq->head->timestamp;

	pthread_mutex_unlock(&mq->mx);

	return timestamp;
}


/* 
 * get_msg_raw	- retuns the first message from the message queue
 */
msg_t *
get_msg_raw(msgqueue_t *mq, mseconds_t timeout)
{
	msg_t *msg;
	int ret;
	struct timespec to;

	if (mq->active == false) {
		logstr(GLOG_ERROR, "get_msg_raw: message queue is marked inactive");
		return NULL;
	}

	ret = pthread_mutex_lock(&mq->mx);
	assert(ret == 0);
	msg = NULL;

	mstotimespec(timeout, &to);

	to.tv_sec += time(NULL);

	if (timeout >= 0) {
		/* the queue is now empty, wait for messages */
		while (mq->head == NULL)
			if (timeout == 0) {
				ret = pthread_cond_wait(&mq->cv, &mq->mx);
			} else {
				ret = pthread_cond_timedwait(&mq->cv, &mq->mx, &to);
				if (ret == ETIMEDOUT)
					break;
			}
	} else {
		/* if timeout < 0, we do not wait for messages */
		if (mq->head == 0) {
			/* there is no message */
			ret = -1;
		}
	}

	if (ret == 0) {
		/* a message has been found on the queue */
		assert(mq->head);
		assert(mq->tail);
		msg = mq->head;
		mq->head = msg->next;
		msg->next = NULL;

		mq->msgcount--;
		if (mq->head == NULL) {
			/* queue now empty */
			assert(mq->tail == msg);
			assert(mq->msgcount == 0);
			mq->tail = NULL;
		}
	}

	pthread_mutex_unlock(&mq->mx);
	return msg;
}

/*
 * get_msg	- Returns the first message from the message queue
 * mimics the semantics of msgrcv().
 */
size_t
get_msg(int msqid, void *msgp, size_t maxsize, int msgflag)
{
	return get_msg_timed(msqid, msgp, maxsize, msgflag, 0);
}

size_t
get_msg_timed(int msqid, void *msgp, size_t maxsize, int msgflag, mseconds_t timeout)
{
	msgqueue_t *mq;
	msg_t *msg;
	size_t msglen;
	size_t msgsize;

	mq = queuebyid(msqid);
	assert(mq);

	if (mq->delaypair)
		msg = get_msg_raw(mq->delaypair, timeout);
	else
		msg = get_msg_raw(mq, timeout);

	/* msg is NULL if timeout occurred */
	if (msg == NULL) {
		msglen = 0;
	} else {
		/* we need just the msgsize without the type field */
		msgsize = msg->msgsz - sizeof(long);

		msglen = (maxsize < msgsize) ? maxsize : msgsize;
		memcpy(msgp, msg->msgp, msglen + sizeof(long));
		Free(msg->msgp);
		Free(msg);
	}

	return msglen;
}

size_t
in_queue_len(int msgid)
{
	msgqueue_t *mq;

	mq = queuebyid(msgid);

	assert(mq);

	return mq->msgcount;
}

size_t
out_queue_len(int msgid)
{
	msgqueue_t *mq;

	mq = queuebyid(msgid);
	assert(mq);

	if (mq->delaypair)
		return mq->delaypair->msgcount;

	return in_queue_len(msgid);
}

int
walk_queue(int msgid, int (*callback) (void *))
{
	msgqueue_t *mq;
	msg_t *msg;
	int ret;

	mq = queuebyid(msgid);
	assert(mq);

	if (mq->active == false) {
		logstr(GLOG_ERROR, "get_msg_raw: message queue is marked inactive");
		return -1;
	}

	if (mq->head) {
		msg = mq->head;
		while (msg) {
			logstr(GLOG_DEBUG, "walk_queue: calling callback function");
			ret = callback(msg->msgp);
			if (ret < 0) {
				logstr(GLOG_ERROR, "walk_queue: callback returned FAILURE");
				return -1;
			}
			msg = msg->next;
		}
	}

	if (mq->delaypair) {
		if (mq->delaypair->head) {
			msg = mq->head;
			while (msg) {
				logstr(GLOG_DEBUG, "walk_queue: calling callback function");
				ret = callback(msg->msgp);
				if (ret < 0) {
					logstr(GLOG_ERROR, "walk_queue: callback returned FAILURE");
					return -1;
				}
				msg = msg->next;
			}
		}
	}
	return 0;
}