summaryrefslogtreecommitdiff
path: root/PACE/pace/win32/semaphore.c
blob: 23728df5c296412e2064e1d80fd5b924636ddf78 (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
/* $Id$

 * =============================================================================
 *
 * = LIBRARY
 *    pace
 *
 * = FILENAME
 *    pace/win32/semaphore.c
 *
 * = AUTHOR
 *    Luther Baker
 *
 * ============================================================================= */

#include "pace/semaphore.h"

#if !defined (PACE_HAS_INLINE)
# include "pace/win32/semaphore.inl"
#endif /* ! PACE_HAS_INLINE */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
# include <stdio.h>
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_close (pace_sem_t * sem)
{
  PACE_WIN32CALL_RETURN
    (PACE_ADAPT_RETVAL
     (CloseHandle (sem), pace_result_), int, -1);
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_destroy (pace_sem_t * sem)
{
  PACE_WIN32CALL_RETURN
    (PACE_ADAPT_RETVAL
     (CloseHandle (sem), pace_result_), int, -1);
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_init (pace_sem_t * sem, int pshared, unsigned int value)
{
  PACE_UNUSED_ARG (pshared);

  /* Create the semaphore with its value initialized to <count> and
     its maximum value initialized to <max>.

     How do we want to call CreateSemaphore? What about the char *?
   */
  *sem = CreateSemaphore (0, value, 2147483647, "noname");

  if (*sem == 0)
    {
      PACE_FAIL_RETURN (-1);
    }
  /* NOTREACHED */
  else
    {
      return 0;
    }
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_post (pace_sem_t * sem)
{
  PACE_WIN32CALL_RETURN
    (PACE_ADAPT_RETVAL
     (ReleaseSemaphore (*sem, 1, 0), pace_result_), int, -1);
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_trywait (pace_sem_t * sem)
{
  int result = WaitForSingleObject (*sem, 0);

  if (result == WAIT_OBJECT_0)
    {
      return 0;
    }
  else
    {
      if (result == WAIT_TIMEOUT)
        {
          errno = EBUSY;
        }
      else
        {
          errno = GetLastError ();
        }
      /* This is a hack, we need to find an appropriate mapping... */
      return -1;
    }
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */

#if (PACE_HAS_POSIX_NONUOF_FUNCS)
int
sem_wait (pace_sem_t * sem)
{
  switch (WaitForSingleObject (*sem, INFINITE))
    {
    case WAIT_OBJECT_0:
      return 0;
    default:
      /* This is a hack, we need to find an appropriate mapping... */
      errno = GetLastError ();
      return -1;
    }
}
#endif /* PACE_HAS_POSIX_NONUOF_FUNCS */