better locking
[dd2d.git] / conbuf.d
blob67c1a3a5f04de05b6784885ccfc6705f2fe23ca5
1 module conbuf is aliced;
2 private:
4 // ////////////////////////////////////////////////////////////////////////// //
5 import core.sync.mutex : Mutex;
6 __gshared Mutex conbufLock;
7 shared static this () { conbufLock = new Mutex(); }
10 // ////////////////////////////////////////////////////////////////////////// //
11 version(test_cbuf)
12 enum ConBufSize = 64;
13 else
14 enum ConBufSize = 256*1024;
16 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
17 // it's fairly easy to search in buffer, and drawing console is not a common
18 // thing, so it doesn't have to be superfast.
19 __gshared char[ConBufSize] cbuf = 0;
20 __gshared int cbufhead, cbuftail; // `cbuftail` points *at* last char
21 shared static this () { cbuf.ptr[0] = '\n'; }
23 shared ulong changeCount = 1;
24 public @property ulong cbufLastChange () nothrow @trusted @nogc { import core.atomic; return atomicLoad(changeCount); }
27 // ////////////////////////////////////////////////////////////////////////// //
28 public void cbufLock() () { pragma(inline, true); conbufLock.lock(); }
29 public void cbufUnlock() () { pragma(inline, true); conbufLock.unlock(); }
32 // ////////////////////////////////////////////////////////////////////////// //
33 public void cbufPut (const(char)[] chrs...) nothrow @trusted @nogc {
34 if (chrs.length) {
35 conbufLock.lock();
36 scope(exit) conbufLock.unlock();
37 import core.atomic;
38 atomicOp!"+="(changeCount, 1);
39 foreach (char ch; chrs) {
40 int np = (cbuftail+1)%ConBufSize;
41 if (np == cbufhead) {
42 // we have to make some room; delete top line for this
43 for (;;) {
44 char och = cbuf.ptr[cbufhead];
45 cbufhead = (cbufhead+1)%ConBufSize;
46 if (cbufhead == np || och == '\n') break;
49 cbuf.ptr[np] = ch;
50 cbuftail = np;
56 // ////////////////////////////////////////////////////////////////////////// //
57 // warning! don't modify conbuf while the range is active!
58 public auto conbufLinesRev () nothrow @trusted @nogc {
59 static struct Line {
60 nothrow @trusted @nogc:
61 private:
62 int h, t; // head and tail, to check validity
63 int sp = -1, ep;
65 public:
66 @property auto front () const { pragma(inline, true); return (sp >= 0 ? cbuf.ptr[sp] : '\x00'); }
67 @property bool empty () const { pragma(inline, true); return (sp < 0 || h != cbufhead || t != cbuftail); }
68 @property auto save () { pragma(inline, true); return Line(h, t, sp, ep); }
69 void popFront () { pragma(inline, true); if (sp < 0 || (sp = (sp+1)%ConBufSize) == ep) sp = -1; }
70 @property usize opDollar () { pragma(inline, true); return (sp >= 0 ? (sp > ep ? ep+ConBufSize-sp : ep-sp) : 0); }
71 alias length = opDollar;
72 char opIndex (usize pos) { pragma(inline, true); return (sp >= 0 ? cbuf.ptr[sp+pos] : '\x00'); }
75 static struct Range {
76 nothrow @trusted @nogc:
77 private:
78 int h, t; // head and tail, to check validity
79 int pos; // position of prev line
80 Line line;
82 void toLineStart () {
83 line.ep = pos;
84 while (pos != cbufhead) {
85 int p = (pos+ConBufSize-1)%ConBufSize;
86 if (cbuf.ptr[p] == '\n') break;
87 pos = p;
89 line.sp = pos;
90 line.h = h;
91 line.t = t;
94 public:
95 @property auto front () pure { pragma(inline, true); return line; }
96 @property bool empty () const { pragma(inline, true); return (pos < 0 || pos == h || h != cbufhead || t != cbuftail); }
97 @property auto save () { pragma(inline, true); return Range(h, t, pos, line); }
98 void popFront () {
99 if (pos < 0 || pos == h || h != cbufhead || t != cbuftail) { line = Line.init; h = t = pos = -1; return; }
100 pos = (pos+ConBufSize-1)%ConBufSize;
101 toLineStart();
105 Range res;
106 res.h = cbufhead;
107 res.pos = res.t = cbuftail;
108 if (cbuf.ptr[res.pos] != '\n') res.pos = (res.pos+1)%ConBufSize;
109 res.toLineStart();
110 //{ import std.stdio; writeln("pos=", res.pos, "; head=", res.h, "; tail=", res.t, "; llen=", res.line.length, "; [", res.line, "]"); }
111 return res;
115 // ////////////////////////////////////////////////////////////////////////// //
116 public void conbufDump () {
117 import std.stdio;
118 int pp = cbufhead;
119 stdout.writeln("==========================");
120 for (;;) {
121 if (cbuf.ptr[pp] == '\n') stdout.write('|');
122 stdout.write(cbuf.ptr[pp]);
123 if (pp == cbuftail) {
124 if (cbuf.ptr[pp] != '\n') stdout.write('\n');
125 break;
127 pp = (pp+1)%ConBufSize;
129 //foreach (auto s; conbufLinesRev) stdout.writeln(s, "|");
133 // ////////////////////////////////////////////////////////////////////////// //
134 version(test_cbuf) unittest {
135 conbufDump();
136 cbufPut("boo\n"); conbufDump();
137 cbufPut("this is another line\n"); conbufDump();
138 cbufPut("one more line\n"); conbufDump();
139 cbufPut("foo\n"); conbufDump();
140 cbufPut("more lines!\n"); conbufDump();
141 cbufPut("and even more lines!\n"); conbufDump();
142 foreach (immutable idx; 0..256) {
143 import std.string : format;
144 cbufPut("line %s\n".format(idx));
145 conbufDump();