Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / lib / omnithread / threaddata.cc
1 //                              Package : omnithread
2 // omnithread/threaddata.cc     Created : 10/2000 dpg1
3 //
4 //    Copyright (C) 2000 AT&T Laboratories Cambridge
5 //
6 //    This file is part of the omnithread library
7 //
8 //    The omnithread library is free software; you can redistribute it and/or
9 //    modify it under the terms of the GNU Library General Public
10 //    License as published by the Free Software Foundation; either
11 //    version 2 of the License, or (at your option) any later version.
12 //
13 //    This library is distributed in the hope that it will be useful,
14 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //    Library General Public License for more details.
17 //
18 //    You should have received a copy of the GNU Library General Public
19 //    License along with this library; if not, write to the Free
20 //    Software Foundation, Inc., 51 Franklin Street, Boston, MA  
21 //    02110-1301, USA
22 //
23
24 // Implementation of per-thread data
25
26 #ifndef INSIDE_THREAD_IMPL_CC
27 #error "threaddata.cc must be #included by a thread implementation."
28 #endif
29
30
31 static omni_thread::key_t allocated_keys = 0;
32
33 omni_thread::key_t
34 omni_thread::allocate_key()
35 {
36   omni_mutex_lock l(*next_id_mutex);
37   return ++allocated_keys;
38 }
39
40 omni_thread::value_t*
41 omni_thread::set_value(key_t k, value_t* v)
42 {
43   if (k == 0) return 0;
44   if (k > _value_alloc) {
45     next_id_mutex->lock();
46     key_t alloc = allocated_keys;
47     next_id_mutex->unlock();
48
49     if (k > alloc) return 0;
50
51     value_t** nv = new value_t*[alloc];
52     key_t i = 0;
53     if (_values) {
54       for (; i < _value_alloc; i++)
55         nv[i] = _values[i];
56       delete [] _values;
57     }
58     for (; i < alloc; i++)
59       nv[i] = 0;
60
61     _values = nv;
62     _value_alloc = alloc;
63   }
64   if (_values[k-1]) delete _values[k-1];
65   _values[k-1] = v;
66   return v;
67 }
68
69 omni_thread::value_t*
70 omni_thread::get_value(key_t k)
71 {
72   if (k > _value_alloc) return 0;
73   return _values[k-1];
74 }
75
76 omni_thread::value_t*
77 omni_thread::remove_value(key_t k)
78 {
79   if (k > _value_alloc) return 0;
80   value_t* v = _values[k-1];
81   _values[k-1] = 0;
82   return v;
83 }