diff --git a/src/add-ons/kernel/drivers/tty/tty.cpp b/src/add-ons/kernel/drivers/tty/tty.cpp index 86b119eabb..d7c4d6fab9 100644 --- a/src/add-ons/kernel/drivers/tty/tty.cpp +++ b/src/add-ons/kernel/drivers/tty/tty.cpp @@ -85,27 +85,26 @@ static void tty_notify_if_available(struct tty *tty, struct tty *otherTTY, class AbstractLocker { -public: - AbstractLocker(tty_cookie *cookie) : fCookie(cookie), fBytes(0) {} + public: + AbstractLocker(tty_cookie *cookie) : fCookie(cookie), fBytes(0) {} - size_t AvailableBytes() const { return fBytes; } + size_t AvailableBytes() const { return fBytes; } -protected: - void Lock() { mutex_lock(fCookie->tty->lock); } - void Unlock() { mutex_unlock(fCookie->tty->lock); } + protected: + void Lock() { mutex_lock(fCookie->tty->lock); } + void Unlock() { mutex_unlock(fCookie->tty->lock); } - tty_cookie *fCookie; - size_t fBytes; + tty_cookie *fCookie; + size_t fBytes; }; class WriterLocker : public AbstractLocker { public: WriterLocker(tty_cookie *sourceCookie); - ~WriterLocker(); - status_t AcquireWriter(bool dontBlock, int32 bytesNeeded); + status_t AcquireWriter(bool dontBlock, size_t bytesNeeded); private: size_t _CheckAvailableBytes() const; @@ -131,37 +130,36 @@ class ReaderLocker : public AbstractLocker { class TTYReferenceLocking { -public: - inline bool Lock(tty_cookie *cookie) - { - MutexLocker _(gTTYCookieLock); + public: + inline bool Lock(tty_cookie *cookie) + { + MutexLocker _(gTTYCookieLock); - if (cookie->closed || cookie->other_tty->open_count == 0) - return false; + if (cookie->closed || cookie->other_tty->open_count == 0) + return false; - cookie->thread_count++; + cookie->thread_count++; - return true; - } - - inline void Unlock(tty_cookie *cookie) - { - MutexLocker locker(gTTYCookieLock); - - sem_id semaphore = -1; - if (--cookie->thread_count == 0 && cookie->closed) - semaphore = cookie->blocking_semaphore; - - - locker.Unlock(); - - if (semaphore >= 0) { - TRACE(("TTYReference: cookie %p closed, last operation done, " - "releasing blocking sem %ld\n", cookie, semaphore)); - - release_sem(semaphore); + return true; + } + + inline void Unlock(tty_cookie *cookie) + { + MutexLocker locker(gTTYCookieLock); + + sem_id semaphore = -1; + if (--cookie->thread_count == 0 && cookie->closed) + semaphore = cookie->blocking_semaphore; + + locker.Unlock(); + + if (semaphore >= 0) { + TRACE(("TTYReference: cookie %p closed, last operation done, " + "releasing blocking sem %ld\n", cookie, semaphore)); + + release_sem(semaphore); + } } - } }; typedef AutoLocker TTYReference; @@ -169,18 +167,20 @@ typedef AutoLocker TTYReference; // #pragma mark - + Request::Request() - : fOwner(NULL), - fCookie(NULL), - fBytesNeeded(0), - fNotified(false), - fError(false) + : + fOwner(NULL), + fCookie(NULL), + fBytesNeeded(0), + fNotified(false), + fError(false) { } void -Request::Init(RequestOwner *owner, tty_cookie *cookie, int32 bytesNeeded) +Request::Init(RequestOwner *owner, tty_cookie *cookie, size_t bytesNeeded) { fOwner = owner; fCookie = cookie; @@ -191,7 +191,7 @@ Request::Init(RequestOwner *owner, tty_cookie *cookie, int32 bytesNeeded) void -Request::Notify(int32 bytesAvailable) +Request::Notify(size_t bytesAvailable) { if (!fNotified && bytesAvailable >= fBytesNeeded && fOwner) { fOwner->Notify(this); @@ -213,8 +213,10 @@ Request::NotifyError(status_t error) // #pragma mark - + RequestQueue::RequestQueue() - : fRequests() + : + fRequests() { } @@ -242,7 +244,7 @@ RequestQueue::Remove(Request *request) void -RequestQueue::NotifyFirst(int32 bytesAvailable) +RequestQueue::NotifyFirst(size_t bytesAvailable) { RecursiveLocker _(gTTYRequestLock); @@ -276,11 +278,13 @@ RequestQueue::NotifyError(tty_cookie *cookie, status_t error) // #pragma mark - + RequestOwner::RequestOwner() - : fSemaphore(NULL), - fCookie(NULL), - fError(B_OK), - fBytesNeeded(1) + : + fSemaphore(NULL), + fCookie(NULL), + fError(B_OK), + fBytesNeeded(1) { fRequestQueues[0] = NULL; fRequestQueues[1] = NULL; @@ -332,7 +336,7 @@ RequestOwner::Dequeue() void -RequestOwner::SetBytesNeeded(int32 bytesNeeded) +RequestOwner::SetBytesNeeded(size_t bytesNeeded) { if (fRequestQueues[0]) fRequests[0].Init(this, fCookie, bytesNeeded); @@ -455,9 +459,9 @@ RequestOwner::NotifyError(Request *request, status_t error) // #pragma mark - + WriterLocker::WriterLocker(tty_cookie *sourceCookie) - : - AbstractLocker(sourceCookie), + : AbstractLocker(sourceCookie), fSource(fCookie->tty), fTarget(fCookie->other_tty), fRequestOwner(), @@ -522,7 +526,7 @@ WriterLocker::_CheckAvailableBytes() const status_t -WriterLocker::AcquireWriter(bool dontBlock, int32 bytesNeeded) +WriterLocker::AcquireWriter(bool dontBlock, size_t bytesNeeded) { if (!fTarget) return B_FILE_ERROR; @@ -583,8 +587,7 @@ WriterLocker::AcquireWriter(bool dontBlock, int32 bytesNeeded) ReaderLocker::ReaderLocker(tty_cookie *cookie) - : - AbstractLocker(cookie), + : AbstractLocker(cookie), fTTY(cookie->tty), fRequestOwner() { @@ -1219,7 +1222,7 @@ tty_write_to_tty(tty_cookie *sourceCookie, const void *buffer, size_t *_length, // ToDo: "buffer" is not yet copied or accessed in a safe way! - int32 bytesNeeded = 1; + size_t bytesNeeded = 1; while (bytesWritten < length) { status_t status = locker.AcquireWriter(dontBlock, bytesNeeded); diff --git a/src/add-ons/kernel/drivers/tty/tty_private.h b/src/add-ons/kernel/drivers/tty/tty_private.h index dbafe7c9f7..1ea2e476b5 100644 --- a/src/add-ons/kernel/drivers/tty/tty_private.h +++ b/src/add-ons/kernel/drivers/tty/tty_private.h @@ -1,7 +1,7 @@ -/* -** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the Haiku License. -*/ +/* + * Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ #ifndef TTY_PRIVATE_H #define TTY_PRIVATE_H @@ -34,75 +34,75 @@ struct tty; struct tty_cookie; class Request : public DoublyLinkedListLinkImpl { -public: - Request(); + public: + Request(); - void Init(RequestOwner *owner, tty_cookie *cookie, int32 bytesNeeded); + void Init(RequestOwner *owner, tty_cookie *cookie, size_t bytesNeeded); - tty_cookie *TTYCookie() const { return fCookie; } + tty_cookie *TTYCookie() const { return fCookie; } - void Notify(int32 bytesAvailable); - void NotifyError(status_t error); + void Notify(size_t bytesAvailable); + void NotifyError(status_t error); - bool WasNotified() const { return fNotified; } - bool HasError() const { return fError; } + bool WasNotified() const { return fNotified; } + bool HasError() const { return fError; } -private: - RequestOwner *fOwner; - tty_cookie *fCookie; - int32 fBytesNeeded; - bool fNotified; - bool fError; + private: + RequestOwner *fOwner; + tty_cookie *fCookie; + size_t fBytesNeeded; + bool fNotified; + bool fError; }; class RequestQueue { -public: - RequestQueue(); - ~RequestQueue() {} + public: + RequestQueue(); + ~RequestQueue() {} - void Add(Request *request); - void Remove(Request *request); + void Add(Request *request); + void Remove(Request *request); - Request *First() const { return fRequests.First(); } - bool IsEmpty() const { return fRequests.IsEmpty(); } + Request *First() const { return fRequests.First(); } + bool IsEmpty() const { return fRequests.IsEmpty(); } - void NotifyFirst(int32 bytesAvailable); - void NotifyError(status_t error); - void NotifyError(tty_cookie *cookie, status_t error); + void NotifyFirst(size_t bytesAvailable); + void NotifyError(status_t error); + void NotifyError(tty_cookie *cookie, status_t error); -private: - typedef DoublyLinkedList RequestList; + private: + typedef DoublyLinkedList RequestList; - RequestList fRequests; + RequestList fRequests; }; class RequestOwner { -public: - RequestOwner(); + public: + RequestOwner(); - void Enqueue(tty_cookie *cookie, RequestQueue *queue1, - RequestQueue *queue2 = NULL); - void Dequeue(); + void Enqueue(tty_cookie *cookie, RequestQueue *queue1, + RequestQueue *queue2 = NULL); + void Dequeue(); - void SetBytesNeeded(int32 bytesNeeded); - int32 BytesNeeded() const { return fBytesNeeded; } + void SetBytesNeeded(size_t bytesNeeded); + size_t BytesNeeded() const { return fBytesNeeded; } - status_t Wait(bool interruptable, Semaphore *sem = NULL); + status_t Wait(bool interruptable, Semaphore *sem = NULL); - bool IsFirstInQueues(); + bool IsFirstInQueues(); - void Notify(Request *request); - void NotifyError(Request *request, status_t error); + void Notify(Request *request); + void NotifyError(Request *request, status_t error); - status_t Error() const { return fError; } + status_t Error() const { return fError; } -private: - Semaphore *fSemaphore; - tty_cookie *fCookie; - status_t fError; - RequestQueue *fRequestQueues[2]; - Request fRequests[2]; - int32 fBytesNeeded; + private: + Semaphore *fSemaphore; + tty_cookie *fCookie; + status_t fError; + RequestQueue *fRequestQueues[2]; + Request fRequests[2]; + size_t fBytesNeeded; };