aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vbackup/queue.c
blob: d9fcd404536555254d9767db92e46e50f96cdf64 (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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <venti.h>
#include <diskfs.h>
#include "queue.h"

Queue*
qalloc(void)
{
	Queue *q;

	q = vtmallocz(sizeof(Queue));
	q->r.l = &q->lk;
	return q;
}

Block*
qread(Queue *q, u32int *pbno)
{
	Block *db;
	u32int bno;

	qlock(&q->lk);
	while(q->nel == 0 && !q->closed)
		rsleep(&q->r);
	if(q->nel == 0 && q->closed){
		qunlock(&q->lk);
		return nil;
	}
	db = q->el[q->ri].db;
	bno = q->el[q->ri].bno;
	if(++q->ri == MAXQ)
		q->ri = 0;
	if(q->nel-- == MAXQ/2)
		rwakeup(&q->r);
	qunlock(&q->lk);
	*pbno = bno;
	return db;
}

void
qwrite(Queue *q, Block *db, u32int bno)
{
	qlock(&q->lk);
	while(q->nel == MAXQ)
		rsleep(&q->r);
	q->el[q->wi].db = db;
	q->el[q->wi].bno = bno;
	if(++q->wi == MAXQ)
		q->wi = 0;
	if(q->nel++ == MAXQ/2)
		rwakeup(&q->r);
	qunlock(&q->lk);
}

void
qclose(Queue *q)
{
	qlock(&q->lk);
	q->closed = 1;
	rwakeup(&q->r);
	qunlock(&q->lk);
}

void
qfree(Queue *q)
{
	vtfree(q);
}