eb42e727 |
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 | |
a89e416c |
26 | #ifndef BOT_THREADING_H |
27 | #define BOT_THREADING_H |
28 | |
29 | #ifdef HAVE_CONFIG_H |
eb42e727 |
30 | #include "config.h" |
a89e416c |
31 | #endif |
eb42e727 |
32 | |
33 | #ifdef MULTITHREAD |
34 | #include <pthread.h> |
35 | #endif |
36 | |
37 | class BotMutex |
38 | { |
39 | #ifdef MULTITHREAD |
40 | pthread_mutex_t mutex; |
41 | #endif |
42 | |
43 | public: |
44 | BotMutex (); |
45 | ~BotMutex (); |
46 | |
47 | void lock (); |
48 | void unlock (); |
49 | }; |
50 | |
51 | // Interface to automatically acquire and release a BotMutex within a |
52 | // block |
53 | class BotLock |
54 | { |
55 | BotMutex& mutex; |
56 | |
57 | public: |
58 | BotLock (BotMutex & m); |
59 | ~BotLock (); |
60 | }; |
61 | |
a89e416c |
62 | #endif |