summaryrefslogtreecommitdiff
path: root/private.c
blob: cee84e912e6ced9c5bea1dfc4f56557bffafac17 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * private.c
 *
 * Description:
 * This translation unit implements routines which are private to
 * the implementation and may be used throughout it.
 */

#include "pthread.h"
#include "implement.h"

/* The threads table works as follows:
   hash into the table,
   if the thread in this slot doesn't match then start single
   stepping from there until we find it, or we hit an empty slot, or
   we end up where we started from.

   The scheme should have these characteristics:
   - if the thread handle is a sequence number then the hash will
     succeed first time every time,
   - if the thread handle is a pseudo randomish value (eg. a pointer)
     then the hash should succeed first time most times.
 */

int
_pthread_new_thread_entry(pthread_t thread, _pthread_threads_thread_t * entry)
{
  _pthread_threads_thread_t * new;

  if (_pthread_threads_count >= PTHREAD_THREADS_MAX)
    {
      return EAGAIN;
    }

  new = &_pthread_threads_table[_PTHREAD_HASH_INDEX(thread)];

  while (new->thread != NULL)
    {
      new++;

      if (new == &_pthread_threads_table[PTHREAD_THREADS_MAX])
	{
	  /* Wrap to the top of the table. */
	  new == _pthread_threads_table;
	}
    }

  if (new->thread != NULL)
    {
      /* INTERNAL ERROR: There should be at least one slot left. */
      return ESRCH;
    }
  else
    {
      new->thread = thread;
      pthread_attr_init(&(new->attr));
      new->joinvalueptr = NULL;
      new->cancelstate = PTHREAD_CANCEL_ENABLE;
      new->canceltype = PTHREAD_CANCEL_DEFERRED;
      new->cancel_pending = FALSE;
      new->cleanupstack = NULL;
      new->destructorstack = NULL;
      new->forkpreparestack = NULL;
      new->forkparentstack = NULL;
      new->forkchildstack = NULL;
    }

  _pthread_threads_count++;
  entry = new;

  return 0;
}

_pthread_threads_thread *
_pthread_find_thread_entry(pthread_t thread)
{
  _pthread_threads_thread_t * entry;
  _pthread_threads_thread_t * start;

  start = entry = &_pthread_threads_table[_PTHREAD_HASH_INDEX(thread)];

  while (entry->thread != thread)
    {
      entry++;

      if (entry == &_pthread_threads_table[PTHREAD_THREADS_MAX])
	{
	  /* Wrap to top of table. */
	  entry = _pthread_threads_table;
	}
    }

  if (entry->thread == NULL || entry == start)
    {
      /* Failed to find the thread. */
      return NULL;
    }

  return entry;
}

void
_pthread_delete_thread_entry(_pthread_threads_thread_t * entry)
{
  /* We don't check that the thread has been properly cleaned up, so
     it had better be done already. */

  /* Remove the thread entry if necessary. */

  if (entry->thread != NULL)
    {
      entry->thread = NULL;
      
      if (_pthread_threads_count > 0)
	{
	  _pthread_threads_count--;
	}
      else
	{
	  /* FIXME: INTERNAL ERROR: This should not happen. */
	}
    }
  else
    {
      /* FIXME: INTERNAL ERROR: This should not happen. */
    }
}