2012-07-22 18:01:12 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-03-19 09:27:28 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2015.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
textbuffer.cpp
|
|
|
|
Provides a indexable text buffer for used by text mode terminals.
|
2012-07-22 18:01:12 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <assert.h>
|
2015-03-19 09:27:28 -04:00
|
|
|
#include <errno.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
|
|
|
|
#include <sortix/kernel/kernel.h>
|
2012-07-22 18:01:12 -04:00
|
|
|
#include <sortix/kernel/kthread.h>
|
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include <sortix/kernel/textbuffer.h>
|
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
|
2015-03-19 09:27:28 -04:00
|
|
|
TextBufferHandle::TextBufferHandle(TextBuffer* textbuf)
|
2012-07-22 18:01:12 -04:00
|
|
|
{
|
|
|
|
this->textbuf = textbuf;
|
|
|
|
this->numused = 0;
|
|
|
|
this->mutex = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
this->unusedcond = KTHREAD_COND_INITIALIZER;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBufferHandle::~TextBufferHandle()
|
|
|
|
{
|
2015-03-19 09:27:28 -04:00
|
|
|
delete textbuf;
|
2012-07-22 18:01:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextBuffer* TextBufferHandle::Acquire()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&mutex);
|
2015-03-19 09:27:28 -04:00
|
|
|
if ( !textbuf )
|
|
|
|
return errno = EINIT, (TextBuffer*) NULL;
|
2012-07-22 18:01:12 -04:00
|
|
|
numused++;
|
2015-03-19 09:27:28 -04:00
|
|
|
return textbuf;
|
2012-07-22 18:01:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBufferHandle::Release(TextBuffer* textbuf)
|
|
|
|
{
|
2012-09-22 08:57:20 -04:00
|
|
|
assert(textbuf);
|
2012-07-22 18:01:12 -04:00
|
|
|
ScopedLock lock(&mutex);
|
2012-09-22 08:57:20 -04:00
|
|
|
assert(numused);
|
2015-03-19 09:27:28 -04:00
|
|
|
numused--;
|
|
|
|
if ( numused == 0 )
|
2012-07-22 18:01:12 -04:00
|
|
|
kthread_cond_signal(&unusedcond);
|
|
|
|
}
|
|
|
|
|
2013-11-11 16:21:56 -05:00
|
|
|
bool TextBufferHandle::EmergencyIsImpaired()
|
|
|
|
{
|
|
|
|
if ( !kthread_mutex_trylock(&mutex) )
|
|
|
|
return true;
|
|
|
|
kthread_mutex_unlock(&mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextBufferHandle::EmergencyRecoup()
|
|
|
|
{
|
|
|
|
if ( !EmergencyIsImpaired() )
|
|
|
|
return true;
|
|
|
|
mutex = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBufferHandle::EmergencyReset()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBuffer* TextBufferHandle::EmergencyAcquire()
|
|
|
|
{
|
|
|
|
// This is during a kernel emergency where preemption has been disabled and
|
|
|
|
// this is the only thread running.
|
2015-03-19 09:27:28 -04:00
|
|
|
return textbuf;
|
2013-11-11 16:21:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextBufferHandle::EmergencyRelease(TextBuffer* textbuf)
|
|
|
|
{
|
|
|
|
// This is during a kernel emergency where preemption has been disabled and
|
|
|
|
// this is the only thread running. We don't maintain the reference count
|
|
|
|
// during this state, so this is a no-operation.
|
|
|
|
(void) textbuf;
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:27:28 -04:00
|
|
|
void TextBufferHandle::Replace(TextBuffer* newtextbuf)
|
2012-07-22 18:01:12 -04:00
|
|
|
{
|
|
|
|
ScopedLock lock(&mutex);
|
2015-03-19 09:27:28 -04:00
|
|
|
while ( 0 < numused )
|
2012-07-22 18:01:12 -04:00
|
|
|
kthread_cond_wait(&unusedcond, &mutex);
|
2015-03-19 09:27:28 -04:00
|
|
|
delete textbuf;
|
|
|
|
textbuf = newtextbuf;
|
2012-07-22 18:01:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sortix
|