MMDevice
Loading...
Searching...
No Matches
DeviceThreads.h
Go to the documentation of this file.
1
2// FILE: DeviceThreads.h
3// PROJECT: Micro-Manager
4// SUBSYSTEM: MMDevice - Device adapter kit
5//-----------------------------------------------------------------------------
6// DESCRIPTION: Cross-platform wrapper class for using threads in MMDevices
7//
8// AUTHOR: Nenad Amodaj, nenad@amodaj.com 11/27/2007
9// COPYRIGHT: University of California, San Francisco, 2007
10// LICENSE: This file is distributed under the BSD license.
11// License text is included with the source distribution.
12//
13// This file is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty
15// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16//
17// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
20
21#pragma once
22
23#ifdef _WIN32
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26#else
27 #include <pthread.h>
28#endif
29
34{
35public:
36 MMDeviceThreadBase() : thread_(0) {}
38
39 virtual int svc() = 0;
40
41 virtual int activate()
42 {
43#ifdef _WIN32
44 DWORD id;
45 thread_ = CreateThread(NULL, 0, ThreadProc, this, 0, &id);
46#else
47 pthread_create(&thread_, NULL, ThreadProc, this);
48#endif
49 return 0; // TODO: return thread id
50 }
51
52 void wait()
53 {
54#ifdef _WIN32
55 WaitForSingleObject(thread_, INFINITE);
56#else
57 pthread_join(thread_, NULL);
58#endif
59 }
60
61private:
62 // Forbid copying
64 MMDeviceThreadBase& operator=(const MMDeviceThreadBase&);
65
66#ifdef _WIN32
67 HANDLE
68#else
69 pthread_t
70#endif
71 thread_;
72
73 static
74#ifdef _WIN32
75 DWORD WINAPI
76#else
77 void*
78#endif
79 ThreadProc(void* param)
80 {
81 MMDeviceThreadBase* pThrObj = (MMDeviceThreadBase*) param;
82#ifdef _WIN32
83 return pThrObj->svc();
84#else
85 pThrObj->svc();
86 return (void*) 0;
87#endif
88 }
89};
90
95{
96public:
98 {
99#ifdef _WIN32
100 InitializeCriticalSection(&lock_);
101#else
102 pthread_mutexattr_t a;
103 pthread_mutexattr_init(&a);
104 pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
105 pthread_mutex_init(&lock_, &a);
106 pthread_mutexattr_destroy(&a);
107#endif
108 }
109
111 {
112#ifdef _WIN32
113 DeleteCriticalSection(&lock_);
114#else
115 pthread_mutex_destroy(&lock_);
116#endif
117 }
118
119 void Lock()
120 {
121#ifdef _WIN32
122 EnterCriticalSection(&lock_);
123#else
124 pthread_mutex_lock(&lock_);
125#endif
126 }
127
128 void Unlock()
129 {
130#ifdef _WIN32
131 LeaveCriticalSection(&lock_);
132#else
133 pthread_mutex_unlock(&lock_);
134#endif
135 }
136
137private:
138 // Forbid copying
140 MMThreadLock& operator=(const MMThreadLock&);
141
142#ifdef _WIN32
143 CRITICAL_SECTION
144#else
145 pthread_mutex_t
146#endif
147 lock_;
148};
149
151{
152public:
153 MMThreadGuard(MMThreadLock& lock) : lock_(&lock)
154 {
155 lock_->Lock();
156 }
157
158 MMThreadGuard(MMThreadLock* lock) : lock_(lock)
159 {
160 if (lock != 0)
161 lock_->Lock();
162 }
163
164 bool isLocked() {return lock_ == 0 ? false : true;}
165
167 {
168 if (lock_ != 0)
169 lock_->Unlock();
170 }
171
172private:
173 // Forbid copying
175 MMThreadGuard& operator=(const MMThreadGuard&);
176
177 MMThreadLock* lock_;
178};
Base class for threads in MM devices.
Definition DeviceThreads.h:34
virtual ~MMDeviceThreadBase()
Definition DeviceThreads.h:37
void wait()
Definition DeviceThreads.h:52
virtual int svc()=0
virtual int activate()
Definition DeviceThreads.h:41
MMDeviceThreadBase()
Definition DeviceThreads.h:36
Definition DeviceThreads.h:151
MMThreadGuard(MMThreadLock *lock)
Definition DeviceThreads.h:158
bool isLocked()
Definition DeviceThreads.h:164
MMThreadGuard(MMThreadLock &lock)
Definition DeviceThreads.h:153
~MMThreadGuard()
Definition DeviceThreads.h:166
Critical section lock.
Definition DeviceThreads.h:95
void Lock()
Definition DeviceThreads.h:119
~MMThreadLock()
Definition DeviceThreads.h:110
void Unlock()
Definition DeviceThreads.h:128
MMThreadLock()
Definition DeviceThreads.h:97