BRL-CAD
thread.cpp
Go to the documentation of this file.
1 /* T H R E A D . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <vector>
24 
25 #ifdef HAVE_PTHREAD_H
26 # include <pthread.h>
27 #endif
28 
29 #include "bu/parallel.h"
30 
31 
32 #if defined(HAVE_THREAD_LOCAL)
33 
34 static thread_local int thread_cpu = 0;
35 
36 #elif defined(HAVE___THREAD)
37 
38 static __thread int thread_cpu = 0;
39 
40 #elif defined(HAVE___DECLSPEC_THREAD)
41 
42 static __declspec(thread) int thread_cpu;
43 
44 #elif defined(HAVE_PTHREAD_H)
45 
46 template<typename T>
47 class ThreadLocal
48 {
49 private:
50  pthread_key_t key;
51  std::vector<T*> vals;
52 protected:
53  void set(T& val) {
54  T* value = new T(val);
55 
57  vals.push_back(value);
59 
60  pthread_setspecific(key, value);
61  }
62  T* get() {
63  T* val = static_cast<T*>(pthread_getspecific(key));
64  return val;
65  }
66 public:
67  ThreadLocal() {
68  T init = 0;
69  pthread_key_create(&key, NULL);
70  set(init);
71  }
72  ~ThreadLocal() {
73  pthread_key_delete(key);
74 
76  while (!vals.empty()) {
77  delete vals.back();
78  vals.pop_back();
79  }
80  vals.clear();
82  }
83  ThreadLocal& operator=(T& val) {
84  set(val);
85  return *this;
86  }
87  bool operator!() {
88  return (get() == NULL);
89  }
90  operator T() {
91  T* val = get();
92  if (!val)
93  return 0;
94  return *val;
95  }
96 };
97 static ThreadLocal<int> thread_cpu;
98 
99 #else
100 # error "Unrecognized thread local storage method for this platform"
101 #endif
102 
103 
104 extern "C" {
105 
106 
107 void
109 {
110  thread_cpu = cpu;
111 }
112 
113 
114 int
116 {
117  return thread_cpu;
118 }
119 
120 
121 } /* extern "C" */
122 
123 /*
124  * Local Variables:
125  * mode: C
126  * tab-width: 8
127  * indent-tabs-mode: t
128  * c-file-style: "stroustrup"
129  * End:
130  * ex: shiftwidth=4 tabstop=8
131  */
Definition: db_flip.c:35
int thread_get_cpu(void)
Definition: thread.cpp:115
void bu_semaphore_acquire(unsigned int i)
Definition: semaphore.c:180
Header file for the BRL-CAD common definitions.
void bu_semaphore_release(unsigned int i)
Definition: semaphore.c:218
void thread_set_cpu(int cpu)
Definition: thread.cpp:108
#define BU_SEM_THREAD
Definition: parallel.h:182