Belle II Software development
MMutex.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/system/MMutex.h"
9
10using namespace Belle2;
11
12MMutex::MMutex() {}
13
14MMutex::MMutex(const MMutex& mutex)
15{
16 *this = mutex;
17}
18
19MMutex::MMutex(void* addr)
20{
21 set((pthread_mutex_t*)addr);
22}
23
24MMutex::~MMutex() {}
25
26bool MMutex::init(void* addr)
27{
28 set(addr);
29 init();
30 return true;
31}
32
33bool MMutex::init()
34{
35 pthread_mutexattr_t attr;
36 pthread_mutexattr_init(&attr);
37 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
38 pthread_mutex_init(m_mu, &attr);
39 pthread_mutexattr_destroy(&attr);
40 return true;
41}
42
43bool MMutex::set(void* addr)
44{
45 m_mu = (pthread_mutex_t*)addr;
46 return true;
47}
48
49bool MMutex::lock()
50{
51 if (pthread_mutex_lock(m_mu) != 0) {
52 return false;
53 } else {
54 return true;
55 }
56}
57
58bool MMutex::trylock()
59{
60 if (pthread_mutex_lock(m_mu) != 0) {
61 return false;
62 }
63 return true;
64}
65
66bool MMutex::unlock()
67{
68 if (pthread_mutex_unlock(m_mu) != 0) {
69 return true;
70 } else {
71 return false;
72 }
73}
74
75bool MMutex::destroy()
76{
77 if (pthread_mutex_destroy(m_mu) != 0) {
78 return true;
79 } else {
80 return false;
81 }
82}
83
84const MMutex& MMutex::operator=(const MMutex& mutex)
85{
86 m_mu = mutex.m_mu;
87 return *this;
88}
Abstract base class for different kinds of events.