summaryrefslogtreecommitdiff
path: root/misc.c
blob: d39af50e20a1d118210598314786f8cea65b9662 (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
/*
 * misc.c
 *
 * Description:
 * This translation unit implements miscellaneous thread functions.
 */

#include <errno.h>

#include "pthread.h"

int
pthread_once(pthread_once_t *once_control,
	     void (*init_routine)(void))
{
  /* A flag, allocated per invocation, that indicates if the amotic
     test-and-set occured. */
  unsigned short flag = 0;

  if (once_control == NULL || init_routine == NULL)
    {
      return EINVAL;
    }

  /* An atomic test-and-set of the "once" flag. */
  pthread_mutex_lock(&once_control->lock);
  if (once_control->flag == 0)
    {
      flag = once_control->flag = 1;
    }
  pthread_mutex_unlock(&once_control->lock);

  if (flag)
    {
      /* Run the init routine. */
      init_routine();
    }
  
  return 0;
}

pthread_t
pthread_self(void)
{
  pthread_t ret;
  /* This TLS index is allocated on DLL load by dll.c */
  extern DWORD _pthread_threadID_TlsIndex;
 
  ret = (pthread_t) TlsGetValue(_pthread_threadID_TlsIndex);

  if (ret == 0)
    {
      /* FIXME: Oh no! This can't happen. */
    }

  return ret;
}

int
pthread_equal(pthread_t t1, pthread_t t2)
{
  return (t1 == t2);
}