Welcome to mirror list, hosted at ThFree Co, Russian Federation.

thread.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c02e283a73437f71587c3a334d0797225ed7b320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "thread.hpp"
#include "assert.hpp"

#if defined(OMIM_OS_BADA)
  #include <FBaseRtThreadThread.h>
#elif defined(OMIM_OS_WINDOWS_NATIVE)
  #include "../std/windows.hpp"
#else
  #include <pthread.h>
  #if defined (OMIM_OS_ANDROID)
    /// External implementations are in android/jni code
    void AndroidThreadAttachToJVM();
    void AndroidThreadDetachFromJVM();
  #endif
#endif


namespace threads
{
#if defined(OMIM_OS_BADA)
  /// BADA specific implementation
  class ThreadImpl : public Osp::Base::Runtime::Thread
  {
    typedef Osp::Base::Runtime::Thread BaseT;
    IRoutine * m_pRoutine;

  public:
    /// Doesn't own pRoutine
    ThreadImpl() : m_pRoutine(0)
    {
      result error = Construct(Osp::Base::Runtime::THREAD_TYPE_WORKER,
                               Osp::Base::Runtime::Thread::DEFAULT_STACK_SIZE,
                               Osp::Base::Runtime::THREAD_PRIORITY_MID);
      ASSERT_EQUAL(error, E_SUCCESS, ("Constructing thread error"));
    }

    int Create(IRoutine * pRoutine)
    {
      m_pRoutine = pRoutine;
      return BaseT::Start();
    }

    int Join()
    {
      return BaseT::Join();
    }

    virtual Osp::Base::Object * Run()
    {
      m_pRoutine->Do();
      return 0;
    }
  };

#elif defined(OMIM_OS_WINDOWS_NATIVE)
  /// Windows native implementation
  class ThreadImpl
  {
    HANDLE m_handle;

  public:
    ThreadImpl() : m_handle(0) {}

    ~ThreadImpl()
    {
      if (m_handle)
        ::CloseHandle(m_handle);
    }

    static DWORD WINAPI WindowsWrapperThreadProc(void * p)
    {
      IRoutine * pRoutine = reinterpret_cast<IRoutine *>(p);
      pRoutine->Do();
      return 0;
    }

    int Create(IRoutine * pRoutine)
    {
      int error = 0;
      m_handle = ::CreateThread(NULL, 0, &WindowsWrapperThreadProc, reinterpret_cast<void *>(pRoutine), 0, NULL);
      if (0 == m_handle)
        error = ::GetLastError();
      return error;
    }

    int Join()
    {
      int error = 0;
      if (WAIT_OBJECT_0 != ::WaitForSingleObject(m_handle, INFINITE))
        error = ::GetLastError();
      return error;
    }
  };
  // end of Windows Native implementation

#else
  /// POSIX pthreads implementation
  class ThreadImpl
  {
    pthread_t m_handle;

  public:
    ThreadImpl() {}

    static void * PthreadsWrapperThreadProc(void * p)
    {
#ifdef OMIM_OS_ANDROID
      // Attach thread to JVM, implemented in android/jni code
      AndroidThreadAttachToJVM();
#endif

      IRoutine * pRoutine = reinterpret_cast<IRoutine *>(p);
      pRoutine->Do();

#ifdef OMIM_OS_ANDROID
      // Detach thread from JVM, implemented in android/jni code
      AndroidThreadDetachFromJVM();
#endif

      ::pthread_exit(NULL);
      return NULL;
    }

    int Create(IRoutine * pRoutine)
    {
      return ::pthread_create(&m_handle, NULL, &PthreadsWrapperThreadProc, reinterpret_cast<void *>(pRoutine));
    }

    int Join()
    {
      return ::pthread_join(m_handle, NULL);
    }
  };
  //////////////////////// end of POSIX pthreads implementation
#endif

  /////////////////////////////////////////////////////////////////////
  // Thread wrapper implementation
  Thread::Thread() : m_impl(new ThreadImpl()), m_routine(0)
  {
  }

  Thread::~Thread()
  {
    delete m_impl;
  }

  bool Thread::Create(IRoutine * pRoutine)
  {
    ASSERT_EQUAL(m_routine, 0, ("Current implementation doesn't allow to reuse thread"));
    int error = m_impl->Create(pRoutine);
    if (0 != error)
    {
      ASSERT ( !"Thread creation failed with error", (error) );
      return false;
    }
    m_routine = pRoutine;
    return true;
  }

  void Thread::Cancel()
  {
    if (m_routine)
    {
      m_routine->Cancel();
      Join();
    }
  }

  void Thread::Join()
  {
    if (m_routine)
    {
      int const error = m_impl->Join();
      if (0 != error)
      {
        ASSERT ( !"Thread join failed. See error value.", (error) );
      }
    }
  }


  ThreadPool::ThreadPool(size_t reserve)
  {
    m_pool.reserve(reserve);
  }

  ThreadPool::~ThreadPool()
  {
    for (size_t i = 0; i < m_pool.size(); ++i)
    {
      delete m_pool[i].first;
      delete m_pool[i].second;
    }
  }

  void ThreadPool::Add(IRoutine * pRoutine)
  {
    ValueT v;
    v.first = new Thread();
    v.second = pRoutine;

    m_pool.push_back(v);

    v.first->Create(pRoutine);
  }

  void ThreadPool::Join()
  {
    for (size_t i = 0; i < m_pool.size(); ++i)
      m_pool[i].first->Join();
  }

  IRoutine * ThreadPool::GetRoutine(size_t i) const
  {
    return m_pool[i].second;
  }


  void Sleep(size_t ms)
  {
#ifdef OMIM_OS_WINDOWS
    ::Sleep(ms);
#else
    timespec t;
    t.tv_nsec =(ms * 1000000) % 1000000000;
    t.tv_sec = (ms * 1000000) / 1000000000;
    nanosleep(&t, 0);
#endif
  }

  ThreadID GetCurrentThreadID()
  {
#ifdef OMIM_OS_WINDOWS
    return ::GetCurrentThreadId();
#else
    return reinterpret_cast<void *>(pthread_self());
#endif
  }
}