GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
archive_memory_pool.cpp
1 #include <graphlab/util/lock_free_pool.hpp>
2 #include <graphlab/rpc/dc_compile_parameters.hpp>
3 #include <vector>
4 #include <utility>
5 namespace graphlab {
6 namespace dc_impl {
7 struct pool_manager {
8  pthread_key_t thlocalkey;
9  typedef std::pair<oarchive, bool> thlocal_type;
10  static void pth_deleter(void* p) {
11  if (p != NULL) {
12  thlocal_type* arc = (thlocal_type*)(p);
13  if (arc->first.buf) free(arc->first.buf);
14  delete arc;
15  }
16  }
17  pool_manager(){
18  pthread_key_create(&thlocalkey, &pth_deleter);
19  }
20 
21  ~pool_manager() {
22  pthread_key_delete(thlocalkey);
23  }
24 };
25 
26 static pool_manager pool;
27 
28 oarchive* oarchive_from_pool() {
29  void* ptr = pthread_getspecific(pool.thlocalkey);
30  pool_manager::thlocal_type* p = (pool_manager::thlocal_type*)(ptr);
31  if (p == NULL) {
32  p = new pool_manager::thlocal_type;
33  p->second = false;
34  p->first.buf = (char*)malloc(BUFFER_RELINQUISH_LIMIT);
35  p->first.off = 0;
36  p->first.len = BUFFER_RELINQUISH_LIMIT;
37  pthread_setspecific(pool.thlocalkey, (void*)p);
38  }
39  if (p->second) return new oarchive;
40  p->second = true;
41  return &(p->first);
42 }
43 
44 void release_oarchive_to_pool(oarchive* oarc) {
45  void* ptr = pthread_getspecific(pool.thlocalkey);
46  pool_manager::thlocal_type* p = (pool_manager::thlocal_type*)(ptr);
47  if ((p != NULL) && &(p->first) == oarc) {
48  oarc->off = 0;
49  p->second = false;
50  } else {
51  if (oarc->buf) free(oarc->buf);
52  delete oarc;
53  }
54 }
55 
56 
57 
58 
59 } // dc_impl
60 } // graphlab