Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarThread.hpp
blob: 4198737a8899e5f2f25309aaaedcc7576a2d945c (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#pragma once

#include "StarException.hpp"
#include "StarString.hpp"

namespace Star {

STAR_STRUCT(ThreadImpl);
STAR_STRUCT(ThreadFunctionImpl);
STAR_STRUCT(MutexImpl);
STAR_STRUCT(ConditionVariableImpl);
STAR_STRUCT(RecursiveMutexImpl);

template <typename Return>
class ThreadFunction;

class Thread {
public:
  // Implementations of this method should sleep for at least the given amount
  // of time, but may sleep for longer due to scheduling.
  static void sleep(unsigned millis);

  // Sleep a more precise amount of time, but uses more resources to do so.
  // Should be less likely to sleep much longer than the given amount of time.
  static void sleepPrecise(unsigned millis);

  // Yield this thread, offering the opportunity to reschedule.
  static void yield();

  static unsigned numberOfProcessors();

  template <typename Function, typename... Args>
  static ThreadFunction<decltype(std::declval<Function>()(std::declval<Args>()...))> invoke(String const& name, Function&& f, Args&&... args);

  Thread(String const& name);
  Thread(Thread&&);
  // Will not automatically join!  ALL implementations of this class MUST call
  // join() in their most derived constructors, or not rely on the destructor
  // joining.
  virtual ~Thread();

  Thread& operator=(Thread&&);

  // Start a thread that is currently in the joined state.  Returns true if the
  // thread was joined and is now started, false if the thread was not joined.
  bool start();

  // Wait for a thread to finish and re-join with the thread, on completion
  // isJoined() will be false.  Returns true if the thread was joinable, and is
  // now joined, false if the thread was already joined.
  bool join();

  // Returns false when this thread been started without being joined.  This is
  // subtlely different than "!isRunning()", in that the thread could have
  // completed its work, but a thread *must* be joined before being restarted.
  bool isJoined() const;

  // Returns false before start() has been called, true immediately after
  // start() has been called, and false once the run() method returns.
  bool isRunning() const;

  String name();

protected:
  virtual void run() = 0;

private:
  unique_ptr<ThreadImpl> m_impl;
};

// Wraps a function call and calls in another thread, very nice lightweight
// one-shot alternative to deriving from Thread.  Handles exceptions in a
// different way from Thread, instead of logging the exception, the exception
// is forwarded and re-thrown during the call to finish().
template <>
class ThreadFunction<void> {
public:
  ThreadFunction();
  ThreadFunction(ThreadFunction&&);

  // Automatically starts the given function, ThreadFunction can also be
  // constructed with Thread::invoke, which is a shorthand.
  ThreadFunction(function<void()> function, String const& name);

  // Automatically calls finish, though BEWARE that often times this is quite
  // dangerous, and this is here mostly as a fallback.  The natural destructor
  // order for members of a class is often wrong, and if the function throws,
  // since this destructor calls finish it will throw.
  ~ThreadFunction();

  ThreadFunction& operator=(ThreadFunction&&);

  // Waits on function finish if function is assigned and started, otherwise
  // does nothing.  If the function threw an exception, it will be re-thrown
  // here (on the first call to finish() only).
  void finish();

  // Returns whether the ThreadFunction::finish method been called and the
  // ThreadFunction has stopped.  Also returns true when the ThreadFunction has
  // been default constructed.
  bool isFinished() const;
  // Returns false if the thread function has stopped running, whether or not
  // finish() has been called.
  bool isRunning() const;

  // Equivalent to !isFinished()
  explicit operator bool() const;

  String name();

private:
  unique_ptr<ThreadFunctionImpl> m_impl;
};

template <typename Return>
class ThreadFunction {
public:
  ThreadFunction();
  ThreadFunction(ThreadFunction&&);
  ThreadFunction(function<Return()> function, String const& name);

  ~ThreadFunction();

  ThreadFunction& operator=(ThreadFunction&&);

  // Finishes the thread, moving and returning the final value of the function.
  // If the function threw an exception, finish() will rethrow that exception.
  // May only be called once, otherwise will throw InvalidMaybeAccessException.
  Return finish();

  bool isFinished() const;
  bool isRunning() const;

  explicit operator bool() const;

  String name();

private:
  ThreadFunction<void> m_function;
  shared_ptr<Maybe<Return>> m_return;
};

// *Non* recursive mutex lock, for use with ConditionVariable
class Mutex {
public:
  Mutex();
  Mutex(Mutex&&);
  ~Mutex();

  Mutex& operator=(Mutex&&);

  void lock();

  // Attempt to acquire the mutex without blocking.
  bool tryLock();

  void unlock();

private:
  friend struct ConditionVariableImpl;
  unique_ptr<MutexImpl> m_impl;
};

class ConditionVariable {
public:
  ConditionVariable();
  ConditionVariable(ConditionVariable&&);
  ~ConditionVariable();

  ConditionVariable& operator=(ConditionVariable&&);

  // Atomically unlocks the mutex argument and waits on the condition.  On
  // acquiring the condition, atomically returns and re-locks the mutex.  Must
  // lock the mutex before calling.  If millis is given, waits for a maximum of
  // the given milliseconds only.
  void wait(Mutex& mutex, Maybe<unsigned> millis = {});

  // Wake one waiting thread.  The calling thread for is allowed to either hold
  // or not hold the mutex that the threads waiting on the condition are using,
  // both will work and result in slightly different scheduling.
  void signal();

  // Wake all threads, policy for holding the mutex is the same for signal().
  void broadcast();

private:
  unique_ptr<ConditionVariableImpl> m_impl;
};

// Recursive mutex lock.  lock() may be called many times freely by the same
// thread, but unlock() must be called an equal number of times to unlock it.
class RecursiveMutex {
public:
  RecursiveMutex();
  RecursiveMutex(RecursiveMutex&&);
  ~RecursiveMutex();

  RecursiveMutex& operator=(RecursiveMutex&&);

  void lock();

  // Attempt to acquire the mutex without blocking.
  bool tryLock();

  void unlock();

private:
  unique_ptr<RecursiveMutexImpl> m_impl;
};

// RAII for mutexes.  Locking and unlocking are always safe, MLocker will never
// attempt to lock the held mutex more than once, or unlock more than once, and
// destruction will always unlock the mutex *iff* it is actually locked.
// (Locked here refers to one specific MLocker *itself* locking the mutex, not
// whether the mutex is locked *at all*, so it is sensible to use with
// RecursiveMutex)
template <typename MutexType>
class MLocker {
public:
  // Pass false to lock to start unlocked
  MLocker(MutexType& ref, bool lock = true);
  ~MLocker();

  MLocker(MLocker const&) = delete;
  MLocker& operator=(MLocker const&) = delete;

  MutexType& mutex();

  void unlock();
  void lock();
  bool tryLock();

private:
  MutexType& m_mutex;
  bool m_locked;
};
typedef MLocker<Mutex> MutexLocker;
typedef MLocker<RecursiveMutex> RecursiveMutexLocker;

class ReadersWriterMutex {
public:
  ReadersWriterMutex();

  void readLock();
  bool tryReadLock();
  void readUnlock();

  void writeLock();
  bool tryWriteLock();
  void writeUnlock();

private:
  Mutex m_mutex;
  ConditionVariable m_readCond;
  ConditionVariable m_writeCond;
  unsigned m_readers;
  unsigned m_writers;
  unsigned m_readWaiters;
  unsigned m_writeWaiters;
};

class ReadLocker {
public:
  ReadLocker(ReadersWriterMutex& rwlock, bool startLocked = true);
  ~ReadLocker();

  ReadLocker(ReadLocker const&) = delete;
  ReadLocker& operator=(ReadLocker const&) = delete;

  void unlock();
  void lock();
  bool tryLock();

private:
  ReadersWriterMutex& m_lock;
  bool m_locked;
};

class WriteLocker {
public:
  WriteLocker(ReadersWriterMutex& rwlock, bool startLocked = true);
  ~WriteLocker();

  WriteLocker(WriteLocker const&) = delete;
  WriteLocker& operator=(WriteLocker const&) = delete;

  void unlock();
  void lock();
  bool tryLock();

private:
  ReadersWriterMutex& m_lock;
  bool m_locked;
};

class SpinLock {
public:
  SpinLock();

  void lock();
  bool tryLock();
  void unlock();

private:
  atomic_flag m_lock;
};
typedef MLocker<SpinLock> SpinLocker;

template <typename MutexType>
MLocker<MutexType>::MLocker(MutexType& ref, bool l)
  : m_mutex(ref), m_locked(false) {
  if (l)
    lock();
}

template <typename MutexType>
MLocker<MutexType>::~MLocker() {
  unlock();
}

template <typename MutexType>
MutexType& MLocker<MutexType>::mutex() {
  return m_mutex;
}

template <typename MutexType>
void MLocker<MutexType>::unlock() {
  if (m_locked) {
    m_mutex.unlock();
    m_locked = false;
  }
}

template <typename MutexType>
void MLocker<MutexType>::lock() {
  if (!m_locked) {
    m_mutex.lock();
    m_locked = true;
  }
}

template <typename MutexType>
bool MLocker<MutexType>::tryLock() {
  if (!m_locked) {
    if (m_mutex.tryLock())
      m_locked = true;
  }

  return m_locked;
}

template <typename Function, typename... Args>
ThreadFunction<decltype(std::declval<Function>()(std::declval<Args>()...))> Thread::invoke(String const& name, Function&& f, Args&&... args) {
  return {bind(std::forward<Function>(f), std::forward<Args>(args)...), name};
}

template <typename Return>
ThreadFunction<Return>::ThreadFunction() {}

template <typename Return>
ThreadFunction<Return>::ThreadFunction(ThreadFunction&&) = default;

template <typename Return>
ThreadFunction<Return>::ThreadFunction(function<Return()> function, String const& name) {
  m_return = make_shared<Maybe<Return>>();
  m_function = ThreadFunction<void>([function = std::move(function), retValue = m_return]() {
      *retValue = function();
    }, name);
}

template <typename Return>
ThreadFunction<Return>::~ThreadFunction() {
  m_function.finish();
}

template <typename Return>
ThreadFunction<Return>& ThreadFunction<Return>::operator=(ThreadFunction&&) = default;

template <typename Return>
Return ThreadFunction<Return>::finish() {
  m_function.finish();
  return m_return->take();
}

template <typename Return>
bool ThreadFunction<Return>::isFinished() const {
  return m_function.isFinished();
}

template <typename Return>
bool ThreadFunction<Return>::isRunning() const {
  return m_function.isRunning();
}

template <typename Return>
ThreadFunction<Return>::operator bool() const {
  return !isFinished();
}

template <typename Return>
String ThreadFunction<Return>::name() {
  return m_function.name();
}

inline SpinLock::SpinLock() {
  m_lock.clear();
}

inline void SpinLock::lock() {
  while (m_lock.test_and_set(std::memory_order_acquire))
    ;
}

inline void SpinLock::unlock() {
  m_lock.clear(std::memory_order_release);
}

inline bool SpinLock::tryLock() {
  return !m_lock.test_and_set(std::memory_order_acquire);
}

}