summaryrefslogtreecommitdiff
path: root/private.c
blob: dc7e111fb76ee771b5f96d2491fc25ddc8ecc67c (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * private.c
 *
 * Description:
 * This translation unit implements routines which are private to
 * the implementation and may be used throughout it.
 */

#include <windows.h>
#include <process.h>
#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 * this;

  /* CRITICAL SECTION */
  pthread_mutex_lock(&_pthread_count_mutex);

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

  this = &_pthread_threads_table[_PTHREAD_HASH_INDEX(thread)];

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

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

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

  _pthread_threads_count++;
  entry = this;

  pthread_mutex_unlock(&_pthread_count_mutex);
  /* END CRITICAL SECTION */

  return 0;
}

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

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

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

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

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

  return this;
}

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

  /* CRITICAL SECTION */
  pthread_mutex_lock(&_pthread_count_mutex);

  /* If this is not NULL then we are removing an entry for a
     failed thread start. If this is NULL we need to get this
     here within the critical section. */
  if (this == NULL)
    {
      this = _PTHREAD_THIS;
    }

  if (this->thread != NULL)
    {
      this->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. */
    }

  pthread_mutex_unlock(&_pthread_count_mutex);
  /* END CRITICAL SECTION */
}