OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
API documentation
Semaphore.cpp
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #include <OnixS/Core/Concurrency/Semaphore.h>
21 
23 
24 #include "NamespaceHelper.h"
25 
26 ONIXS_HANDLER_NAMESPACE_BEGIN
27 
29 {
30 public:
31  Impl(int initialCounter) :
32  sem_(initialCounter)
33  {
34  }
35 
37  {
38  }
39 
40  bool acquire(int timeoutInMs)
41  {
42  if (timeoutInMs == -1)
43  {
44  sem_.acquire();
45  return true;
46  }
47  else
48  {
49  const OnixS::Time::TotalSeconds seconds = timeoutInMs / 1000;
50  const OnixS::Time::Nanoseconds nanoseconds = (timeoutInMs % 1000) * 1000000;
51  const OnixS::Time::TimeSpan timeout(seconds, OnixS::Time::NanosecondSubseconds(nanoseconds));
52  return sem_.acquire(timeout);
53  }
54  }
55 
56  void release()
57  {
58  sem_.release();
59  }
60 
61 private:
62  OnixS::Concurrency::Semaphore sem_;
63 };
64 
65 Semaphore::Semaphore(int initialCounter) :
66  impl_(new Impl(initialCounter))
67 {
68 }
69 
71 {
72  delete impl_;
73 }
74 
75 bool Semaphore::acquire(int timeoutInMs)
76 {
77  return impl_->acquire(timeoutInMs);
78 }
79 
81 {
82  impl_->release();
83 }
84 
85 ONIXS_HANDLER_NAMESPACE_END