Commit 8fa3c789 authored by monty@hundin.mysql.fi's avatar monty@hundin.mysql.fi
Browse files

Added support for semaphores in mysys.

(Needed for query cache for systems which doesn't have native semaphores)
parent 578a9d99
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1530,7 +1530,7 @@ AC_SUBST(MAKE_SHELL)
AC_CHECK_HEADERS(varargs.h stdarg.h dirent.h locale.h ndir.h sys/dir.h \
 sys/file.h sys/ndir.h sys/ptem.h sys/pte.h sys/select.h sys/stream.h \
 sys/mman.h curses.h termcap.h termio.h termbits.h asm/termbits.h grp.h \
paths.h)
paths.h semaphore.h)

# Already-done: strcasecmp
AC_CHECK_FUNCS(lstat putenv select setenv setlocale strcoll tcgetattr)
+13 −4
Original line number Diff line number Diff line
@@ -33,20 +33,29 @@

C_MODE_START

#ifndef __WIN__
#ifdef HAVE_SEMAPHORE_H
#include <semaphore.h>
#else

#ifdef __WIN__
typedef HANDLE sem_t;
#else
typedef struct {
  pthread_mutex_t mutex;
  pthread_cond_t  cond;
  uint            count;
} sem_t;
#endif

int sem_init(sem_t * sem, int pshared, unsigned int value);
int sem_destroy(sem_t * sem);
int sem_trywait(sem_t * sem);
int sem_wait(sem_t * sem);
int sem_post(sem_t * sem);
int sem_post_multiple(sem_t * sem,int count);
int sem_getvalue(sem_t * sem, int * sval);
int sem_post_multiple(sem_t * sem, unsigned int count);
int sem_getvalue(sem_t * sem, unsigned int * sval);

#endif /* __WIN__ */
#endif

C_MODE_END
#endif /* !_my_semaphore_h_ */
+1 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\
			my_quick.c my_lockmem.c my_static.c \
			my_getopt.c my_mkdir.c \
			default.c my_compress.c checksum.c raid.cc \
			my_net.c \
			my_net.c my_semaphore.c \
			my_vsnprintf.c charset.c my_bitmap.c my_bit.c md5.c \
			my_gethostbyname.c rijndael.c my_aes.c sha1.c
EXTRA_DIST =		thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
+2 −2
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ int handle_options(int *argc, char ***argv,
  uint opt_found, argvpos= 0, length, i;
  my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
          option_is_loose;
  char *progname= *(*argv), **pos, *optend, *prev_found;
  char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found;
  const struct my_option *optp;
  int error;

@@ -84,7 +84,7 @@ int handle_options(int *argc, char ***argv,
  (*argv)++; /*      --- || ----      */
  init_variables(longopts);

  for (pos= *argv; *pos; pos++)
  for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
  {
    char *cur_arg= *pos;
    if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */

mysys/my_semaphore.c

0 → 100644
+103 −0
Original line number Diff line number Diff line
/* 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>

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

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) */
Loading