| 1 | #include "PrecompiledHeaders.h"
|
|---|
| 2 |
|
|---|
| 3 | #ifdef HAVE_CONFIG_H
|
|---|
| 4 | #include <config.h>
|
|---|
| 5 | #endif
|
|---|
| 6 |
|
|---|
| 7 | #include "Mil.h"
|
|---|
| 8 | #include "Thread.h"
|
|---|
| 9 | #include "ModuleCommon.h"
|
|---|
| 10 |
|
|---|
| 11 | //#include <fecti/Utility/FBlockMemoryAllocator/FAllocatorImpl_Block.cpp>
|
|---|
| 12 | //#include <fecti/Utility/FThread.cpp>
|
|---|
| 13 |
|
|---|
| 14 | #ifdef ENABLE_NLS
|
|---|
| 15 | #include <libintl.h>
|
|---|
| 16 | #else
|
|---|
| 17 | #define gettext(x) x
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | namespace mil {
|
|---|
| 21 |
|
|---|
| 22 | post_tfree_memory_function posts[MIL_MAX_THREADS];
|
|---|
| 23 |
|
|---|
| 24 | #ifdef MIL_TBB_PRODUCER
|
|---|
| 25 | #ifdef _MSC_VER
|
|---|
| 26 | #ifdef _DEBUG
|
|---|
| 27 | #pragma comment(lib, "ix86/tbb_debug.lib")
|
|---|
| 28 | #else
|
|---|
| 29 | #pragma comment(lib, "ix86/tbb.lib")
|
|---|
| 30 | #endif
|
|---|
| 31 | #endif
|
|---|
| 32 | namespace pool {
|
|---|
| 33 | //tbb::scalable_allocator<TbbMemoryUnit> tbb_a;
|
|---|
| 34 | tbb::tbb_allocator<TbbMemoryUnit> tbb_a;
|
|---|
| 35 | }
|
|---|
| 36 | #endif
|
|---|
| 37 |
|
|---|
| 38 | namespace thread {
|
|---|
| 39 | Serial id_serial(MIL_MAX_THREADS);
|
|---|
| 40 | static atomic<void*> threads[MIL_MAX_THREADS];
|
|---|
| 41 |
|
|---|
| 42 | void* get(id_t thread_id) {
|
|---|
| 43 | if (!is_valid_id(thread_id)) {
|
|---|
| 44 | halt << "thread_id is out of range";
|
|---|
| 45 | }
|
|---|
| 46 | return threads[thread_id].load();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | void set(id_t thread_id, void* object) {
|
|---|
| 50 | if (!is_valid_id(thread_id)) {
|
|---|
| 51 | halt << "thread_id is out of range";
|
|---|
| 52 | }
|
|---|
| 53 | threads[thread_id].store(object);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void assert_id(id_t thread_id) {
|
|---|
| 57 | assert(is_valid_id(thread_id));
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | bool is_valid_id(id_t thread_id) {
|
|---|
| 61 | if (thread_id < 0 || thread_id >= static_cast<id_t>(_countof(threads)) || thread_id == Serial::INVALID_VALUE) {
|
|---|
| 62 | return false;
|
|---|
| 63 | }
|
|---|
| 64 | return true;
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | ExitNotifier exitNotifier /* XXX depends on id_serial */;
|
|---|
| 69 | int auto_joiner_index = 0;
|
|---|
| 70 | AutoJoiner auto_joiners[MIL_MAX_THREADS];
|
|---|
| 71 | atomic<int> auto_join_native_id_once;
|
|---|
| 72 | atomic<thread::native_id_t> auto_join_native_id;
|
|---|
| 73 |
|
|---|
| 74 | static void check_id() {
|
|---|
| 75 | if (!auto_join_native_id_once.load()) {
|
|---|
| 76 | auto_join_native_id_once.store(1);
|
|---|
| 77 | auto_join_native_id.store(thread::get_native_id());
|
|---|
| 78 | }
|
|---|
| 79 | if (auto_join_native_id.load() != thread::get_native_id()) {
|
|---|
| 80 | halt << "native thread id is not match [" << auto_join_native_id.load() << "] != [" << thread::get_native_id() << "]";
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void set_auto_join_body(AutoJoiner& a) {
|
|---|
| 85 | check_id();
|
|---|
| 86 |
|
|---|
| 87 | auto_joiners[auto_joiner_index] = a;
|
|---|
| 88 | auto_joiner_index++;
|
|---|
| 89 | if (auto_joiner_index >= MIL_MAX_THREADS) {
|
|---|
| 90 | halt << "auto_joiner_index is too big [" << auto_joiner_index << "]";
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | void do_auto_join() {
|
|---|
| 96 | check_id();
|
|---|
| 97 |
|
|---|
| 98 | for (int i = 0; i < auto_joiner_index; i++) {
|
|---|
| 99 | auto_joiners[i].post_exit(auto_joiners[i].obj, auto_joiners[i].event_memory);
|
|---|
| 100 | }
|
|---|
| 101 | for (int i = 0; i < auto_joiner_index; i++) {
|
|---|
| 102 | auto_joiners[i].join(auto_joiners[i].obj);
|
|---|
| 103 | }
|
|---|
| 104 | auto_joiner_index = 0;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #undef filter_exception_and_start
|
|---|
| 111 | extern "C" int filter_exception_and_start(int argc, char** argv, int (*start)(int, char**)) {
|
|---|
| 112 | return start(argc, argv);
|
|---|
| 113 | /*
|
|---|
| 114 | try {
|
|---|
| 115 | return start(argc, argv);
|
|---|
| 116 | }
|
|---|
| 117 | //catch (boost::exception& e)
|
|---|
| 118 | //{
|
|---|
| 119 | // debug << "c++ boost::exception [" << boost::diagnostic_information(e) << "]";
|
|---|
| 120 | //}
|
|---|
| 121 | catch (std::exception& e) {
|
|---|
| 122 | //debug << "c++ std::exception [" << e.what() << "]";
|
|---|
| 123 | //_ftprintf(stderr, gettext("c++ std::exception [%s]\n"), e.what());
|
|---|
| 124 | fprintf(stderr, gettext("c++ std::exception [%s]\n"), e.what()); // e.what() -> char
|
|---|
| 125 | }
|
|---|
| 126 | return 1;
|
|---|
| 127 | */
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | #ifdef __OBJC__
|
|---|
| 131 | namespace mil {
|
|---|
| 132 | class CocoaThreadManager : boost::noncopyable {
|
|---|
| 133 | Mutex mutex;
|
|---|
| 134 | NSPort* port_matrix[MIL_MAX_THREADS][MIL_MAX_THREADS];
|
|---|
| 135 | NSConnection* send_connection_matrix[MIL_MAX_THREADS][MIL_MAX_THREADS];
|
|---|
| 136 | NSConnection* receive_connection_matrix[MIL_MAX_THREADS][MIL_MAX_THREADS];
|
|---|
| 137 | id proxies[MIL_MAX_THREADS][MIL_MAX_THREADS];
|
|---|
| 138 | public:
|
|---|
| 139 | CocoaThreadManager() {
|
|---|
| 140 | synchronized (mutex) {
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | ~CocoaThreadManager() {
|
|---|
| 145 | synchronized (mutex) {
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | id getProxy(int sender, int receiver) {
|
|---|
| 150 | synchronized (mutex) {
|
|---|
| 151 | if (proxies[sender][receiver]) {
|
|---|
| 152 | return proxies[sender][receiver];
|
|---|
| 153 | }
|
|---|
| 154 | port_matrix[sender][receiver] = [NSPort port];
|
|---|
| 155 | port_matrix[receiver][sender] = [NSPort port];
|
|---|
| 156 | send_connection_matrix[sender][receiver] =
|
|---|
| 157 | [NSConnection connectionWithReceivePort:port_matrix[sender][receiver] sendPort:port_matrix[receiver][sender]];
|
|---|
| 158 |
|
|---|
| 159 | proxies[sender][receiver] = [send_connection_matrix[sender][receiver] rootProxy];
|
|---|
| 160 | return proxies[sender][receiver];
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void setProxy(int receiver, id object) {
|
|---|
| 165 | synchronized (mutex) {
|
|---|
| 166 | for (unsigned int sender = 0; sender < _countof(send_connection_matrix[0]); sender++) {
|
|---|
| 167 | if (send_connection_matrix[sender][receiver] && !receive_connection_matrix[sender][receiver]) {
|
|---|
| 168 | receive_connection_matrix[sender][receiver] =
|
|---|
| 169 | [NSConnection connectionWithReceivePort:port_matrix[receiver][sender] sendPort:port_matrix[sender][receiver]];
|
|---|
| 170 | [receive_connection_matrix[sender][receiver] setRootObject: object];
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | };
|
|---|
| 176 |
|
|---|
| 177 | }
|
|---|
| 178 | #endif
|
|---|