-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceThreads.h
More file actions
178 lines (157 loc) · 3.76 KB
/
DeviceThreads.h
File metadata and controls
178 lines (157 loc) · 3.76 KB
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
///////////////////////////////////////////////////////////////////////////////
// FILE: DeviceThreads.h
// PROJECT: Micro-Manager
// SUBSYSTEM: MMDevice - Device adapter kit
//-----------------------------------------------------------------------------
// DESCRIPTION: Cross-platform wrapper class for using threads in MMDevices
//
// AUTHOR: Nenad Amodaj, nenad@amodaj.com 11/27/2007
// COPYRIGHT: University of California, San Francisco, 2007
// LICENSE: This file is distributed under the BSD license.
// License text is included with the source distribution.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
#pragma once
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <pthread.h>
#endif
/**
* @brief Base class for threads in MM devices.
*/
class MMDeviceThreadBase
{
public:
MMDeviceThreadBase() : thread_(0) {}
virtual ~MMDeviceThreadBase() {}
virtual int svc() = 0;
virtual int activate()
{
#ifdef _WIN32
DWORD id;
thread_ = CreateThread(NULL, 0, ThreadProc, this, 0, &id);
#else
pthread_create(&thread_, NULL, ThreadProc, this);
#endif
return 0; // TODO: return thread id
}
void wait()
{
#ifdef _WIN32
WaitForSingleObject(thread_, INFINITE);
#else
pthread_join(thread_, NULL);
#endif
}
private:
// Forbid copying
MMDeviceThreadBase(const MMDeviceThreadBase&);
MMDeviceThreadBase& operator=(const MMDeviceThreadBase&);
#ifdef _WIN32
HANDLE
#else
pthread_t
#endif
thread_;
static
#ifdef _WIN32
DWORD WINAPI
#else
void*
#endif
ThreadProc(void* param)
{
MMDeviceThreadBase* pThrObj = (MMDeviceThreadBase*) param;
#ifdef _WIN32
return pThrObj->svc();
#else
pThrObj->svc();
return (void*) 0;
#endif
}
};
/**
* @brief Critical section lock.
*/
class MMThreadLock
{
public:
MMThreadLock()
{
#ifdef _WIN32
InitializeCriticalSection(&lock_);
#else
pthread_mutexattr_t a;
pthread_mutexattr_init(&a);
pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lock_, &a);
pthread_mutexattr_destroy(&a);
#endif
}
~MMThreadLock()
{
#ifdef _WIN32
DeleteCriticalSection(&lock_);
#else
pthread_mutex_destroy(&lock_);
#endif
}
void Lock()
{
#ifdef _WIN32
EnterCriticalSection(&lock_);
#else
pthread_mutex_lock(&lock_);
#endif
}
void Unlock()
{
#ifdef _WIN32
LeaveCriticalSection(&lock_);
#else
pthread_mutex_unlock(&lock_);
#endif
}
private:
// Forbid copying
MMThreadLock(const MMThreadLock&);
MMThreadLock& operator=(const MMThreadLock&);
#ifdef _WIN32
CRITICAL_SECTION
#else
pthread_mutex_t
#endif
lock_;
};
class MMThreadGuard
{
public:
MMThreadGuard(MMThreadLock& lock) : lock_(&lock)
{
lock_->Lock();
}
MMThreadGuard(MMThreadLock* lock) : lock_(lock)
{
if (lock != 0)
lock_->Lock();
}
bool isLocked() {return lock_ == 0 ? false : true;}
~MMThreadGuard()
{
if (lock_ != 0)
lock_->Unlock();
}
private:
// Forbid copying
MMThreadGuard(const MMThreadGuard&);
MMThreadGuard& operator=(const MMThreadGuard&);
MMThreadLock* lock_;
};