Basic multithreading support
[clinton/bobotpp.git] / source / BotThreading.C
1 // BotThreading.C -*- C++ -*-
2 // Copyright (c) 2008 Clinton Ebadi
3
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // any later version.
8
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 // 02110-1301, USA.
18
19 #include "BotThreading.H"
20
21 #ifdef MULTITHREAD
22
23 #include <pthread.h>
24 #include <iostream>
25
26 BotMutex::BotMutex ()
27 {
28 pthread_t self = pthread_self ();
29 std::cerr << "Mutex Init..."
30 << " Mutex: " << &mutex
31 << " Thread: " << &self
32 << std::endl;
33 pthread_mutex_init (&mutex, 0);
34 }
35
36 BotMutex::~BotMutex ()
37 {
38 pthread_t self = pthread_self ();
39 std::cerr << "Mutex Destroy..."
40 << " Mutex: " << &mutex
41 << " Thread: " << &self
42 << std::endl;
43 pthread_mutex_destroy (&mutex);
44 }
45
46 void BotMutex::lock ()
47 {
48 pthread_t self = pthread_self ();
49 std::cerr << "< Mutex Lock..."
50 << " Mutex: " << &mutex
51 << " Thread: " << &self
52 << std::endl;
53 pthread_mutex_lock (&mutex);
54 }
55
56 void BotMutex::unlock ()
57 {
58 pthread_t self = pthread_self ();
59 std::cerr << "> Mutex Unlock..."
60 << " Mutex: " << &mutex
61 << " Thread: " << &self
62 << std::endl;
63 pthread_mutex_unlock (&mutex);
64 }
65
66 #else // non threaded noops
67
68 BotMutex::BotMutex ()
69 { }
70
71 BotMutex::~BotMutex ()
72 { }
73
74 void BotMutex::lock ()
75 { }
76
77 void BotMutex::unlock ()
78 { }
79
80 #endif // MULTITHREAD
81
82 BotLock::BotLock (BotMutex & m)
83 : mutex(m)
84 {
85 pthread_t self = pthread_self ();
86 std::cerr << "Lock Init..."
87 << " Lock: " << this
88 << " Mutex: " << &mutex
89 << " Thread: " << &self
90 << std::endl;
91 mutex.lock ();
92 }
93
94 BotLock::~BotLock ()
95 {
96 pthread_t self = pthread_self ();
97 std::cerr << "Lock Destroy..."
98 << " Lock: " << this
99 << " Mutex: " << &mutex
100 << " Thread: " << &self
101 << std::endl;
102 mutex.unlock ();
103 }