MEL
Microthread & Execution library
CriticalSection_mthread.h
1 #pragma once
2 /*
3  * SPDX-FileCopyrightText: 2005,2022 Daniel Barrientos
4  * <danivillamanin@gmail.com>
5  *
6  * SPDX-License-Identifier: MIT
7  */
8 #include <atomic>
9 #include <tasking/Event_mthread.h>
10 namespace mel
11 {
12  namespace tasking
13  {
20  template <bool multithread = true> class CriticalSection_mthread
21  {
22  public:
30  bool enter();
31  void leave();
32  // TODO try enter
33  private:
35  std::shared_ptr<mel::tasking::Process> mOwner;
36  std::atomic<int> mCount; // number of times owner as acquired
37  // section
38  };
40  template <> class MEL_API CriticalSection_mthread<false>
41  {
42  public:
50  bool enter();
51  void leave();
52  // TODO try enter
53  private:
55  std::shared_ptr<::mel::tasking::Process> mOwner;
56  int mCount; // number of times owner as acquired section
57  };
58  namespace _private
59  {
60  class _WrapperBase
61  {
62  public:
63  virtual ~_WrapperBase() {}
64 
65  private:
66  };
67  template <bool ismth> class _Wrapper : public _WrapperBase
68  {
69  public:
70  _Wrapper( CriticalSection_mthread<ismth>& cs ) : mCS( cs )
71  {
72  cs.enter();
73  }
74  ~_Wrapper() { mCS.leave(); }
75 
76  private:
77  CriticalSection_mthread<ismth>& mCS;
78  };
79 
80  } // namespace _private
83  {
84  public:
86  template <bool ismth>
88  : mWrapper( new _private::_Wrapper<ismth>( cs ) )
89  {
90  }
91 
92  private:
93  // CriticalSection_mthread& mCS;
94  std::unique_ptr<_private::_WrapperBase> mWrapper;
95  };
96 
97  } // namespace tasking
98 } // namespace mel
A critical section for synchronizing microthreads (AKA Process)
Definition: CriticalSection_mthread.h:21
class similar to Event Class (which is for thread synchronization) but for Process (with Microthread ...
Definition: Event_mthread.h:167
Definition: CriticalSection_mthread.h:83
Lock_mthread(CriticalSection_mthread< ismth > &cs)
enter critical section.
Definition: CriticalSection_mthread.h:87
Definition: Callback_Impl.h:11