~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to base/thread.h

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <pthread.h>
38
38
#endif
39
39
 
40
 
#ifdef OS_WINDOWS
41
 
#define BEGINTHREAD(src, stack, func, arg, flag, id) \
42
 
     (HANDLE)_beginthreadex((void *)(src), (unsigned)(stack), \
43
 
                       (unsigned(_stdcall *)(void *))(func), (void *)(arg), \
44
 
                       (unsigned)(flag), (unsigned *)(id))
45
 
#endif
46
 
 
47
40
namespace mozc {
48
41
 
49
42
class Thread {
50
43
 public:
51
 
  virtual void Run() {}
 
44
  virtual void Run() { }
52
45
 
53
46
  void Start() {
54
47
    if (IsRunning()) {
57
50
 
58
51
    Detach();
59
52
#ifdef OS_WINDOWS
60
 
    DWORD id = 0;
61
 
    handle_ = BEGINTHREAD(0, 0, &Thread::Wrapper, this, 0, &id);
 
53
    handle_ = reinterpret_cast<HANDLE>(
 
54
                  _beginthreadex(NULL, 0, &Thread::WrapperForWindows,
 
55
                                 this, 0, NULL));
62
56
#else
63
57
    is_running_ = true;
64
 
    if (0 != pthread_create(&handle_, 0, &Thread::Wrapper,
 
58
    if (0 != pthread_create(&handle_, 0, &Thread::WrapperForPOSIX,
65
59
                            static_cast<void *>(this))) {
66
60
      is_running_ = false;
67
61
    }
88
82
    if (!joinable_) {
89
83
      return;
90
84
    }
 
85
    if (!handle_) {
 
86
      return;
 
87
    }
91
88
#ifdef OS_WINDOWS
92
89
    ::WaitForSingleObject(handle_, INFINITE);
93
90
    ::CloseHandle(handle_);
94
91
    handle_ = NULL;
95
92
#else
96
93
    pthread_join(handle_, NULL);
 
94
    handle_ = 0;
97
95
#endif
98
96
  }
99
97
 
100
98
  // This method is not encouraged especiialy in Windows.
101
99
  void Terminate() {
 
100
    if (handle_) {
102
101
#ifdef OS_WINDOWS
103
 
    ::TerminateThread(handle_, 0);
 
102
      ::TerminateThread(handle_, 0);
 
103
      handle_ = NULL;
104
104
#else
105
 
    pthread_cancel(handle_);
 
105
      pthread_cancel(handle_);
 
106
      handle_ = 0;
106
107
#endif
 
108
    }
107
109
  }
108
110
 
109
111
  Thread() : handle_(0),
126
128
    handle_ = 0;
127
129
  }
128
130
 
129
 
  static void* Wrapper(void *ptr) {
130
 
    Thread *p = static_cast<Thread *>(ptr);
131
 
#ifndef OS_WINDOWS
 
131
#if defined(OS_WINDOWS)
 
132
  static unsigned __stdcall WrapperForWindows(void *ptr) {
 
133
    Thread *p = static_cast<Thread *>(ptr);
 
134
    p->Run();
 
135
    return 0;
 
136
  }
 
137
#else
 
138
  static void * WrapperForPOSIX(void *ptr) {
 
139
    Thread *p = static_cast<Thread *>(ptr);
132
140
    pthread_cleanup_push(&Thread::Cleanup, static_cast<void *>(&p->is_running_));
133
 
#endif
134
141
    p->Run();
135
 
#ifndef OS_WINDOWS
136
142
    pthread_cleanup_pop(1);
137
 
#endif
138
143
    return NULL;
139
144
  }
 
145
#endif
140
146
 
141
147
#ifndef OS_WINDOWS
142
148
  static void Cleanup(void *ptr) {