* Changed type of "bytesNeeded" to size_t whereever it's used.

* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17491 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-05-17 14:12:41 +00:00
parent 467a2ae6f8
commit 6632e1f482
2 changed files with 110 additions and 107 deletions
+60 -57
View File
@@ -85,27 +85,26 @@ static void tty_notify_if_available(struct tty *tty, struct tty *otherTTY,
class AbstractLocker { class AbstractLocker {
public: public:
AbstractLocker(tty_cookie *cookie) : fCookie(cookie), fBytes(0) {} AbstractLocker(tty_cookie *cookie) : fCookie(cookie), fBytes(0) {}
size_t AvailableBytes() const { return fBytes; } size_t AvailableBytes() const { return fBytes; }
protected: protected:
void Lock() { mutex_lock(fCookie->tty->lock); } void Lock() { mutex_lock(fCookie->tty->lock); }
void Unlock() { mutex_unlock(fCookie->tty->lock); } void Unlock() { mutex_unlock(fCookie->tty->lock); }
tty_cookie *fCookie; tty_cookie *fCookie;
size_t fBytes; size_t fBytes;
}; };
class WriterLocker : public AbstractLocker { class WriterLocker : public AbstractLocker {
public: public:
WriterLocker(tty_cookie *sourceCookie); WriterLocker(tty_cookie *sourceCookie);
~WriterLocker(); ~WriterLocker();
status_t AcquireWriter(bool dontBlock, int32 bytesNeeded); status_t AcquireWriter(bool dontBlock, size_t bytesNeeded);
private: private:
size_t _CheckAvailableBytes() const; size_t _CheckAvailableBytes() const;
@@ -131,37 +130,36 @@ class ReaderLocker : public AbstractLocker {
class TTYReferenceLocking { class TTYReferenceLocking {
public: public:
inline bool Lock(tty_cookie *cookie) inline bool Lock(tty_cookie *cookie)
{ {
MutexLocker _(gTTYCookieLock); MutexLocker _(gTTYCookieLock);
if (cookie->closed || cookie->other_tty->open_count == 0) if (cookie->closed || cookie->other_tty->open_count == 0)
return false; return false;
cookie->thread_count++; cookie->thread_count++;
return true; return true;
} }
inline void Unlock(tty_cookie *cookie) inline void Unlock(tty_cookie *cookie)
{ {
MutexLocker locker(gTTYCookieLock); MutexLocker locker(gTTYCookieLock);
sem_id semaphore = -1; sem_id semaphore = -1;
if (--cookie->thread_count == 0 && cookie->closed) if (--cookie->thread_count == 0 && cookie->closed)
semaphore = cookie->blocking_semaphore; semaphore = cookie->blocking_semaphore;
locker.Unlock();
locker.Unlock();
if (semaphore >= 0) {
if (semaphore >= 0) { TRACE(("TTYReference: cookie %p closed, last operation done, "
TRACE(("TTYReference: cookie %p closed, last operation done, " "releasing blocking sem %ld\n", cookie, semaphore));
"releasing blocking sem %ld\n", cookie, semaphore));
release_sem(semaphore);
release_sem(semaphore); }
} }
}
}; };
typedef AutoLocker<tty_cookie, TTYReferenceLocking> TTYReference; typedef AutoLocker<tty_cookie, TTYReferenceLocking> TTYReference;
@@ -169,18 +167,20 @@ typedef AutoLocker<tty_cookie, TTYReferenceLocking> TTYReference;
// #pragma mark - // #pragma mark -
Request::Request() Request::Request()
: fOwner(NULL), :
fCookie(NULL), fOwner(NULL),
fBytesNeeded(0), fCookie(NULL),
fNotified(false), fBytesNeeded(0),
fError(false) fNotified(false),
fError(false)
{ {
} }
void void
Request::Init(RequestOwner *owner, tty_cookie *cookie, int32 bytesNeeded) Request::Init(RequestOwner *owner, tty_cookie *cookie, size_t bytesNeeded)
{ {
fOwner = owner; fOwner = owner;
fCookie = cookie; fCookie = cookie;
@@ -191,7 +191,7 @@ Request::Init(RequestOwner *owner, tty_cookie *cookie, int32 bytesNeeded)
void void
Request::Notify(int32 bytesAvailable) Request::Notify(size_t bytesAvailable)
{ {
if (!fNotified && bytesAvailable >= fBytesNeeded && fOwner) { if (!fNotified && bytesAvailable >= fBytesNeeded && fOwner) {
fOwner->Notify(this); fOwner->Notify(this);
@@ -213,8 +213,10 @@ Request::NotifyError(status_t error)
// #pragma mark - // #pragma mark -
RequestQueue::RequestQueue() RequestQueue::RequestQueue()
: fRequests() :
fRequests()
{ {
} }
@@ -242,7 +244,7 @@ RequestQueue::Remove(Request *request)
void void
RequestQueue::NotifyFirst(int32 bytesAvailable) RequestQueue::NotifyFirst(size_t bytesAvailable)
{ {
RecursiveLocker _(gTTYRequestLock); RecursiveLocker _(gTTYRequestLock);
@@ -276,11 +278,13 @@ RequestQueue::NotifyError(tty_cookie *cookie, status_t error)
// #pragma mark - // #pragma mark -
RequestOwner::RequestOwner() RequestOwner::RequestOwner()
: fSemaphore(NULL), :
fCookie(NULL), fSemaphore(NULL),
fError(B_OK), fCookie(NULL),
fBytesNeeded(1) fError(B_OK),
fBytesNeeded(1)
{ {
fRequestQueues[0] = NULL; fRequestQueues[0] = NULL;
fRequestQueues[1] = NULL; fRequestQueues[1] = NULL;
@@ -332,7 +336,7 @@ RequestOwner::Dequeue()
void void
RequestOwner::SetBytesNeeded(int32 bytesNeeded) RequestOwner::SetBytesNeeded(size_t bytesNeeded)
{ {
if (fRequestQueues[0]) if (fRequestQueues[0])
fRequests[0].Init(this, fCookie, bytesNeeded); fRequests[0].Init(this, fCookie, bytesNeeded);
@@ -455,9 +459,9 @@ RequestOwner::NotifyError(Request *request, status_t error)
// #pragma mark - // #pragma mark -
WriterLocker::WriterLocker(tty_cookie *sourceCookie) WriterLocker::WriterLocker(tty_cookie *sourceCookie)
: : AbstractLocker(sourceCookie),
AbstractLocker(sourceCookie),
fSource(fCookie->tty), fSource(fCookie->tty),
fTarget(fCookie->other_tty), fTarget(fCookie->other_tty),
fRequestOwner(), fRequestOwner(),
@@ -522,7 +526,7 @@ WriterLocker::_CheckAvailableBytes() const
status_t status_t
WriterLocker::AcquireWriter(bool dontBlock, int32 bytesNeeded) WriterLocker::AcquireWriter(bool dontBlock, size_t bytesNeeded)
{ {
if (!fTarget) if (!fTarget)
return B_FILE_ERROR; return B_FILE_ERROR;
@@ -583,8 +587,7 @@ WriterLocker::AcquireWriter(bool dontBlock, int32 bytesNeeded)
ReaderLocker::ReaderLocker(tty_cookie *cookie) ReaderLocker::ReaderLocker(tty_cookie *cookie)
: : AbstractLocker(cookie),
AbstractLocker(cookie),
fTTY(cookie->tty), fTTY(cookie->tty),
fRequestOwner() 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! // ToDo: "buffer" is not yet copied or accessed in a safe way!
int32 bytesNeeded = 1; size_t bytesNeeded = 1;
while (bytesWritten < length) { while (bytesWritten < length) {
status_t status = locker.AcquireWriter(dontBlock, bytesNeeded); status_t status = locker.AcquireWriter(dontBlock, bytesNeeded);
+50 -50
View File
@@ -1,7 +1,7 @@
/* /*
** Copyright 2004, Axel Dörfler, [email protected]. All rights reserved. * Copyright 2004-2006, Axel Dörfler, [email protected]. All rights reserved.
** Distributed under the terms of the Haiku License. * Distributed under the terms of the MIT License.
*/ */
#ifndef TTY_PRIVATE_H #ifndef TTY_PRIVATE_H
#define TTY_PRIVATE_H #define TTY_PRIVATE_H
@@ -34,75 +34,75 @@ struct tty;
struct tty_cookie; struct tty_cookie;
class Request : public DoublyLinkedListLinkImpl<Request> { class Request : public DoublyLinkedListLinkImpl<Request> {
public: public:
Request(); 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 Notify(size_t bytesAvailable);
void NotifyError(status_t error); void NotifyError(status_t error);
bool WasNotified() const { return fNotified; } bool WasNotified() const { return fNotified; }
bool HasError() const { return fError; } bool HasError() const { return fError; }
private: private:
RequestOwner *fOwner; RequestOwner *fOwner;
tty_cookie *fCookie; tty_cookie *fCookie;
int32 fBytesNeeded; size_t fBytesNeeded;
bool fNotified; bool fNotified;
bool fError; bool fError;
}; };
class RequestQueue { class RequestQueue {
public: public:
RequestQueue(); RequestQueue();
~RequestQueue() {} ~RequestQueue() {}
void Add(Request *request); void Add(Request *request);
void Remove(Request *request); void Remove(Request *request);
Request *First() const { return fRequests.First(); } Request *First() const { return fRequests.First(); }
bool IsEmpty() const { return fRequests.IsEmpty(); } bool IsEmpty() const { return fRequests.IsEmpty(); }
void NotifyFirst(int32 bytesAvailable); void NotifyFirst(size_t bytesAvailable);
void NotifyError(status_t error); void NotifyError(status_t error);
void NotifyError(tty_cookie *cookie, status_t error); void NotifyError(tty_cookie *cookie, status_t error);
private: private:
typedef DoublyLinkedList<Request> RequestList; typedef DoublyLinkedList<Request> RequestList;
RequestList fRequests; RequestList fRequests;
}; };
class RequestOwner { class RequestOwner {
public: public:
RequestOwner(); RequestOwner();
void Enqueue(tty_cookie *cookie, RequestQueue *queue1, void Enqueue(tty_cookie *cookie, RequestQueue *queue1,
RequestQueue *queue2 = NULL); RequestQueue *queue2 = NULL);
void Dequeue(); void Dequeue();
void SetBytesNeeded(int32 bytesNeeded); void SetBytesNeeded(size_t bytesNeeded);
int32 BytesNeeded() const { return fBytesNeeded; } 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 Notify(Request *request);
void NotifyError(Request *request, status_t error); void NotifyError(Request *request, status_t error);
status_t Error() const { return fError; } status_t Error() const { return fError; }
private: private:
Semaphore *fSemaphore; Semaphore *fSemaphore;
tty_cookie *fCookie; tty_cookie *fCookie;
status_t fError; status_t fError;
RequestQueue *fRequestQueues[2]; RequestQueue *fRequestQueues[2];
Request fRequests[2]; Request fRequests[2];
int32 fBytesNeeded; size_t fBytesNeeded;
}; };