new fortune
[feisty_meow.git] / nucleus / library / processes / safe_roller.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : safe_roller                                                       *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1998-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "safe_roller.h"
16
17 #include <basis/functions.h>
18 #include <basis/mutex.h>
19 #include <structures/roller.h>
20 #include <structures/static_memory_gremlin.h>
21
22 using namespace basis;
23 using namespace structures;
24
25 namespace processes {
26
27 SAFE_STATIC(mutex, __roller_synch, )
28
29 void safe_add(int &to_change, int addition)
30 {
31   auto_synchronizer l(__roller_synch());
32   to_change += addition;
33 }
34
35 //////////////
36
37 safe_roller::safe_roller(int start_of_range, int end_of_range)
38 : _rolling(new int_roller(start_of_range, end_of_range)),
39   _lock(new mutex)
40 {
41 }
42
43 safe_roller::~safe_roller()
44 {
45   WHACK(_rolling);
46   WHACK(_lock);
47 }
48
49 int safe_roller::next_id()
50 {
51   _lock->lock();
52   int to_return = _rolling->next_id();
53   _lock->unlock();
54   return to_return;
55 }
56
57 int safe_roller::current() const
58 {
59   _lock->lock();
60   int to_return = _rolling->current();
61   _lock->unlock();
62   return to_return;
63 }
64
65 void safe_roller::set_current(int new_current)
66 {
67   _lock->lock();
68   _rolling->set_current(new_current);
69   _lock->unlock();
70 }
71
72 } //namespace.
73
74