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

Added support for semaphores in mysys.

(Needed for query cache for systems which doesn't have native semaphores)
parent 578a9d99
...@@ -1530,7 +1530,7 @@ AC_SUBST(MAKE_SHELL) ...@@ -1530,7 +1530,7 @@ AC_SUBST(MAKE_SHELL)
AC_CHECK_HEADERS(varargs.h stdarg.h dirent.h locale.h ndir.h sys/dir.h \ 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/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 \ 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 # Already-done: strcasecmp
AC_CHECK_FUNCS(lstat putenv select setenv setlocale strcoll tcgetattr) AC_CHECK_FUNCS(lstat putenv select setenv setlocale strcoll tcgetattr)
......
...@@ -33,20 +33,29 @@ ...@@ -33,20 +33,29 @@
C_MODE_START C_MODE_START
#ifndef __WIN__ #ifdef HAVE_SEMAPHORE_H
#include <semaphore.h> #include <semaphore.h>
#else #else
#ifdef __WIN__
typedef HANDLE sem_t; 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_init(sem_t * sem, int pshared, unsigned int value);
int sem_destroy(sem_t * sem); int sem_destroy(sem_t * sem);
int sem_trywait(sem_t * sem); int sem_trywait(sem_t * sem);
int sem_wait(sem_t * sem); int sem_wait(sem_t * sem);
int sem_post(sem_t * sem); int sem_post(sem_t * sem);
int sem_post_multiple(sem_t * sem,int count); int sem_post_multiple(sem_t * sem, unsigned int count);
int sem_getvalue(sem_t * sem, int * sval); int sem_getvalue(sem_t * sem, unsigned int * sval);
#endif /* __WIN__ */ #endif
C_MODE_END C_MODE_END
#endif /* !_my_semaphore_h_ */ #endif /* !_my_semaphore_h_ */
...@@ -46,7 +46,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\ ...@@ -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_quick.c my_lockmem.c my_static.c \
my_getopt.c my_mkdir.c \ my_getopt.c my_mkdir.c \
default.c my_compress.c checksum.c raid.cc \ 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_vsnprintf.c charset.c my_bitmap.c my_bit.c md5.c \
my_gethostbyname.c rijndael.c my_aes.c sha1.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 \ EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
......
...@@ -75,7 +75,7 @@ int handle_options(int *argc, char ***argv, ...@@ -75,7 +75,7 @@ int handle_options(int *argc, char ***argv,
uint opt_found, argvpos= 0, length, i; uint opt_found, argvpos= 0, length, i;
my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used, my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
option_is_loose; option_is_loose;
char *progname= *(*argv), **pos, *optend, *prev_found; char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found;
const struct my_option *optp; const struct my_option *optp;
int error; int error;
...@@ -84,7 +84,7 @@ int handle_options(int *argc, char ***argv, ...@@ -84,7 +84,7 @@ int handle_options(int *argc, char ***argv,
(*argv)++; /* --- || ---- */ (*argv)++; /* --- || ---- */
init_variables(longopts); init_variables(longopts);
for (pos= *argv; *pos; pos++) for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
{ {
char *cur_arg= *pos; char *cur_arg= *pos;
if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */ if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */
......
/* 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) */
...@@ -375,7 +375,7 @@ sem_post (sem_t * sem) ...@@ -375,7 +375,7 @@ sem_post (sem_t * sem)
*/ */
int int
sem_post_multiple (sem_t * sem, int count ) sem_post_multiple (sem_t * sem, unsigned int count)
{ {
#ifdef EXTRA_DEBUG #ifdef EXTRA_DEBUG
if (sem == NULL || *sem == NULL || count <= 0) if (sem == NULL || *sem == NULL || count <= 0)
...@@ -397,7 +397,7 @@ sem_post_multiple (sem_t * sem, int count ) ...@@ -397,7 +397,7 @@ sem_post_multiple (sem_t * sem, int count )
} }
int int
sem_getvalue (sem_t *sem, int *sval) sem_getvalue (sem_t *sem, unsigned int *sval)
{ {
errno = ENOSYS; errno = ENOSYS;
return -1; return -1;
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
* Mountain View, California 94043 * Mountain View, California 94043
*/ */
int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused))) int my_rwlock_init(rw_lock_t *rwp, void *arg __attribute__((unused)))
{ {
pthread_condattr_t cond_attr; pthread_condattr_t cond_attr;
...@@ -74,7 +74,9 @@ int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused))) ...@@ -74,7 +74,9 @@ int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused)))
return( 0 ); return( 0 );
} }
int my_rwlock_destroy( rw_lock_t *rwp ) {
int my_rwlock_destroy(rw_lock_t *rwp)
{
pthread_mutex_destroy( &rwp->lock ); pthread_mutex_destroy( &rwp->lock );
pthread_cond_destroy( &rwp->readers ); pthread_cond_destroy( &rwp->readers );
pthread_cond_destroy( &rwp->writers ); pthread_cond_destroy( &rwp->writers );
...@@ -82,11 +84,13 @@ int my_rwlock_destroy( rw_lock_t *rwp ) { ...@@ -82,11 +84,13 @@ int my_rwlock_destroy( rw_lock_t *rwp ) {
return( 0 ); return( 0 );
} }
int my_rw_rdlock( rw_lock_t *rwp ) {
int my_rw_rdlock(rw_lock_t *rwp)
{
pthread_mutex_lock(&rwp->lock); pthread_mutex_lock(&rwp->lock);
/* active or queued writers */ /* active or queued writers */
while ( ( rwp->state < 0 ) || rwp->waiters ) while (( rwp->state < 0 ) || rwp->waiters)
pthread_cond_wait( &rwp->readers, &rwp->lock); pthread_cond_wait( &rwp->readers, &rwp->lock);
rwp->state++; rwp->state++;
...@@ -95,8 +99,8 @@ int my_rw_rdlock( rw_lock_t *rwp ) { ...@@ -95,8 +99,8 @@ int my_rw_rdlock( rw_lock_t *rwp ) {
return( 0 ); return( 0 );
} }
int my_rw_wrlock( rw_lock_t *rwp ) { int my_rw_wrlock(rw_lock_t *rwp)
{
pthread_mutex_lock(&rwp->lock); pthread_mutex_lock(&rwp->lock);
rwp->waiters++; /* another writer queued */ rwp->waiters++; /* another writer queued */
...@@ -106,10 +110,12 @@ int my_rw_wrlock( rw_lock_t *rwp ) { ...@@ -106,10 +110,12 @@ int my_rw_wrlock( rw_lock_t *rwp ) {
--rwp->waiters; --rwp->waiters;
pthread_mutex_unlock( &rwp->lock ); pthread_mutex_unlock( &rwp->lock );
return( 0 ); return(0);
} }
int my_rw_unlock( rw_lock_t *rwp ) {
int my_rw_unlock(rw_lock_t *rwp)
{
DBUG_PRINT("rw_unlock", DBUG_PRINT("rw_unlock",
("state: %d waiters: %d", rwp->state, rwp->waiters)); ("state: %d waiters: %d", rwp->state, rwp->waiters));
pthread_mutex_lock(&rwp->lock); pthread_mutex_lock(&rwp->lock);
...@@ -121,7 +127,9 @@ int my_rw_unlock( rw_lock_t *rwp ) { ...@@ -121,7 +127,9 @@ int my_rw_unlock( rw_lock_t *rwp ) {
pthread_cond_signal( &rwp->writers ); pthread_cond_signal( &rwp->writers );
else else
pthread_cond_broadcast( &rwp->readers ); pthread_cond_broadcast( &rwp->readers );
} else { }
else
{
if ( --rwp->state == 0 ) /* no more readers */ if ( --rwp->state == 0 ) /* no more readers */
pthread_cond_signal( &rwp->writers ); pthread_cond_signal( &rwp->writers );
} }
......
...@@ -373,7 +373,7 @@ static my_bool ...@@ -373,7 +373,7 @@ static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)), get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
char *argument __attribute__((unused))) char *argument __attribute__((unused)))
{ {
switch(optid) { switch (optid) {
case 'v': case 'v':
opt_verbose++; opt_verbose++;
break; break;
...@@ -398,8 +398,9 @@ static int get_options(int argc, char **argv) ...@@ -398,8 +398,9 @@ static int get_options(int argc, char **argv)
if (argc >= 1) if (argc >= 1)
{ {
fprintf(stderr,"%s: Too many arguments\n", my_progname);
usage(0); usage(0);
exit(1); exit(1);
} }
return(0); return(0);
} }
......
...@@ -881,7 +881,7 @@ static int create_table_from_dump(THD* thd, NET* net, const char* db, ...@@ -881,7 +881,7 @@ static int create_table_from_dump(THD* thd, NET* net, const char* db,
/* we do not want to log create table statement */ /* we do not want to log create table statement */
save_options = thd->options; save_options = thd->options;
thd->options &= ~OPTION_BIN_LOG; thd->options &= ~(ulong) OPTION_BIN_LOG;
thd->proc_info = "Creating table from master dump"; thd->proc_info = "Creating table from master dump";
// save old db in case we are creating in a different database // save old db in case we are creating in a different database
char* save_db = thd->db; char* save_db = thd->db;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment