moving things around

git-svn-id: svn://qnap.local/TwoTerm/trunk@1987 5590a31f-7b70-45f8-8c82-aa3a8e5f4507
This commit is contained in:
Kelvin Sherlock
2011-01-12 03:50:56 +00:00
parent 08d61ce529
commit effa18a344
4 changed files with 0 additions and 0 deletions

26
cpp/Lock.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "Lock.h"
Lock::Lock()
{
pthread_mutex_init(&_mutex, NULL);
}
Lock::~Lock()
{
pthread_mutex_destroy(&_mutex);
}
void Lock::lock()
{
pthread_mutex_lock(&_mutex);
}
void Lock::unlock()
{
pthread_mutex_unlock(&_mutex);
}
bool Lock::tryLock()
{
return pthread_mutex_trylock(&_mutex) == 0;
}

31
cpp/Lock.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef __LOCK_H__
#define __LOCK_H__
#include <pthread.h>
class Lock {
public:
Lock();
~Lock();
void lock();
void unlock();
bool tryLock();
private:
pthread_mutex_t _mutex;
};
class Locker {
public:
Locker(Lock& lock) : _lock(lock) { _lock.lock(); }
~Locker() { _lock.unlock(); }
private:
Lock &_lock;
};
#endif

75
cpp/OutputChannel.cpp Normal file
View File

@@ -0,0 +1,75 @@
/*
* OutputChannel.cpp
* 2Term
*
* Created by Kelvin Sherlock on 7/7/2010.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "OutputChannel.h"
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <cerrno>
bool OutputChannel::write(uint8_t c)
{
return write(&c, 1);
}
bool OutputChannel::write(const char *str)
{
return write(str, std::strlen(str));
}
bool OutputChannel::write(const void *vp, size_t size)
{
if (!size) return true;
for (unsigned i = 0; ;)
{
ssize_t s = ::write(_fd, vp, size);
if (s < 0)
{
switch (errno)
{
case EAGAIN:
case EINTR:
if (++i < 3) break;
default:
_error = errno;
// throw?
return false;
}
}
else if (size == s)
{
return true;
}
else if (s == 0)
{
if (++i == 3)
{
_error = EIO;
return false;
}
}
else
{
size -= s;
vp = (uint8_t *)vp + s;
if (size == 0) return true;
}
}
return false;
}

36
cpp/OutputChannel.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* OutputChannel.h
* 2Term
*
* Created by Kelvin Sherlock on 7/7/2010.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#ifndef __OUTPUT_CHANNEL_H__
#define __OUTPUT_CHANNEL_H__
#include <stdint.h>
#include <sys/types.h>
class OutputChannel
{
public:
OutputChannel(int fd) : _fd(fd), _error(0) {};
bool write(uint8_t);
bool write(const char *);
bool write(const void *, size_t);
int error() const { return _error; }
private:
OutputChannel(const OutputChannel&);
OutputChannel& operator=(const OutputChannel&);
int _fd;
int _error;
};
#endif