Fix crash when providing bad command line argument
[clinton/bobotpp.git] / source / BotThreading.H
CommitLineData
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
37class BotMutex
38{
39#ifdef MULTITHREAD
40 pthread_mutex_t mutex;
0c98ce0c 41 pthread_mutexattr_t attrs;
eb42e727 42#endif
43
44public:
0c98ce0c 45 BotMutex (bool recursive = false);
eb42e727 46 ~BotMutex ();
47
48 void lock ();
49 void unlock ();
50};
51
52// Interface to automatically acquire and release a BotMutex within a
53// block
54class BotLock
55{
56 BotMutex& mutex;
57
58public:
59 BotLock (BotMutex & m);
60 ~BotLock ();
61};
62
a89e416c 63#endif