aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-10-06 03:25:22 +0000
committerMike Buland <eichlan@xagasoft.com>2011-10-06 03:25:22 +0000
commit208b983734d7431699f4bd3534e08321e42ada86 (patch)
treea6865f0db36e9c3b8dd13b9ed4d10d282f0f7cbf
parent1358cb4c7325e01c13533ca402cccc28575cdf14 (diff)
downloadlibbu++-208b983734d7431699f4bd3534e08321e42ada86.tar.gz
libbu++-208b983734d7431699f4bd3534e08321e42ada86.tar.bz2
libbu++-208b983734d7431699f4bd3534e08321e42ada86.tar.xz
libbu++-208b983734d7431699f4bd3534e08321e42ada86.zip
Renamed most of the core threading system, some ancillary systems need some
kind of prefix or something, we could stick with Ito, I will until I think of something else.
-rw-r--r--src/condition.cpp (renamed from src/itocondition.cpp)14
-rw-r--r--src/condition.h (renamed from src/itocondition.h)16
-rw-r--r--src/itocounter.h4
-rw-r--r--src/itolocker.cpp13
-rw-r--r--src/itolocker.h18
-rw-r--r--src/itoqueue.h8
-rw-r--r--src/itoserver.h12
-rw-r--r--src/mutex.cpp (renamed from src/itomutex.cpp)12
-rw-r--r--src/mutex.h (renamed from src/itomutex.h)10
-rw-r--r--src/mutexlocker.cpp24
-rw-r--r--src/mutexlocker.h21
-rw-r--r--src/thread.cpp (renamed from src/ito.cpp)20
-rw-r--r--src/thread.h (renamed from src/ito.h)24
13 files changed, 105 insertions, 91 deletions
diff --git a/src/itocondition.cpp b/src/condition.cpp
index 3d8db60..2f55ce2 100644
--- a/src/itocondition.cpp
+++ b/src/condition.cpp
@@ -7,24 +7,24 @@
7 7
8#include <sys/time.h> 8#include <sys/time.h>
9 9
10#include "bu/itocondition.h" 10#include "bu/condition.h"
11 11
12Bu::ItoCondition::ItoCondition() 12Bu::Condition::Condition()
13{ 13{
14 pthread_cond_init( &cond, NULL ); 14 pthread_cond_init( &cond, NULL );
15} 15}
16 16
17Bu::ItoCondition::~ItoCondition() 17Bu::Condition::~Condition()
18{ 18{
19 pthread_cond_destroy( &cond ); 19 pthread_cond_destroy( &cond );
20} 20}
21 21
22int Bu::ItoCondition::wait() 22int Bu::Condition::wait()
23{ 23{
24 return pthread_cond_wait( &cond, &mutex ); 24 return pthread_cond_wait( &cond, &mutex );
25} 25}
26 26
27int Bu::ItoCondition::wait( int nSec, int nUSec ) 27int Bu::Condition::wait( int nSec, int nUSec )
28{ 28{
29 struct timeval now; 29 struct timeval now;
30 struct timespec timeout; 30 struct timespec timeout;
@@ -37,12 +37,12 @@ int Bu::ItoCondition::wait( int nSec, int nUSec )
37 return pthread_cond_timedwait( &cond, &mutex, &timeout ); 37 return pthread_cond_timedwait( &cond, &mutex, &timeout );
38} 38}
39 39
40int Bu::ItoCondition::signal() 40int Bu::Condition::signal()
41{ 41{
42 return pthread_cond_signal( &cond ); 42 return pthread_cond_signal( &cond );
43} 43}
44 44
45int Bu::ItoCondition::broadcast() 45int Bu::Condition::broadcast()
46{ 46{
47 return pthread_cond_broadcast( &cond ); 47 return pthread_cond_broadcast( &cond );
48} 48}
diff --git a/src/itocondition.h b/src/condition.h
index 88e8d6c..71634f5 100644
--- a/src/itocondition.h
+++ b/src/condition.h
@@ -5,25 +5,25 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_CONDITION_H 8#ifndef BU_CONDITION_H
9#define BU_ITO_CONDITION_H 9#define BU_CONDITION_H
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
13#include "itomutex.h" 13#include "bu/mutex.h"
14 14
15namespace Bu 15namespace Bu
16{ 16{
17 /** 17 /**
18 * Ito condition. This is a fairly simple condition mechanism. As you may 18 * Ito condition. This is a fairly simple condition mechanism. As you may
19 * notice this class inherits from the ItoMutex class, this is because all 19 * notice this class inherits from the Mutex class, this is because all
20 * conditions must be within a locked block. The standard usage of a 20 * conditions must be within a locked block. The standard usage of a
21 * condition is to pause one thread, perhaps indefinately, until another 21 * condition is to pause one thread, perhaps indefinately, until another
22 * thread signals that it is alright to procede. 22 * thread signals that it is alright to procede.
23 * <br> 23 * <br>
24 * Standard usage for the thread that wants to wait is as follows: 24 * Standard usage for the thread that wants to wait is as follows:
25 * <pre> 25 * <pre>
26 * ItoCondition cond; 26 * Condition cond;
27 * ... // Perform setup and enter your run loop 27 * ... // Perform setup and enter your run loop
28 * cond.lock(); 28 * cond.lock();
29 * while( !isFinished() ) // Could be anything you're waiting for 29 * while( !isFinished() ) // Could be anything you're waiting for
@@ -36,18 +36,18 @@ namespace Bu
36 * or broadcast. See both of those functions for the difference. 36 * or broadcast. See both of those functions for the difference.
37 *@ingroup Threading 37 *@ingroup Threading
38 */ 38 */
39 class ItoCondition : public ItoMutex 39 class Condition : public Mutex
40 { 40 {
41 public: 41 public:
42 /** 42 /**
43 * Create a condition. 43 * Create a condition.
44 */ 44 */
45 ItoCondition(); 45 Condition();
46 46
47 /** 47 /**
48 * Destroy a condition. 48 * Destroy a condition.
49 */ 49 */
50 ~ItoCondition(); 50 ~Condition();
51 51
52 /** 52 /**
53 * Wait forever, or until signalled. This has to be called from within 53 * Wait forever, or until signalled. This has to be called from within
diff --git a/src/itocounter.h b/src/itocounter.h
index 10df467..10126a5 100644
--- a/src/itocounter.h
+++ b/src/itocounter.h
@@ -8,7 +8,7 @@
8#ifndef BU_ITO_COUNTER_H 8#ifndef BU_ITO_COUNTER_H
9#define BU_ITO_COUNTER_H 9#define BU_ITO_COUNTER_H
10 10
11#include "itomutex.h" 11#include "mutex.h"
12 12
13namespace Bu 13namespace Bu
14{ 14{
@@ -42,7 +42,7 @@ namespace Bu
42 42
43 private: 43 private:
44 T tCounter; /**< The counter itself. */ 44 T tCounter; /**< The counter itself. */
45 ItoMutex mOperate; /**< The master mutex, used on all operations. */ 45 Mutex mOperate; /**< The master mutex, used on all operations. */
46 }; 46 };
47} 47}
48 48
diff --git a/src/itolocker.cpp b/src/itolocker.cpp
deleted file mode 100644
index 17e97fd..0000000
--- a/src/itolocker.cpp
+++ /dev/null
@@ -1,13 +0,0 @@
1#include "bu/itolocker.h"
2#include "bu/itomutex.h"
3
4Bu::ItoLocker::ItoLocker( Bu::ItoMutex &mu ) :
5 mu( mu )
6{
7 mu.lock();
8}
9
10Bu::ItoLocker::~ItoLocker()
11{
12 mu.unlock();
13}
diff --git a/src/itolocker.h b/src/itolocker.h
deleted file mode 100644
index 76e5198..0000000
--- a/src/itolocker.h
+++ /dev/null
@@ -1,18 +0,0 @@
1#ifndef BU_ITO_LOCKER_H
2#define BU_ITO_LOCKER_H
3
4namespace Bu
5{
6 class ItoMutex;
7 class ItoLocker
8 {
9 public:
10 ItoLocker( ItoMutex &mu );
11 virtual ~ItoLocker();
12
13 private:
14 ItoMutex & mu;
15 };
16};
17
18#endif
diff --git a/src/itoqueue.h b/src/itoqueue.h
index dc3cadb..039e09c 100644
--- a/src/itoqueue.h
+++ b/src/itoqueue.h
@@ -10,8 +10,8 @@
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
13#include "itomutex.h" 13#include "mutex.h"
14#include "itocondition.h" 14#include "condition.h"
15 15
16namespace Bu 16namespace Bu
17{ 17{
@@ -232,8 +232,8 @@ namespace Bu
232 Item *pEnd; /**< The end of the queue, the last element to dequeue. */ 232 Item *pEnd; /**< The end of the queue, the last element to dequeue. */
233 long nSize; /**< The number of items in the queue. */ 233 long nSize; /**< The number of items in the queue. */
234 234
235 ItoMutex mOperate; /**< The master mutex, used on all operations. */ 235 Mutex mOperate; /**< The master mutex, used on all operations. */
236 ItoCondition cBlock; /**< The condition for blocking dequeues. */ 236 Condition cBlock; /**< The condition for blocking dequeues. */
237 }; 237 };
238} 238}
239 239
diff --git a/src/itoserver.h b/src/itoserver.h
index 902c684..75b3349 100644
--- a/src/itoserver.h
+++ b/src/itoserver.h
@@ -16,8 +16,8 @@
16 16
17#include "bu/string.h" 17#include "bu/string.h"
18#include "bu/list.h" 18#include "bu/list.h"
19#include "bu/ito.h" 19#include "bu/thread.h"
20#include "bu/itomutex.h" 20#include "bu/mutex.h"
21#include "bu/itoqueue.h" 21#include "bu/itoqueue.h"
22#include "bu/set.h" 22#include "bu/set.h"
23 23
@@ -50,7 +50,7 @@ namespace Bu
50 * happening within the server itself, and actually makes it useful. 50 * happening within the server itself, and actually makes it useful.
51 *@ingroup Threading Serving 51 *@ingroup Threading Serving
52 */ 52 */
53 class ItoServer : public Ito 53 class ItoServer : public Thread
54 { 54 {
55 friend class ItoClient; 55 friend class ItoClient;
56 friend class SrvClientLinkFactory; 56 friend class SrvClientLinkFactory;
@@ -74,7 +74,7 @@ namespace Bu
74 74
75 private: 75 private:
76 class SrvClientLink; 76 class SrvClientLink;
77 class ItoClient : public Ito 77 class ItoClient : public Thread
78 { 78 {
79 friend class Bu::ItoServer::SrvClientLink; 79 friend class Bu::ItoServer::SrvClientLink;
80 public: 80 public:
@@ -96,7 +96,7 @@ namespace Bu
96 int iPort; 96 int iPort;
97 int nTimeoutSec; 97 int nTimeoutSec;
98 int nTimeoutUSec; 98 int nTimeoutUSec;
99 ItoMutex imProto; 99 Mutex imProto;
100 }; 100 };
101 101
102 class SrvClientLink : public Bu::ClientLink 102 class SrvClientLink : public Bu::ClientLink
@@ -132,7 +132,7 @@ namespace Bu
132 typedef ItoQueue<ItoClient *> ClientQueue; 132 typedef ItoQueue<ItoClient *> ClientQueue;
133 ClientHash hClients; 133 ClientHash hClients;
134 ClientQueue qClientCleanup; 134 ClientQueue qClientCleanup;
135 ItoMutex imClients; 135 Mutex imClients;
136 136
137 void clientCleanup( int iSocket ); 137 void clientCleanup( int iSocket );
138 }; 138 };
diff --git a/src/itomutex.cpp b/src/mutex.cpp
index 8de6336..dbaaece 100644
--- a/src/itomutex.cpp
+++ b/src/mutex.cpp
@@ -5,29 +5,29 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#include "bu/itomutex.h" 8#include "bu/mutex.h"
9 9
10Bu::ItoMutex::ItoMutex() 10Bu::Mutex::Mutex()
11{ 11{
12 pthread_mutex_init( &mutex, NULL ); 12 pthread_mutex_init( &mutex, NULL );
13} 13}
14 14
15Bu::ItoMutex::~ItoMutex() 15Bu::Mutex::~Mutex()
16{ 16{
17 pthread_mutex_destroy( &mutex ); 17 pthread_mutex_destroy( &mutex );
18} 18}
19 19
20int Bu::ItoMutex::lock() 20int Bu::Mutex::lock()
21{ 21{
22 return pthread_mutex_lock( &mutex ); 22 return pthread_mutex_lock( &mutex );
23} 23}
24 24
25int Bu::ItoMutex::unlock() 25int Bu::Mutex::unlock()
26{ 26{
27 return pthread_mutex_unlock( &mutex ); 27 return pthread_mutex_unlock( &mutex );
28} 28}
29 29
30int Bu::ItoMutex::trylock() 30int Bu::Mutex::trylock()
31{ 31{
32 return pthread_mutex_trylock( &mutex ); 32 return pthread_mutex_trylock( &mutex );
33} 33}
diff --git a/src/itomutex.h b/src/mutex.h
index 68a0c1d..b5c8b7a 100644
--- a/src/itomutex.h
+++ b/src/mutex.h
@@ -5,8 +5,8 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_MUTEX_H 8#ifndef BU_MUTEX_H
9#define BU_ITO_MUTEX_H 9#define BU_MUTEX_H
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
@@ -19,13 +19,13 @@ namespace Bu
19 * or initing them. 19 * or initing them.
20 *@ingroup Threading 20 *@ingroup Threading
21 */ 21 */
22 class ItoMutex 22 class Mutex
23 { 23 {
24 public: 24 public:
25 /** 25 /**
26 * Create an unlocked mutex. 26 * Create an unlocked mutex.
27 */ 27 */
28 ItoMutex(); 28 Mutex();
29 29
30 /** 30 /**
31 * Destroy a mutex. This can only be done when a mutex is unlocked. 31 * Destroy a mutex. This can only be done when a mutex is unlocked.
@@ -33,7 +33,7 @@ namespace Bu
33 * wait for the mutex to unlock, the odds of which are usually farily 33 * wait for the mutex to unlock, the odds of which are usually farily
34 * low at deconstruction time. 34 * low at deconstruction time.
35 */ 35 */
36 ~ItoMutex(); 36 ~Mutex();
37 37
38 /** 38 /**
39 * Lock the mutex. This causes all future calls to lock on this 39 * Lock the mutex. This causes all future calls to lock on this
diff --git a/src/mutexlocker.cpp b/src/mutexlocker.cpp
new file mode 100644
index 0000000..90b730e
--- /dev/null
+++ b/src/mutexlocker.cpp
@@ -0,0 +1,24 @@
1#include "bu/mutexlocker.h"
2#include "bu/mutex.h"
3
4Bu::MutexLocker::MutexLocker( Bu::Mutex &mu ) :
5 mu( mu )
6{
7 mu.lock();
8}
9
10Bu::MutexLocker::~MutexLocker()
11{
12 mu.unlock();
13}
14
15void Bu::MutexLocker::unlock()
16{
17 mu.unlock();
18}
19
20void Bu::MutexLocker::relock()
21{
22 mu.lock();
23}
24
diff --git a/src/mutexlocker.h b/src/mutexlocker.h
new file mode 100644
index 0000000..7c3c97e
--- /dev/null
+++ b/src/mutexlocker.h
@@ -0,0 +1,21 @@
1#ifndef BU_MUTEX_LOCKER_H
2#define BU_MUTEX_LOCKER_H
3
4namespace Bu
5{
6 class Mutex;
7 class MutexLocker
8 {
9 public:
10 MutexLocker( Mutex &mu );
11 virtual ~MutexLocker();
12
13 void unlock();
14 void relock();
15
16 private:
17 Mutex &mu;
18 };
19};
20
21#endif
diff --git a/src/ito.cpp b/src/thread.cpp
index aa9a597..e4563a2 100644
--- a/src/ito.cpp
+++ b/src/thread.cpp
@@ -5,51 +5,51 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#include "bu/ito.h" 8#include "bu/thread.h"
9 9
10#include "bu/config.h" 10#include "bu/config.h"
11 11
12Bu::Ito::Ito() 12Bu::Thread::Thread()
13{ 13{
14} 14}
15 15
16Bu::Ito::~Ito() 16Bu::Thread::~Thread()
17{ 17{
18} 18}
19 19
20bool Bu::Ito::start() 20bool Bu::Thread::start()
21{ 21{
22 nHandle = pthread_create( &ptHandle, NULL, threadRunner, this ); 22 nHandle = pthread_create( &ptHandle, NULL, threadRunner, this );
23 23
24 return true; 24 return true;
25} 25}
26 26
27bool Bu::Ito::stop() 27bool Bu::Thread::stop()
28{ 28{
29 pthread_cancel( ptHandle ); 29 pthread_cancel( ptHandle );
30 30
31 return true; 31 return true;
32} 32}
33 33
34void *Bu::Ito::threadRunner( void *pThread ) 34void *Bu::Thread::threadRunner( void *pThread )
35{ 35{
36 ((Ito *)pThread)->run(); 36 ((Thread *)pThread)->run();
37 pthread_exit( NULL ); 37 pthread_exit( NULL );
38 return NULL; 38 return NULL;
39} 39}
40 40
41bool Bu::Ito::join() 41bool Bu::Thread::join()
42{ 42{
43 pthread_join( ptHandle, NULL ); 43 pthread_join( ptHandle, NULL );
44 return true; 44 return true;
45} 45}
46 46
47void Bu::Ito::yield() 47void Bu::Thread::yield()
48{ 48{
49#ifndef WIN32 49#ifndef WIN32
50 pthread_yield(); 50 pthread_yield();
51#else 51#else
52 #warning Bu::Ito::yield IS A STUB for WIN32!!!! 52 #warning Bu::Thread::yield IS A STUB for WIN32!!!!
53#endif 53#endif
54} 54}
55 55
diff --git a/src/ito.h b/src/thread.h
index 9f50b2a..70e6f5f 100644
--- a/src/ito.h
+++ b/src/thread.h
@@ -5,8 +5,8 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_H 8#ifndef BU_THREAD_H
9#define BU_ITO_H 9#define BU_THREAD_H
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
@@ -19,18 +19,18 @@ namespace Bu
19 * run in it's own thread, one per class instance. 19 * run in it's own thread, one per class instance.
20 *@ingroup Threading 20 *@ingroup Threading
21 */ 21 */
22 class Ito 22 class Thread
23 { 23 {
24 public: 24 public:
25 /** 25 /**
26 * Construct an Ito thread. 26 * Construct an Thread thread.
27 */ 27 */
28 Ito(); 28 Thread();
29 29
30 /** 30 /**
31 * Destroy an Ito thread. 31 * Destroy an Thread thread.
32 */ 32 */
33 virtual ~Ito(); 33 virtual ~Thread();
34 34
35 /** 35 /**
36 * Begin thread execution. This will call the overridden run function, 36 * Begin thread execution. This will call the overridden run function,
@@ -38,7 +38,7 @@ namespace Bu
38 * exits, the thread is killed, or the thread is cancelled (optionally). 38 * exits, the thread is killed, or the thread is cancelled (optionally).
39 * The thread started in this manner has access to all of it's class 39 * The thread started in this manner has access to all of it's class
40 * variables, but be sure to protect possible multiple-access with 40 * variables, but be sure to protect possible multiple-access with
41 * ItoMutex objects. 41 * ThreadMutex objects.
42 * @returns True if starting the thread was successful. False if 42 * @returns True if starting the thread was successful. False if
43 * something went wrong and the thread has not started. 43 * something went wrong and the thread has not started.
44 */ 44 */
@@ -63,7 +63,7 @@ namespace Bu
63 /** 63 /**
64 * Join the thread in action. This function performs what is commonly 64 * Join the thread in action. This function performs what is commonly
65 * called a thread join. That is that it effectively makes the calling 65 * called a thread join. That is that it effectively makes the calling
66 * thread an the Ito thread contained in the called object one in the 66 * thread an the Thread thread contained in the called object one in the
67 * same, and pauses the calling thread until the called thread exits. 67 * same, and pauses the calling thread until the called thread exits.
68 * That is, when called from, say, your main(), mythread.join() will 68 * That is, when called from, say, your main(), mythread.join() will
69 * not return until the thread mythread has exited. This is very handy 69 * not return until the thread mythread has exited. This is very handy
@@ -79,10 +79,10 @@ namespace Bu
79 79
80 protected: 80 protected:
81 /** 81 /**
82 * The workhorse of the Ito class. This is the function that will run 82 * The workhorse of the Thread class. This is the function that will run
83 * in the thread, when this function exits the thread dies and is 83 * in the thread, when this function exits the thread dies and is
84 * cleaned up by the system. Make sure to read up on ItoMutex, 84 * cleaned up by the system. Make sure to read up on ThreadMutex,
85 * ItoCondition, and cancel to see how to control and protect 85 * ThreadCondition, and cancel to see how to control and protect
86 * everything you do in a safe way within this function. 86 * everything you do in a safe way within this function.
87 *@returns I'm not sure right now, but this is the posix standard form. 87 *@returns I'm not sure right now, but this is the posix standard form.
88 */ 88 */