CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clientctx.cc
Go to the documentation of this file.
1 
5 #include "clientctx.h"
6 
7 #include <cassert>
8 
9 #include "interrupt.h"
10 #include "util/concurrency.h"
11 #include "util/smalloc.h"
12 
13 using namespace std; // NOLINT
14 
16 
17 
19  delete instance_;
20  instance_ = NULL;
21 }
22 
23 
25  lock_tls_blocks_ = reinterpret_cast<pthread_mutex_t *>(
26  smalloc(sizeof(pthread_mutex_t)));
27  int retval = pthread_mutex_init(lock_tls_blocks_, NULL);
28  assert(retval == 0);
29 }
30 
31 
33  pthread_mutex_destroy(lock_tls_blocks_);
34  free(lock_tls_blocks_);
35 
36  for (unsigned i = 0; i < tls_blocks_.size(); ++i) {
37  delete tls_blocks_[i];
38  }
39 
40  int retval = pthread_key_delete(thread_local_storage_);
41  assert(retval == 0);
42 }
43 
44 
46  if (instance_ == NULL) {
47  instance_ = new ClientCtx();
48  int retval =
49  pthread_key_create(&instance_->thread_local_storage_, TlsDestructor);
50  assert(retval == 0);
51  }
52 
53  return instance_;
54 }
55 
56 
57 void ClientCtx::Get(uid_t *uid, gid_t *gid, pid_t *pid, InterruptCue **ic) {
58  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
59  pthread_getspecific(thread_local_storage_));
60  if ((tls == NULL) || !tls->is_set) {
61  *uid = -1;
62  *gid = -1;
63  *pid = -1;
64  *ic = NULL;
65  } else {
66  *uid = tls->uid;
67  *gid = tls->gid;
68  *pid = tls->pid;
69  *ic = tls->interrupt_cue;
70  }
71 }
72 
73 
75  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
76  pthread_getspecific(thread_local_storage_));
77  if (tls == NULL)
78  return false;
79 
80  return tls->is_set;
81 }
82 
83 
84 void ClientCtx::Set(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic) {
85  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
86  pthread_getspecific(thread_local_storage_));
87 
88  if (tls == NULL) {
89  tls = new ThreadLocalStorage(uid, gid, pid, ic);
90  int retval = pthread_setspecific(thread_local_storage_, tls);
91  assert(retval == 0);
92  MutexLockGuard lock_guard(lock_tls_blocks_);
93  tls_blocks_.push_back(tls);
94  } else {
95  tls->uid = uid;
96  tls->gid = gid;
97  tls->pid = pid;
98  tls->interrupt_cue = ic;
99  tls->is_set = true;
100  }
101 }
102 
103 
104 void ClientCtx::TlsDestructor(void *data) {
105  ThreadLocalStorage *tls = static_cast<ClientCtx::ThreadLocalStorage *>(data);
106  delete tls;
107 
108  assert(instance_);
109  MutexLockGuard lock_guard(instance_->lock_tls_blocks_);
110  for (vector<ThreadLocalStorage *>::iterator i =
111  instance_->tls_blocks_.begin(), iEnd = instance_->tls_blocks_.end();
112  i != iEnd; ++i)
113  {
114  if ((*i) == tls) {
115  instance_->tls_blocks_.erase(i);
116  break;
117  }
118  }
119 }
120 
121 
123  ThreadLocalStorage *tls = static_cast<ThreadLocalStorage *>(
124  pthread_getspecific(thread_local_storage_));
125  if (tls != NULL) {
126  tls->is_set = false;
127  tls->uid = -1;
128  tls->gid = -1;
129  tls->pid = -1;
130  tls->interrupt_cue = NULL;
131  }
132 }
bool is_set
either not yet set or deliberately unset
Definition: clientctx.h:40
void Get(uid_t *uid, gid_t *gid, pid_t *pid, InterruptCue **ic)
Definition: clientctx.cc:57
static ClientCtx * instance_
Definition: clientctx.h:53
assert((mem||(size==0))&&"Out Of Memory")
bool IsSet()
Definition: clientctx.cc:74
InterruptCue * interrupt_cue
A non-owning pointer.
Definition: clientctx.h:39
static void TlsDestructor(void *data)
Definition: clientctx.cc:104
void Unset()
Definition: clientctx.cc:122
~ClientCtx()
Definition: clientctx.cc:32
void Set(uid_t uid, gid_t gid, pid_t pid, InterruptCue *ic)
Definition: clientctx.cc:84
Definition: mutex.h:42
static void CleanupInstance()
Definition: clientctx.cc:18
static ClientCtx * GetInstance()
Definition: clientctx.cc:45