summaryrefslogtreecommitdiff
path: root/mysys/my_semaphore.c
blob: aa216cbc2897da6784b7a27c5d5a59840568bd7b (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
/* Copyright (C) 2002 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
  Simple implementation of semaphores, needed to compile MySQL on systems
  that doesn't support semaphores.
*/

#include <my_global.h>
#include <my_semaphore.h>
#include <errno.h>

#if !defined(__WIN__) && !defined(HAVE_SEMAPHORE_H) && defined(THREAD)

int sem_init(sem_t * sem, int pshared, uint value)
{
  sem->count=value;
  pthread_cond_init(&sem->cond, 0);
  pthread_mutex_init(&sem->mutex, 0);
  return 0;
}

int sem_destroy(sem_t * sem)
{
  int err1,err2;
  err1=pthread_cond_destroy(&sem->cond);
  err2=pthread_mutex_destroy(&sem->mutex);
  if (err1 || err2)
  {
    errno=err1 ? err1 : err2;
    return -1;
  }
  return 0;
}

int sem_wait(sem_t * sem)
{
  if ((errno=pthread_mutex_lock(&sem->mutex)))
    return -1;
  while (!sem->count)
    pthread_cond_wait(&sem->cond, &sem->mutex);
  if (errno)
    return -1;
  sem->count--; /* mutex is locked here */
  pthread_mutex_unlock(&sem->mutex);
  return 0;
}

int sem_trywait(sem_t * sem)
{
  if ((errno=pthread_mutex_lock(&sem->mutex)))
    return -1;
  if (sem->count)
    sem->count--;
  else
    errno=EAGAIN;
  pthread_mutex_unlock(&sem->mutex);
  return errno ? -1 : 0;
}


int sem_post(sem_t * sem)
{
  if ((errno=pthread_mutex_lock(&sem->mutex)))
    return -1;
  sem->count++;
  pthread_mutex_unlock(&sem->mutex);    /* does it really matter what to do */
  pthread_cond_signal(&sem->cond);      /* first: x_unlock or x_signal ?    */
  return 0;
}

int sem_post_multiple(sem_t * sem, uint count)
{
  if ((errno=pthread_mutex_lock(&sem->mutex)))
    return -1;
  sem->count+=count;
  pthread_mutex_unlock(&sem->mutex);    /* does it really matter what to do */
  pthread_cond_broadcast(&sem->cond);   /* first: x_unlock or x_broadcast ? */
  return 0;
}

int sem_getvalue(sem_t * sem, uint *sval)
{
  if ((errno=pthread_mutex_lock(&sem->mutex)))
    return -1;
  *sval=sem->count;
  pthread_mutex_unlock(&sem->mutex);
  return 0;
}

#endif /* !defined(__WIN__) && !defined(HAVE_SEMAPHORE_H) && defined(THREAD) */