Clipper
clipper_thread.h
1 
4 //C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5 //L
6 //L This library is free software and is distributed under the terms
7 //L and conditions of version 2.1 of the GNU Lesser General Public
8 //L Licence (LGPL) with the following additional clause:
9 //L
10 //L `You may also combine or link a "work that uses the Library" to
11 //L produce a work containing portions of the Library, and distribute
12 //L that work under terms of your choice, provided that you give
13 //L prominent notice with each copy of the work that the specified
14 //L version of the Library is used in it, and that you include or
15 //L provide public access to the complete corresponding
16 //L machine-readable source code for the Library including whatever
17 //L changes were used in the work. (i.e. If you make changes to the
18 //L Library you must distribute those, but you do not need to
19 //L distribute source or object code to those portions of the work
20 //L not covered by this licence.)'
21 //L
22 //L Note that this clause grants an additional right and does not impose
23 //L any additional restriction, and so does not affect compatibility
24 //L with the GNU General Public Licence (GPL). If you wish to negotiate
25 //L other terms, please contact the maintainer.
26 //L
27 //L You can redistribute it and/or modify the library under the terms of
28 //L the GNU Lesser General Public License as published by the Free Software
29 //L Foundation; either version 2.1 of the License, or (at your option) any
30 //L later version.
31 //L
32 //L This library is distributed in the hope that it will be useful, but
33 //L WITHOUT ANY WARRANTY; without even the implied warranty of
34 //L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 //L Lesser General Public License for more details.
36 //L
37 //L You should have received a copy of the CCP4 licence and/or GNU
38 //L Lesser General Public License along with this library; if not, write
39 //L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40 //L The GNU Lesser General Public can also be obtained by writing to the
41 //L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 //L MA 02111-1307 USA
43 
44 
45 #ifndef CLIPPER_THREAD
46 #define CLIPPER_THREAD
47 
48 
49 #include "clipper_message.h"
50 #include "clipper_sysdep.h"
51 
52 
53 namespace clipper
54 {
55 
56 
57 #ifndef CLIPPER_DISABLE_THREADS
58 
59 
61 
64  class Mutex {
65  public:
67  Mutex() { CLIPPER_MUTEX_INIT( &mutex ); }
69  ~Mutex() { CLIPPER_MUTEX_FREE( &mutex ); }
71  inline void lock() { CLIPPER_MUTEX_LOCK( &mutex ); }
73  inline void unlock() { CLIPPER_MUTEX_UNLK( &mutex ); }
74  protected:
75  CLIPPER_MUTEX_TYPE mutex;
76  };
77 
78 
80 
111  class Thread_base {
112  public:
113  Thread_base();
114  virtual ~Thread_base() {}
115  bool run();
116  bool join();
117  static void lock() { mutex_global.lock(); }
118  static void unlock() { mutex_global.unlock(); }
119  int id() const { return id_; }
120 
121  protected:
122  virtual void Run() = 0;
123 
124  private:
125  static CLIPPER_THREAD_RETTYPE Entry( CLIPPER_THREAD_ARGTYPE thisptr );
126  static Mutex mutex_global;
127  static int next_id;
128  CLIPPER_THREAD_TYPE thread;
129  int id_;
130  };
131 
132 
133 #else
134 
135 
136  class Mutex {
137  public:
138  Mutex() {}
139  ~Mutex() {}
140  inline void lock() {}
141  inline void unlock() {}
142  };
143 
144  class Thread_base {
145  public:
146  Thread_base() {}
147  virtual ~Thread_base() {}
148  bool run() { Run(); return true; }
149  bool join() { return true; }
150  static void lock() {}
151  static void unlock() {}
152  int id() const { return -1; }
153  protected:
154  virtual void Run() { clipper::Message::message( clipper::Message_fatal( "No Run method defined" ) ); }
155  };
156 
157 
158 #endif
159 
160 
161 } // namespace clipper
162 
163 #endif
Fatal message (level = 9)
Definition: clipper_message.h:129
Mutex()
constructor: create the mutex
Definition: clipper_thread.h:67
~Mutex()
destructor: destroy the mutex
Definition: clipper_thread.h:69
void lock()
lock the mutex
Definition: clipper_thread.h:71
Thread base class: Override this to create new threads.
Definition: clipper_thread.h:111
Mutex class: used for locking and unlocking shared resources.
Definition: clipper_thread.h:64
static void message(const T &message)
pass a message
Definition: clipper_message.h:93
void unlock()
unlock the mutex
Definition: clipper_thread.h:73