Basic multithreading support
[clinton/bobotpp.git] / source / BotThreading.H
1 // BotThreading.H -*- 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 // Basic Mutex and automatic lock interface that falls back to noops
20 // (wasting one byte of space for each fake mutex...oh well) in the
21 // absence of threading. This is intended to be used to ensure that
22 // multiple Guile threads can call the bot without corrupting its data
23 // structures without forcing pthreads or locking overhead on the
24 // usual single threaded standalone bot
25
26 #include "config.h"
27
28 #ifdef MULTITHREAD
29 #include <pthread.h>
30 #endif
31
32 class BotMutex
33 {
34 #ifdef MULTITHREAD
35 pthread_mutex_t mutex;
36 #endif
37
38 public:
39 BotMutex ();
40 ~BotMutex ();
41
42 void lock ();
43 void unlock ();
44 };
45
46 // Interface to automatically acquire and release a BotMutex within a
47 // block
48 class BotLock
49 {
50 BotMutex& mutex;
51
52 public:
53 BotLock (BotMutex & m);
54 ~BotLock ();
55 };
56
57