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

Revision 34853, 4.6 kB (checked in by saturday06, 4 years ago)

pty

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