ScaleOut C++ Native Client API  5.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
lock_ticket.h
1 /*
2  Copyright (c) 2013 by ScaleOut Software, Inc.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 #include "soss_svccli.h"
21 #include "soss_key.h"
22 #include "boost/thread.hpp"
23 
24 namespace sosscli {
25 
32 {
33 public:
34 
35  /* Constructors */
36 
38  LockTicket(void);
39 
43  LockTicket(SOSS_OBJKEY const &key, const int32_t ticket);
44 
47  LockTicket(const LockTicket &other);
48 
49  // Destructor.
50  virtual ~LockTicket(void);
51 
52  /* Lock ticket operations */
53 
55  void release(void);
56 
57  /* Accessors */
58 
61  bool locked(void) const;
62 
65  bool is_null_ticket(void) const;
66 
67  /* operator overloads */
68 
70  LockTicket& operator=(const LockTicket& rhs);
71 
78  inline operator int32_t() const
79  {
80  // if the LockTicket is not yet initialized, return native null lock ticket
81  if(!state_)
82  return SOSSLIB_NULL_LOCK_TKT;
83 
84  return this->state_->lock_ticket_;
85  }
86 
87 protected:
88 
89 private:
90 
91  /* Nested Classes */
92 
93  class LockTicketState : private boost::noncopyable
94  {
95  public:
96  LockTicketState(SOSS_OBJKEY const &key, const int32_t ticket);
97 
100  ~LockTicketState(void);
101 
103  SOSS_OBJKEY obj_key_;
104 
106  int32_t lock_ticket_;
107 
109  bool locked_;
110  };
111 
112  typedef boost::shared_ptr<LockTicketState> lock_state_t;
113  lock_state_t state_;
114  boost::mutex ticket_mutex_;
115 };
116 
117 }