feisty meow concerns codebase 2.140
safe_roller.cpp
Go to the documentation of this file.
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>
21
22using namespace basis;
23using namespace structures;
24
25namespace processes {
26
27SAFE_STATIC(mutex, __roller_synch, )
28
29void safe_add(int &to_change, int addition)
30{
31 auto_synchronizer l(__roller_synch());
32 to_change += addition;
33}
34
36
37safe_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
44{
45 WHACK(_rolling);
46 WHACK(_lock);
47}
48
50{
51 _lock->lock();
52 int to_return = _rolling->next_id();
53 _lock->unlock();
54 return to_return;
55}
56
58{
59 _lock->lock();
60 int to_return = _rolling->current();
61 _lock->unlock();
62 return to_return;
63}
64
65void 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
auto_synchronizer simplifies concurrent code by automatically unlocking.
Definition mutex.h:113
void lock()
Clamps down on the mutex, if possible.
Definition mutex.cpp:101
void unlock()
Gives up the possession of the mutex.
Definition mutex.cpp:113
int next_id()
returns a unique (per instance of this type) id.
void set_current(int new_current)
allows the current id to be manipulated.
safe_roller(int start_of_range=0, int end_of_range=MAXINT32)
Provides numbers between the start and end in a thread-safe way.
int current() const
returns the current id to be used; be careful!
A roller that's based on integers. This is the most common type so far.
Definition roller.h:79
contents current() const
returns the current id to be used; be careful!
Definition roller.h:102
void set_current(contents new_current)
allows the current id to be manipulated.
Definition roller.h:94
contents next_id()
returns a unique (per instance of this type) id.
Definition roller.h:105
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
void safe_add(int &to_change, int addition)
thread-safe integer addition.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#define SAFE_STATIC(type, func_name, parms)
Statically defines a singleton object whose scope is the program's lifetime.