My Project
singthreads.h
Go to the documentation of this file.
1 #ifndef _SINGULAR_LIBTHREAD
2 #define _SINGULAR_LIBTHREAD
3 
4 #include <cstdlib>
5 #include <new>
6 #include <string>
7 #include <vector>
8 
9 namespace LibThread {
10 
11 class ThreadState;
12 typedef void *(*ThreadFunc)(ThreadState *, void *);
13 
14 ThreadState *createThread(ThreadFunc body, void **arg, const char **error);
15 void *joinThread(ThreadState *thread);
16 
17 template <typename T>
18 T *shared_alloc(std::size_t n);
19 template <typename T>
20 T *shared_alloc0(std::size_t n);
21 template <typename T>
22 T shared_free(T *p);
23 
24 #ifdef USE_SHARED_ALLOCATOR
25 
26 template <class T>
27 struct SharedAllocator {
28  typedef T value_type;
29  SharedAllocator() {
30  // do nothing
31  }
32  template <class U> SharedAllocator(const SharedAllocator<U>&) noexcept {
33  // do nothing
34  }
35  T *allocate(std::size_t n) {
36  T *p = shared_alloc0<T>(n);
37  if (p) return p;
38  throw std::bad_alloc();
39  }
40  void deallocate(T *p, std::size_t n) noexcept {
41  shared_free(p);
42  }
43 };
44 
45 typedef std::basic_string<char, std::char_traits<char>, SharedAllocator<char> >
46  SharedString;
47 
48 template <typename T>
49 class SharedVector : public std::vector<T, SharedAllocator<T> > {
50 public:
51  SharedVector() : std::vector<T, SharedAllocator<T>>() { }
52  SharedVector(std::size_t n, const T& value = T()) : std::vector<T, SharedAllocator<T> >(n, value) { }
53  SharedVector(SharedVector &other) : std::vector<T, SharedAllocator<T> >(&other) { }
54 };
55 
56 #endif
57 
58 }
59 
60 #endif // _SINGULAR_LIBTHREAD
int p
Definition: cfModGcd.cc:4080
void error(const char *fmt,...)
Definition: emacs.cc:55
STATIC_VAR jList * T
Definition: janet.cc:30
ThreadState * createThread(void *(*thread_func)(ThreadState *, void *), void *arg)
Definition: shared.cc:1469
void * joinThread(ThreadState *ts)
Definition: shared.cc:1474
T * shared_alloc(std::size_t n)
void *(* ThreadFunc)(ThreadState *, void *)
Definition: singthreads.h:12
T shared_free(T *p)
Definition: thread.cc:52
T * shared_alloc0(std::size_t n)