| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * MO Library
|
|---|
| 5 | */
|
|---|
| 6 | #include "Environment.h"
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | #ifdef MOL_OS_WINDOWS
|
|---|
| 10 | #include "os-windows/PrecompiledHeaders.h"
|
|---|
| 11 | #include "os-windows/Os.h"
|
|---|
| 12 | #else
|
|---|
| 13 | #include "os-unix/Os.h"
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | #ifdef MOL_GUI_WINDOWS
|
|---|
| 17 | #include "gui-windows/Gui.h"
|
|---|
| 18 | #elif defined(MOL_GUI_FLTK)
|
|---|
| 19 | #include "gui-fltk/Gui.h"
|
|---|
| 20 | #else
|
|---|
| 21 | #include "gui-qt/Gui.h"
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #include <memory>
|
|---|
| 25 | #include <cstdlib>
|
|---|
| 26 | #include <cmath>
|
|---|
| 27 | #include <cerrno>
|
|---|
| 28 | #include <cstring>
|
|---|
| 29 | #include <climits>
|
|---|
| 30 |
|
|---|
| 31 | #include <iostream>
|
|---|
| 32 | #include <string>
|
|---|
| 33 | #include <stack>
|
|---|
| 34 | #include <queue>
|
|---|
| 35 | #include <map>
|
|---|
| 36 |
|
|---|
| 37 | #include <boost/static_assert.hpp>
|
|---|
| 38 | #include <boost/utility.hpp>
|
|---|
| 39 | #include <boost/cstdint.hpp>
|
|---|
| 40 | #include <boost/integer.hpp>
|
|---|
| 41 | #include <boost/cast.hpp>
|
|---|
| 42 | #include <boost/array.hpp>
|
|---|
| 43 | #include <boost/preprocessor.hpp>
|
|---|
| 44 | #include <boost/concept_check.hpp>
|
|---|
| 45 | #include <boost/pool/singleton_pool.hpp>
|
|---|
| 46 | #include <boost/intrusive/list.hpp>
|
|---|
| 47 | #include <boost/intrusive/list_hook.hpp>
|
|---|
| 48 | #include <boost/intrusive/slist.hpp>
|
|---|
| 49 | #include <boost/intrusive/slist_hook.hpp>
|
|---|
| 50 | #include <boost/ptr_container/ptr_map.hpp>
|
|---|
| 51 |
|
|---|
| 52 | #include <boost/lexical_cast.hpp>
|
|---|
| 53 | #include <boost/format.hpp>
|
|---|
| 54 | #include <boost/typeof/typeof.hpp>
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | #ifndef decltype
|
|---|
| 58 | # define decltype(expression) BOOST_TYPEOF(expression)
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | namespace mol
|
|---|
| 62 | {
|
|---|
| 63 | inline static void yield();
|
|---|
| 64 | inline static bool execute_in_single_processor();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | #include "MscCrt.h"
|
|---|
| 68 | #include "Debug.h"
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | #ifdef HAVE_CSTDATOMIC
|
|---|
| 72 | #include <cstdatomic>
|
|---|
| 73 | #else
|
|---|
| 74 | #ifdef MOL_INTERLOCKED
|
|---|
| 75 | namespace std
|
|---|
| 76 | {
|
|---|
| 77 | template <typename T>
|
|---|
| 78 | class atomic : boost::noncopyable
|
|---|
| 79 | {
|
|---|
| 80 | void* t;
|
|---|
| 81 | public:
|
|---|
| 82 | atomic(T t)
|
|---|
| 83 | {
|
|---|
| 84 | store(t);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | //T load() const volatile
|
|---|
| 88 | T load() volatile
|
|---|
| 89 | {
|
|---|
| 90 | BOOST_STATIC_ASSERT(sizeof(T) <= sizeof(void*));
|
|---|
| 91 | void* got = InterlockedCompareExchangePointer(
|
|---|
| 92 | reinterpret_cast<void* volatile *>(&t), 0, 0);
|
|---|
| 93 | T cooked;
|
|---|
| 94 | memcpy(&cooked, &got, sizeof(cooked));
|
|---|
| 95 | return cooked;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | T exchange(T new_val) volatile
|
|---|
| 99 | {
|
|---|
| 100 | void* tmp = NULL;
|
|---|
| 101 | memcpy(&tmp, &new_val, sizeof(new_val));
|
|---|
| 102 | void* got = InterlockedExchangePointer(
|
|---|
| 103 | reinterpret_cast<void* volatile *>(&t), tmp);
|
|---|
| 104 | T cooked;
|
|---|
| 105 | memcpy(&cooked, &got, sizeof(cooked));
|
|---|
| 106 | return cooked;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void store(T new_val) volatile
|
|---|
| 110 | {
|
|---|
| 111 | exchange(new_val);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | private:
|
|---|
| 115 | operator T() const volatile
|
|---|
| 116 | {
|
|---|
| 117 | return load();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | T operator=(T new_val) volatile
|
|---|
| 121 | {
|
|---|
| 122 | store(new_val);
|
|---|
| 123 | }
|
|---|
| 124 | };
|
|---|
| 125 | }
|
|---|
| 126 | #else
|
|---|
| 127 | #include <pthread.h>
|
|---|
| 128 | namespace std
|
|---|
| 129 | {
|
|---|
| 130 | template <typename T>
|
|---|
| 131 | class atomic : boost::noncopyable
|
|---|
| 132 | {
|
|---|
| 133 | T t;
|
|---|
| 134 | pthread_mutex_t mutex;
|
|---|
| 135 | public:
|
|---|
| 136 | atomic(T t) : t(t)
|
|---|
| 137 | {
|
|---|
| 138 | pthread_mutex_init(&mutex, NULL);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | ~atomic()
|
|---|
| 142 | {
|
|---|
| 143 | pthread_mutex_destroy(&mutex);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | //T load() const volatile
|
|---|
| 147 | T load()
|
|---|
| 148 | {
|
|---|
| 149 | pthread_mutex_lock(&mutex);
|
|---|
| 150 | T retval = t;
|
|---|
| 151 | pthread_mutex_unlock(&mutex);
|
|---|
| 152 | return retval;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | //T exchange(T new_val) volatile
|
|---|
| 156 | T exchange(T new_val)
|
|---|
| 157 | {
|
|---|
| 158 | pthread_mutex_lock(&mutex);
|
|---|
| 159 | T old = t;
|
|---|
| 160 | t = new_val;
|
|---|
| 161 | pthread_mutex_unlock(&mutex);
|
|---|
| 162 | return old;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | //void store(T new_val) volatile
|
|---|
| 166 | void store(T new_val)
|
|---|
| 167 | {
|
|---|
| 168 | exchange(new_val);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | private:
|
|---|
| 172 | //operator T() const volatile
|
|---|
| 173 | operator T() const
|
|---|
| 174 | {
|
|---|
| 175 | return load();
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | //T operator=(T new_val) volatile
|
|---|
| 179 | T operator=(T new_val)
|
|---|
| 180 | {
|
|---|
| 181 | store(new_val);
|
|---|
| 182 | }
|
|---|
| 183 | };
|
|---|
| 184 | }
|
|---|
| 185 | #endif
|
|---|
| 186 | #endif
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | namespace mol
|
|---|
| 191 | {
|
|---|
| 192 |
|
|---|
| 193 | template <class T, int V = 0
|
|---|
| 194 | + (sizeof(T) <= 16 ? 0:1)
|
|---|
| 195 | + (sizeof(T) <= 32 ? 0:1)
|
|---|
| 196 | + (sizeof(T) <= 64 ? 0:1)
|
|---|
| 197 | + (sizeof(T) <= 192 ? 0:1)
|
|---|
| 198 | + (sizeof(T) <= 1024 ? 0:1)
|
|---|
| 199 | > struct PoolSizeSelector {};
|
|---|
| 200 |
|
|---|
| 201 | template <class T> struct PoolSizeSelector<T, 0>
|
|---|
| 202 | {
|
|---|
| 203 | enum { SIZE = 16 };
|
|---|
| 204 | };
|
|---|
| 205 | template <class T> struct PoolSizeSelector<T, 1>
|
|---|
| 206 | {
|
|---|
| 207 | enum { SIZE = 32 };
|
|---|
| 208 | };
|
|---|
| 209 | template <class T> struct PoolSizeSelector<T, 2>
|
|---|
| 210 | {
|
|---|
| 211 | enum { SIZE = 64 };
|
|---|
| 212 | };
|
|---|
| 213 | template <class T> struct PoolSizeSelector<T, 3>
|
|---|
| 214 | {
|
|---|
| 215 | enum { SIZE = 192 };
|
|---|
| 216 | };
|
|---|
| 217 | template <class T> struct PoolSizeSelector<T, 4>
|
|---|
| 218 | {
|
|---|
| 219 | enum { SIZE = 1024 };
|
|---|
| 220 | };
|
|---|
| 221 |
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|