root/lang/objective-cplusplus/i3/trunk/src/mil/include/mil/StaticData.cpp @ 36267

Revision 36267, 5.7 kB (checked in by saturday06, 3 years ago)

asasasas

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