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#include <mutex>
24#include <thread>
25
32{
33public:
35
37 {
38 // Detaching on destruction may not be the ideal design, but the only one
39 // possible given previous behavior (which leaked the native handle).
40 if (thread_.joinable())
41 thread_.detach();
42 }
43
44 // Cannot copy a thread
47
48 // We also cannot safely support move because the thread calls svc() by
49 // reference.
52
58 virtual int svc() = 0;
59
60 void activate()
61 {
62 if (thread_.joinable())
63 thread_.detach();
64 thread_ = std::thread([this]() { (void)svc(); });
65 // Note: failure to create the thread will throw std::system_error, but
66 // that only happens upon resource exhaustion (normally rare; akin to
67 // std::bad_alloc). Most callers do not handle this, but terminating with
68 // an uncaught exception is preferable to silently continuing (as we
69 // previously did).
70 }
71
72 void wait()
73 {
74 // Calling wait() on an already-wait()ed thread, or a never-activate()ed
75 // thread, is dubious usage, but the previous implementation allowed it,
76 // at least on Windows. So we must remain lenient here.
77 if (thread_.joinable())
78 thread_.join();
79 }
80
81private:
82 std::thread thread_;
83};
84
92{
93public:
94 MMThreadLock() = default;
95
96 // Disallow moving because we always did and no reason to newly allow.
97 ~MMThreadLock() = default;
98 MMThreadLock(const MMThreadLock&) = delete;
102
103 void Lock()
104 {
105 mutex_.lock();
106 }
107
108 void Unlock()
109 {
110 mutex_.unlock();
111 }
112
113private:
114 std::recursive_mutex mutex_;
115};
116
117
124{
125public:
126 MMThreadGuard(MMThreadLock& lock) : lock_(&lock)
127 {
128 lock_->Lock();
129 }
130
131 MMThreadGuard(MMThreadLock* lock) : lock_(lock)
132 {
133 if (lock != nullptr)
134 lock_->Lock();
135 }
136
138 {
139 if (lock_ != nullptr)
140 lock_->Unlock();
141 }
142
143 MMThreadGuard(const MMThreadGuard&) = delete;
147
148private:
149 MMThreadLock* lock_;
150};
Base class for threads in MM devices.
Definition DeviceThreads.h:32
virtual ~MMDeviceThreadBase()
Definition DeviceThreads.h:36
void activate()
Definition DeviceThreads.h:60
void wait()
Definition DeviceThreads.h:72
MMDeviceThreadBase & operator=(const MMDeviceThreadBase &)=delete
MMDeviceThreadBase(const MMDeviceThreadBase &)=delete
virtual int svc()=0
This function is called on the new thread.
MMDeviceThreadBase & operator=(MMDeviceThreadBase &&)=delete
MMDeviceThreadBase(MMDeviceThreadBase &&)=delete
MMDeviceThreadBase()
Definition DeviceThreads.h:34
RAII object to acquire and auto-release MMThreadLock.
Definition DeviceThreads.h:124
MMThreadGuard(MMThreadGuard &&)=delete
MMThreadGuard(MMThreadLock *lock)
Definition DeviceThreads.h:131
MMThreadGuard & operator=(MMThreadGuard &&)=delete
MMThreadGuard(MMThreadLock &lock)
Definition DeviceThreads.h:126
MMThreadGuard(const MMThreadGuard &)=delete
MMThreadGuard & operator=(const MMThreadGuard &)=delete
~MMThreadGuard()
Definition DeviceThreads.h:137
Critical section lock.
Definition DeviceThreads.h:92
MMThreadLock(const MMThreadLock &)=delete
MMThreadLock()=default
MMThreadLock & operator=(const MMThreadLock &)=delete
~MMThreadLock()=default
void Lock()
Definition DeviceThreads.h:103
MMThreadLock & operator=(MMThreadLock &&)=delete
void Unlock()
Definition DeviceThreads.h:108
MMThreadLock(MMThreadLock &&)=delete