RWLockImplWin32.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 
23 #include "icl_core_thread/Common.h"
25 
26 namespace icl_core {
27 namespace thread {
28 
30  : m_number_of_writer(0),
31  m_number_of_reader(0),
32  m_writer_pid(0)
33 {
34  m_reader_mutex_event = CreateEvent(NULL, FALSE, TRUE, NULL);
35  m_writer_mutex = CreateMutex(NULL, FALSE, NULL);
36 }
37 
39 {
40  CloseHandle(m_reader_mutex_event);
41  CloseHandle(m_writer_mutex);
42 }
43 
45 {
46  return readLock(INFINITE);
47 }
48 
49 // ReadLock with absolute timeout
51 {
52  return readLock(impl::getRelativeTimeout(timeout));
53 }
54 
55 // ReadLock with relative timeout
57 {
58  return readLock(DWORD(timeout.toMSec()));
59 }
60 
62 {
63  // get the reader access
64  bool ret = false;
66  {
67  if (m_reader_pid.empty())
68  {
69  ret = WaitForSingleObject(m_reader_mutex_event, timeout) == WAIT_OBJECT_0;
70  }
71  if (ret || !m_reader_pid.empty())
72  {
73  ret = true;
74  m_reader_pid.push_back(GetCurrentThreadId());
75  }
77  }
78  return ret;
79 }
80 
82 {
83  return readLock(0);
84 }
85 
87 {
88  return writeLock(INFINITE);
89 }
90 
91 // WriteLock with absolute timeout
93 {
94  return writeLock(impl::getRelativeTimeout(timeout));
95 }
96 
97 // WriteLock with relative timeout
99 {
100  return writeLock(DWORD(timeout.toMSec()));
101 }
102 
104 {
105  bool ret = (WaitForSingleObject(m_writer_mutex, timeout) == WAIT_OBJECT_0)
106  && (WaitForSingleObject(m_reader_mutex_event, timeout) == WAIT_OBJECT_0);
107  if (ret)
108  {
109  m_writer_pid = GetCurrentThreadId();
111  }
112  return ret;
113 }
114 
116 {
117  return writeLock(0);
118 }
119 
121 {
122  int thread_pid = GetCurrentThreadId();
123 
124  // writer unlock
125  if (thread_pid == m_writer_pid)
126  {
127  ReleaseMutex(m_writer_mutex);
129  if (m_number_of_writer == 0)
130  {
131  m_writer_pid = 0;
132  SetEvent(m_reader_mutex_event);
133  }
134  }
135  // search for reader
136  else
137  {
138  if (m_reader_access_lock.lock(TimeSpan(30000, 0)))
139  {
140  std::vector<int>::iterator iter;
141  for (iter = m_reader_pid.begin(); iter != m_reader_pid.end(); iter++)
142  {
143  if (thread_pid == *iter)
144  {
145  m_reader_pid.erase(iter);
146  if (m_reader_pid.empty())
147  {
148  SetEvent(m_reader_mutex_event);
149  };
150  break;
151  }
152  }
154  }
155  }
156 }
157 
158 }
159 }
Represents absolute times.
Definition: TimeStamp.h:61
int64_t toMSec() const
May result in an overflow if seconds are too large.
Definition: TimeSpan.cpp:163
Contains icl_core::thread::tMutex.
const TimeSpan timeout(1, 0)
Repesents absolute times.
Definition: TimeSpan.h:46
Contains icl_core::thread::RWLockImplWin32.
TimeSpan getRelativeTimeout(const TimeStamp &timeout_absolute)
Definition: Common.h:46


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58