-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCinThread.cpp
211 lines (173 loc) · 5.76 KB
/
CinThread.cpp
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
//==============================================================================
//
// CinThread.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC 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 Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "CinThread.h"
#include <istream>
#include <ostream>
#include "Debug.h"
#include "Duration.h"
#include "FileSystem.h"
#include "FunctionGuard.h"
#include "Restart.h"
#include "Singleton.h"
#include "SysConsole.h"
#include "SysTypes.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
CinThread::CinThread() : Thread(OperationsFaction),
client_(nullptr)
{
Debug::ft("CinThread.ctor");
buff_[0] = NUL;
SetInitialized();
}
//------------------------------------------------------------------------------
CinThread::~CinThread()
{
Debug::ftnt("CinThread.dtor");
}
//------------------------------------------------------------------------------
c_string CinThread::AbbrName() const
{
return "cin";
}
//------------------------------------------------------------------------------
void CinThread::ClearClient(const Thread* client)
{
if(client_ == client) client_ = nullptr;
}
//------------------------------------------------------------------------------
void CinThread::Destroy()
{
Debug::ft("CinThread.Destroy");
Singleton<CinThread>::Destroy();
}
//------------------------------------------------------------------------------
void CinThread::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Thread::Display(stream, prefix, options);
stream << prefix << "buff : " << buff_ << CRLF;
stream << prefix << "client : " << client_ << CRLF;
}
//------------------------------------------------------------------------------
fn_name CinThread_Enter = "CinThread.Enter";
void CinThread::Enter()
{
// Read input in advance and buffer it. If a client is already waiting
// for input, interrupt it immediately. It resumes execution in GetLine,
// which reports the input to the client. Sleep forever. When a client
// finally reads the console, GetLine awakens us to read the next input.
//
while(true)
{
Debug::ft(CinThread_Enter);
auto& source = SysConsole::In();
EnterBlockingOperation(BlockedOnStream, CinThread_Enter);
{
FileSystem::GetLine(source, buff_);
}
ExitBlockingOperation(CinThread_Enter);
// Unless there was an error, sleep after notifying the client that
// input is available. This gives the client time to wake up and
// process the input. If there is no client, sleeping buffers the
// input until a client requests it. Append an endline to the input
// so that we don't see buff_.empty() and immediately put the client
// back to sleep when it requests the input.
//
if(source)
{
buff_.push_back(CRLF);
if(client_ != nullptr) client_->Interrupt(WorkAvailable);
Pause(TIMEOUT_NEVER);
}
else
{
source.clear();
}
}
}
//------------------------------------------------------------------------------
std::streamsize CinThread::GetLine(string& buff)
{
Debug::ft("CinThread.GetLine");
// Do not read from the console during a restart. It blocks a thread,
// which prevents it from exiting.
//
if(Restart::GetStage() != Running) return StreamRestart;
auto client = RunningThread();
// Make sure we're running unpreemptably, which will prevent more than
// one client from being in this code at the same time.
//
FunctionGuard guard(Guard_MakeUnpreemptable);
auto server = Singleton<CinThread>::Instance();
if(server->buff_.empty())
{
// Nothing is buffered. Register the client and put it to sleep;
// the server thread will interrupt it when input is available.
//
if(!server->SetClient(client)) return StreamInUse;
Pause(TIMEOUT_NEVER);
// When we put the client to sleep, another thread could interrupt
// it before console input arrives. To receive input, the client
// must call this function again.
//
if(server->buff_.empty())
{
server->client_ = nullptr;
return StreamInterrupt;
}
}
// Input is available. Copy it to the client's buffer, reset the
// server's data, and awaken the server thread so that it can read
// the next input.
//
buff = server->buff_;
server->buff_.clear();
server->client_ = nullptr;
guard.Release();
server->Interrupt(ResumeExecution);
return buff.size();
}
//------------------------------------------------------------------------------
void CinThread::Patch(sel_t selector, void* arguments)
{
Thread::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
bool CinThread::SetClient(Thread* client)
{
Debug::ft("CinThread.SetClient");
// This succeeds if
// o no client is currently registered
// o CLIENT is already registered
//
if(client_ == nullptr)
{
client_ = client;
return true;
}
return (client_ == client);
}
}