summaryrefslogtreecommitdiff
path: root/private.c
blob: a880042ef2cbbb902fd1c68b6d3b695073bf955f (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
/*
 * 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"

int
_pthread_getthreadindex(pthread_t thread)
{
  /* The hash 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.

     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 t = _PTHREAD_HASH_INDEX(*thread);
  int it = t; /* The initial thread index */

  while ((_pthread_threads_table[t])->thread != thread) {
    t++;
    if (t == PTHREAD_THREADS_MAX)
      t = 0; /* Wrap around to the first slot */
    if (t == it)
      return -1; /* Failed to find the thread */
  }
  return t;
}