Not exactly an Atomic implementation - If you use shared resources, this helps you protecting the access with a Mutex.

Work in progress...

Committer:
MatteoT
Date:
Fri Jul 19 06:13:48 2013 +0000
Revision:
3:1069ef627cff
Parent:
2:16cab88c1203
method templates inside header file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MatteoT 0:6c75b6083087 1 #ifndef _SHAREDOBJECT_H_
MatteoT 0:6c75b6083087 2 #define _SHAREDOBJECT_H_
MatteoT 0:6c75b6083087 3
MatteoT 0:6c75b6083087 4 #include "mbed.h"
MatteoT 0:6c75b6083087 5 #include "rtos.h"
MatteoT 0:6c75b6083087 6
MatteoT 0:6c75b6083087 7 /** Template class used to protect a shared resource with a Mutex.
MatteoT 0:6c75b6083087 8 */
MatteoT 0:6c75b6083087 9 template <class T>
MatteoT 0:6c75b6083087 10 class SharedObject
MatteoT 0:6c75b6083087 11 {
MatteoT 0:6c75b6083087 12
MatteoT 0:6c75b6083087 13 /** get/set controll mutex
MatteoT 0:6c75b6083087 14 */
MatteoT 0:6c75b6083087 15 Mutex _readwrite_mutex;
MatteoT 0:6c75b6083087 16
MatteoT 0:6c75b6083087 17 /** value of the object
MatteoT 0:6c75b6083087 18 */
MatteoT 0:6c75b6083087 19 T _value;
MatteoT 0:6c75b6083087 20
MatteoT 0:6c75b6083087 21 public:
MatteoT 0:6c75b6083087 22
MatteoT 0:6c75b6083087 23 /** Resource constructor.
MatteoT 0:6c75b6083087 24 * @param value sets the initial value of the resource.
MatteoT 0:6c75b6083087 25 */
MatteoT 3:1069ef627cff 26 SharedObject (const T& value)
MatteoT 3:1069ef627cff 27 {
MatteoT 3:1069ef627cff 28 _readwrite_mutex.lock();
MatteoT 3:1069ef627cff 29 _value = value;
MatteoT 3:1069ef627cff 30 _readwrite_mutex.unlock();
MatteoT 3:1069ef627cff 31 }
MatteoT 0:6c75b6083087 32
MatteoT 0:6c75b6083087 33 /** Resource constructor without initial value.
MatteoT 0:6c75b6083087 34 */
MatteoT 3:1069ef627cff 35 SharedObject ()
MatteoT 3:1069ef627cff 36 {
MatteoT 3:1069ef627cff 37 _readwrite_mutex.unlock();
MatteoT 3:1069ef627cff 38 }
MatteoT 0:6c75b6083087 39
MatteoT 0:6c75b6083087 40 /** Sets the specified value_destination with the value of the shared resource.
MatteoT 0:6c75b6083087 41 */
MatteoT 3:1069ef627cff 42 void get (T& value_destination) const
MatteoT 3:1069ef627cff 43 {
MatteoT 3:1069ef627cff 44 _readwrite_mutex.lock();
MatteoT 3:1069ef627cff 45 value_destination = _value;
MatteoT 3:1069ef627cff 46 _readwrite_mutex.unlock();
MatteoT 3:1069ef627cff 47 }
MatteoT 2:16cab88c1203 48 ///Returns the value of the shared resource (may be slower than get).
MatteoT 3:1069ef627cff 49 operator T () const
MatteoT 3:1069ef627cff 50 {
MatteoT 3:1069ef627cff 51 T tmp_value;
MatteoT 3:1069ef627cff 52 get(tmp_value);
MatteoT 3:1069ef627cff 53 return tmp_value;
MatteoT 3:1069ef627cff 54 }
MatteoT 0:6c75b6083087 55
MatteoT 0:6c75b6083087 56 /** Sets the value of the shared resource with the specified new_value.
MatteoT 0:6c75b6083087 57 */
MatteoT 3:1069ef627cff 58 void set (const T& new_value)
MatteoT 3:1069ef627cff 59 {
MatteoT 3:1069ef627cff 60 _readwrite_mutex.lock();
MatteoT 3:1069ef627cff 61 _value = new_value;
MatteoT 3:1069ef627cff 62 _readwrite_mutex.unlock();
MatteoT 3:1069ef627cff 63 }
MatteoT 2:16cab88c1203 64 ///Alias of set.
MatteoT 3:1069ef627cff 65 void operator= (const T& new_value)
MatteoT 3:1069ef627cff 66 {
MatteoT 3:1069ef627cff 67 set(new_value);
MatteoT 3:1069ef627cff 68 }
MatteoT 0:6c75b6083087 69 };
MatteoT 0:6c75b6083087 70
MatteoT 0:6c75b6083087 71
MatteoT 0:6c75b6083087 72 #endif