NEURON
tqueue.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <InterViews/resource.h>
3 #include "tqueue.h"
4 #include "pool.h"
5 
6 #include "classreg.h"
7 #include "nrnoc2iv.h"
8 
9 #define PROFILE 0
10 #include "profile.h"
11 
12 #define DOCHECK 0
13 
14 #if COLLECT_TQueue_STATISTICS
15 #define STAT(arg) ++arg;
16 #else
17 #define STAT(arg) /**/
18 #endif
19 
20 static const char* errmess_;
21 
22 static double insert(void* v) {
23  TQueue* q = (TQueue*) v;
24  q->insert(*getarg(1), (void*) 1);
25  return 1.;
26 }
27 static double print(void* v) {
28  TQueue* q = (TQueue*) v;
29  q->print();
30  return 1.;
31 }
32 
33 static double least(void* v) {
34  TQueue* q = (TQueue*) v;
35  TQItem* i = q->least();
36  double x = -1e9;
37  if (i) {
38  x = i->t_;
39  }
40  return x;
41 }
42 static double rmleast(void* v) {
43  TQueue* q = (TQueue*) v;
44  TQItem* i = q->least();
45  double x = -1e9;
46  if (i) {
47  x = i->t_;
48  q->remove(i);
49  }
50  return x;
51 }
52 
53 static double mvleast(void* v) {
54  TQueue* q = (TQueue*) v;
55  q->move_least(*getarg(1));
56  return 1.;
57 }
58 
59 static double remove(void* v) {
60  TQueue* q = (TQueue*) v;
61  q->remove(q->find(*getarg(1)));
62  return 1.;
63 }
64 
65 static double find(void* v) {
66  TQueue* q = (TQueue*) v;
67  TQItem* i = q->find(*getarg(1));
68  double x = -1e9;
69  if (i) {
70  x = i->t_;
71  q->remove(i);
72  }
73  return x;
74 }
75 static double stats(void* v) {
76  TQueue* q = (TQueue*) v;
77  q->statistics();
78  return 1.;
79 }
80 
81 static Member_func members[] = {"insrt",
82  insert,
83  "least",
84  least,
85  "move_least",
86  mvleast,
87  "remove_least",
88  rmleast,
89  "remove",
90  remove,
91  "find",
92  find,
93  "stats",
94  stats,
95  "printf",
96  print,
97  0,
98  0};
99 
100 static void* cons(Object*) {
101  assert(0);
102  TQueue* q = new TQueue(0);
103  return (void*) q;
104 }
105 
106 static void destruct(void* v) {
107  TQueue* q = (TQueue*) v;
108  delete q;
109 }
110 
111 void TQueue_reg() {
112  class2oc("TQueue", cons, destruct, members, NULL, NULL, NULL);
113 }
114 
115 //----------------
116 
117 implementPool(TQItemPool, TQItem)
118 #if BBTQ == 0
119 #include <bbtqueue.cpp>
120 #endif
121 
122 #if BBTQ == 1
123 #include <rbtqueue.cpp>
124 #endif
125 
126 #if BBTQ == 2
127 #include <sptqueue.cpp>
128 #endif
129 
130 #if BBTQ == 3
131 #include <sptfifoq.cpp>
132 #endif
133 
134 #if BBTQ == 4
135 #include <spt2queue.cpp>
136 #endif
137 
138 #if BBTQ == 5
139 #include <sptbinq.cpp>
140 #endif
141 
142  SelfQueue::SelfQueue(TQItemPool* tp, int mkmut) {
143  MUTCONSTRUCT(mkmut)
144  tpool_ = tp;
145  head_ = nil;
146 }
147 SelfQueue::~SelfQueue() {
148  remove_all();
150 }
151 TQItem* SelfQueue::insert(void* d) {
152  MUTLOCK
153  TQItem* q = tpool_->alloc();
154  q->left_ = nil;
155  q->right_ = head_;
156  if (head_) {
157  head_->left_ = q;
158  }
159  head_ = q;
160  q->data_ = d;
161  MUTUNLOCK
162  return q;
163 }
164 void* SelfQueue::remove(TQItem* q) {
165  MUTLOCK
166  if (q->left_) {
167  q->left_->right_ = q->right_;
168  }
169  if (q->right_) {
170  q->right_->left_ = q->left_;
171  }
172  if (q == head_) {
173  head_ = q->right_;
174  }
175  tpool_->hpfree(q);
176  MUTUNLOCK
177  return q->data_;
178 }
179 void SelfQueue::remove_all() {
180  MUTLOCK
181  for (TQItem* q = first(); q; q = next(q)) {
182  tpool_->hpfree(q);
183  }
184  head_ = nil;
185  MUTUNLOCK
186 }
#define nil
Definition: enter-scope.h:36
Definition: bbtqueue.h:6
static int first
Definition: fmenu.cpp:190
#define assert(ex)
Definition: hocassrt.h:32
#define getarg
Definition: hocdec.h:15
#define v
Definition: md1redef.h:4
#define i
Definition: md1redef.h:12
Item * next(Item *item)
Definition: list.cpp:88
size_t q
#define MUTCONSTRUCT(mkmut)
Definition: nrnmutdec.h:70
#define MUTDESTRUCT
Definition: nrnmutdec.h:71
#define MUTLOCK
Definition: nrnmutdec.h:72
#define MUTUNLOCK
Definition: nrnmutdec.h:73
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1560
void remove_all(EventButton)
#define NULL
Definition: sptree.h:16
Definition: hocdec.h:227
implementPool(TQItemPool, TQItem) SelfQueue
Definition: tqueue.cpp:117
static Member_func members[]
Definition: tqueue.cpp:81
static void * cons(Object *)
Definition: tqueue.cpp:100
void TQueue_reg()
Definition: tqueue.cpp:111
static void destruct(void *v)
Definition: tqueue.cpp:106
static double remove(void *v)
Definition: tqueue.cpp:59
static double find(void *v)
Definition: tqueue.cpp:65
static double mvleast(void *v)
Definition: tqueue.cpp:53
static const char * errmess_
Definition: tqueue.cpp:20
static double insert(void *v)
Definition: tqueue.cpp:22
static double rmleast(void *v)
Definition: tqueue.cpp:42
static double stats(void *v)
Definition: tqueue.cpp:75
static double print(void *v)
Definition: tqueue.cpp:27
static double least(void *v)
Definition: tqueue.cpp:33