xtd 0.2.0
Loading...
Searching...
No Matches
thread_local_object.h
Go to the documentation of this file.
1
4#pragma once
5#include "mutex.h"
6#include "thread.h"
7#include "../func.h"
8#include "../invalid_operation_exception.h"
9#include "../object.h"
10#include "../time_span.h"
11#include <map>
12
14namespace xtd {
16 namespace threading {
37 template<typename value_t>
38 class thread_local_object : public object {
39 struct data : public object {
41 bool track_all_values = false;
42 func<value_t> value_factory;
43 std::map<intptr, value_t> values;
44 };
45
46 struct lock_guard_mutex {
47 lock_guard_mutex(mutex& mutex) : mutex_(mutex) {mutex_.lock();}
48 ~lock_guard_mutex() {mutex_.unlock();}
49 private:
50 mutex& mutex_;
51 };
52
53 public:
55
64 explicit thread_local_object(bool track_all_values) {data_->track_all_values = track_all_values;}
67 explicit thread_local_object(const func<value_t>& value_factory) {data_->value_factory = value_factory;}
72 thread_local_object(const func<value_t>& value_factory, bool track_all_values) {
73 data_->value_factory = value_factory;
74 data_->track_all_values = track_all_values;
75 }
77
79 template<typename func_t>
80 explicit thread_local_object(func_t value_factory) : thread_local_object(func<value_t> {value_factory}) {}
81 template<typename func_t>
82 explicit thread_local_object(func_t value_factory, bool track_all_values) : thread_local_object(func<value_t> {value_factory}, track_all_values) {}
85 thread_local_object& operator =(const thread_local_object& other) = default;
87
89
93 bool is_value_created() const noexcept {
94 lock_guard_mutex lock {data_->mutex};
95 return data_->values.find(thread::current_thread().thread_id()) != data_->values.end();
96 }
97
101 value_t value() const noexcept {
102 lock_guard_mutex lock {data_->mutex};
103 if (!is_value_created()) data_->values[thread::current_thread().thread_id()] = data_->value_factory.is_empty() ? value_t {} : data_->values[thread::current_thread().thread_id()] = data_->value_factory();
104 return data_->values.find(thread::current_thread().thread_id())->second;
105 }
106
110 void value(value_t value) noexcept {
111 lock_guard_mutex lock {data_->mutex};
112 data_->values[thread::current_thread().thread_id()] = value;
113 }
114
119 std::vector<value_t> values() const {
120 if (!data_->track_all_values) throw invalid_operation_exception(csf_);
121 lock_guard_mutex lock {data_->mutex};
122 auto values = std::vector<value_t> {};
123 for (const auto& entry : data_->values)
124 values.push_back(entry.second);
125 return values;
126 }
128
130
132 ustring to_string() const noexcept override {
133 return ustring::format("{}", value());
134 }
136
137 private:
138 mutable std::shared_ptr<data> data_ = std::make_shared<data>();
139 };
140 }
141}
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition delegate.h:363
The exception that is thrown when a method call is invalid for the object's current state.
Definition invalid_operation_exception.h:18
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:32
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:30
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.h:48
Provides thread-local storage of data.
Definition thread_local_object.h:38
thread_local_object(bool track_all_values)
Initializes the xtd::threading::thread_local_object instance and specifies whether all values are acc...
Definition thread_local_object.h:64
thread_local_object(const func< value_t > &value_factory, bool track_all_values)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.h:72
void value(value_t value) noexcept
Sets the value of this instance for the current thread.
Definition thread_local_object.h:110
thread_local_object()=default
Initializes the xtd::threading::thread_local_object instance.
bool is_value_created() const noexcept
Gets whether xtd::threading::thread_local_object::value is initialized on the current thread.
Definition thread_local_object.h:93
ustring to_string() const noexcept override
Returns a sxd::ustring that represents the current object.
Definition thread_local_object.h:132
value_t value() const noexcept
Gets the value of this instance for the current thread.
Definition thread_local_object.h:101
std::vector< value_t > values() const
Gets a list containing the values stored by all threads that have accessed this instance.
Definition thread_local_object.h:119
thread_local_object(const func< value_t > &value_factory)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.h:67
intptr thread_id() const noexcept
Gets the native operating system thread id.
static thread & current_thread() noexcept
Gets the currently running thread.
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition ustring.h:1131
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
delegate< result_t(arguments_t...)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.h:16
@ other
The operating system is other.
Contains xtd::threading::mutex exception.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::threading::thread class.