A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DataPacket.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 #ifndef DATA_PACKET_H
19 #define DATA_PACKET_H
20 
21 // by Jia, Aug 4th
22 
23 #include <pthread.h>
24 
25 class DataPacket {
26 
27 private:
28  // bytes
29  void* bytes;
30 
31  // size
32  size_t size;
33 
34  // mutex
35  pthread_mutex_t mutex;
36 
37 
38  // reference count
39  int refCount;
40 
41 public:
42  // constructor
43  DataPacket(void* bytes, size_t size) {
44  this->bytes = bytes;
45  this->size = size;
46  this->refCount = 0;
47  pthread_mutex_init(&mutex, nullptr);
48  }
49 
50  // destructor
52  pthread_mutex_destroy(&mutex);
53  }
54 
55  // get bytes
56  void* getBytes() {
57  return this->bytes;
58  }
59 
60  // get size
61  size_t getSize() {
62  return this->size;
63  }
64 
65  // get reference count
66  int getRefCount() {
67  return this->refCount;
68  }
69 
70  // increment reference count
71  void incRefCount() {
72  pthread_mutex_lock(&mutex);
73  this->refCount++;
74  pthread_mutex_unlock(&mutex);
75  }
76 
77  // decrement reference count
78  void decRefCount() {
79  pthread_mutex_lock(&mutex);
80  this->refCount--;
81  pthread_mutex_unlock(&mutex);
82  if (this->refCount < 0) {
83  std::cout << "WARNING: data packet reference count < 0" << std::endl;
84  }
85  }
86 };
87 
88 
89 #endif
size_t getSize()
Definition: DataPacket.h:61
void * bytes
Definition: DataPacket.h:29
int getRefCount()
Definition: DataPacket.h:66
size_t size
Definition: DataPacket.h:32
DataPacket(void *bytes, size_t size)
Definition: DataPacket.h:43
int refCount
Definition: DataPacket.h:39
pthread_mutex_t mutex
Definition: DataPacket.h:35
void * getBytes()
Definition: DataPacket.h:56
void decRefCount()
Definition: DataPacket.h:78
void incRefCount()
Definition: DataPacket.h:71