SCM Library
Spherical Cube Map rendering library
 All Classes Files Functions Variables Friends Pages
scm-guard.hpp
1 // Copyright (C) 2011-2012 Robert Kooima
2 //
3 // LIBSCM is free software; you can redistribute it and/or modify it under the
4 // terms of the GNU General Public License as published by the Free Software
5 // Foundation; either version 2 of the License, or (at your option) any later
6 // version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITH-
9 // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 
13 #ifndef SCM_GUARD_HPP
14 #define SCM_GUARD_HPP
15 
16 #include <SDL.h>
17 #include <SDL_thread.h>
18 
19 //------------------------------------------------------------------------------
20 
23 
24 template <typename T> class scm_guard
25 {
26  SDL_mutex *data_mutex;
27  T data;
28 
29 public:
30 
32 
33  scm_guard(T d) : data(d)
34  {
35  data_mutex = SDL_CreateMutex();
36  }
37 
39 
41  {
42  SDL_DestroyMutex(data_mutex);
43  }
44 
46 
47  void set(T d)
48  {
49  SDL_mutexP(data_mutex);
50  data = d;
51  SDL_mutexV(data_mutex);
52  }
53 
55 
56  T get() const
57  {
58  T d;
59  SDL_mutexP(data_mutex);
60  d = data;
61  SDL_mutexV(data_mutex);
62  return d;
63  }
64 };
65 
66 //------------------------------------------------------------------------------
67 
68 #endif
~scm_guard()
Destroy this guard's mutex.
Definition: scm-guard.hpp:40
void set(T d)
Atomically set the guarded value.
Definition: scm-guard.hpp:47
The scm_guard template enforces mutual exclusion on a single value of its templated type...
Definition: scm-guard.hpp:24
scm_guard(T d)
Create the mutex for this guard.
Definition: scm-guard.hpp:33