summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorunknown <monty@hundin.mysql.fi>2002-06-29 00:16:15 +0300
committerunknown <monty@hundin.mysql.fi>2002-06-29 00:16:15 +0300
commit370016677af391c58c03a0eca198936b3ac5d382 (patch)
tree4ad1a20d59a1252f985bb184bc8e51fc1988c045 /mysys
parent7311cbe7dee5980d921ec43177fd268783eb18e2 (diff)
downloadmariadb-git-370016677af391c58c03a0eca198936b3ac5d382.tar.gz
Added support for semaphores in mysys.
(Needed for query cache for systems which doesn't have native semaphores) mysys/my_getopt.c: Safety fix. mysys/my_winsem.c: Shange all semaphore code to be uniform mysys/thr_rwlock.c: cleanup sql/gen_lex_hash.cc: Error message if wrong number of arguments. sql/slave.cc: R
Diffstat (limited to 'mysys')
-rw-r--r--mysys/Makefile.am2
-rw-r--r--mysys/my_getopt.c4
-rw-r--r--mysys/my_semaphore.c103
-rw-r--r--mysys/my_winsem.c4
-rw-r--r--mysys/thr_rwlock.c26
5 files changed, 125 insertions, 14 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am
index afaa82a8777..3ca07b7b80b 100644
--- a/mysys/Makefile.am
+++ b/mysys/Makefile.am
@@ -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 \
diff --git a/mysys/my_getopt.c b/mysys/my_getopt.c
index ce9006e03c0..9583b5a72c3 100644
--- a/mysys/my_getopt.c
+++ b/mysys/my_getopt.c
@@ -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 */
diff --git a/mysys/my_semaphore.c b/mysys/my_semaphore.c
new file mode 100644
index 00000000000..de4aac1cdbd
--- /dev/null
+++ b/mysys/my_semaphore.c
@@ -0,0 +1,103 @@
+/* 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) */
diff --git a/mysys/my_winsem.c b/mysys/my_winsem.c
index 268a05a0b21..e2713d189b2 100644
--- a/mysys/my_winsem.c
+++ b/mysys/my_winsem.c
@@ -375,7 +375,7 @@ sem_post (sem_t * sem)
*/
int
-sem_post_multiple (sem_t * sem, int count )
+sem_post_multiple (sem_t * sem, unsigned int count)
{
#ifdef EXTRA_DEBUG
if (sem == NULL || *sem == NULL || count <= 0)
@@ -397,7 +397,7 @@ sem_post_multiple (sem_t * sem, int count )
}
int
-sem_getvalue (sem_t *sem, int *sval)
+sem_getvalue (sem_t *sem, unsigned int *sval)
{
errno = ENOSYS;
return -1;
diff --git a/mysys/thr_rwlock.c b/mysys/thr_rwlock.c
index 4e2cf3554b7..5f561762a3a 100644
--- a/mysys/thr_rwlock.c
+++ b/mysys/thr_rwlock.c
@@ -58,7 +58,7 @@
* 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;
@@ -74,7 +74,9 @@ int my_rwlock_init( rw_lock_t *rwp, void *arg __attribute__((unused)))
return( 0 );
}
-int my_rwlock_destroy( rw_lock_t *rwp ) {
+
+int my_rwlock_destroy(rw_lock_t *rwp)
+{
pthread_mutex_destroy( &rwp->lock );
pthread_cond_destroy( &rwp->readers );
pthread_cond_destroy( &rwp->writers );
@@ -82,11 +84,13 @@ int my_rwlock_destroy( rw_lock_t *rwp ) {
return( 0 );
}
-int my_rw_rdlock( rw_lock_t *rwp ) {
+
+int my_rw_rdlock(rw_lock_t *rwp)
+{
pthread_mutex_lock(&rwp->lock);
/* active or queued writers */
- while ( ( rwp->state < 0 ) || rwp->waiters )
+ while (( rwp->state < 0 ) || rwp->waiters)
pthread_cond_wait( &rwp->readers, &rwp->lock);
rwp->state++;
@@ -95,8 +99,8 @@ int my_rw_rdlock( rw_lock_t *rwp ) {
return( 0 );
}
-int my_rw_wrlock( rw_lock_t *rwp ) {
-
+int my_rw_wrlock(rw_lock_t *rwp)
+{
pthread_mutex_lock(&rwp->lock);
rwp->waiters++; /* another writer queued */
@@ -106,10 +110,12 @@ int my_rw_wrlock( rw_lock_t *rwp ) {
--rwp->waiters;
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",
("state: %d waiters: %d", rwp->state, rwp->waiters));
pthread_mutex_lock(&rwp->lock);
@@ -121,7 +127,9 @@ int my_rw_unlock( rw_lock_t *rwp ) {
pthread_cond_signal( &rwp->writers );
else
pthread_cond_broadcast( &rwp->readers );
- } else {
+ }
+ else
+ {
if ( --rwp->state == 0 ) /* no more readers */
pthread_cond_signal( &rwp->writers );
}