summaryrefslogtreecommitdiff
path: root/omnithread/threaddata.cc
blob: d54c4391445845c980cead21c702155539bf457f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//				Package : omnithread
// omnithread/threaddata.cc	Created : 10/2000 dpg1
//
//    Copyright (C) 2000 AT&T Laboratories Cambridge
//
//    This file is part of the omnithread library
//
//    The omnithread library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Library General Public
//    License as published by the Free Software Foundation; either
//    version 2 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Library General Public License for more details.
//
//    You should have received a copy of the GNU Library General Public
//    License along with this library; if not, write to the Free
//    Software Foundation, Inc., 51 Franklin Street, Boston, MA  
//    02110-1301, USA
//

// Implementation of per-thread data

#ifndef INSIDE_THREAD_IMPL_CC
#error "threaddata.cc must be #included by a thread implementation."
#endif


static omni_thread::key_t allocated_keys = 0;

omni_thread::key_t
omni_thread::allocate_key()
{
  omni_mutex_lock l(*next_id_mutex);
  return ++allocated_keys;
}

omni_thread::value_t*
omni_thread::set_value(key_t k, value_t* v)
{
  if (k == 0) return 0;
  if (k > _value_alloc) {
    next_id_mutex->lock();
    key_t alloc = allocated_keys;
    next_id_mutex->unlock();

    if (k > alloc) return 0;

    value_t** nv = new value_t*[alloc];
    key_t i = 0;
    if (_values) {
      for (; i < _value_alloc; i++)
	nv[i] = _values[i];
      delete [] _values;
    }
    for (; i < alloc; i++)
      nv[i] = 0;

    _values = nv;
    _value_alloc = alloc;
  }
  if (_values[k-1]) delete _values[k-1];
  _values[k-1] = v;
  return v;
}

omni_thread::value_t*
omni_thread::get_value(key_t k)
{
  if (k > _value_alloc) return 0;
  return _values[k-1];
}

omni_thread::value_t*
omni_thread::remove_value(key_t k)
{
  if (k > _value_alloc) return 0;
  value_t* v = _values[k-1];
  _values[k-1] = 0;
  return v;
}