Commit 0573b66d authored by monty@hundin.mysql.fi's avatar monty@hundin.mysql.fi
Browse files

Added support for rw_tryrdlock() and rw_trywrlock()

parent 66f86faa
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -485,6 +485,8 @@ int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
#define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
#define rw_rdlock(A) pthread_mutex_lock((A))
#define rw_wrlock(A) pthread_mutex_lock((A))
#define rw_tryrdlock(A) pthread_mutex_trylock((A))
#define rw_trywrlock(A) pthread_mutex_trylock((A))
#define rw_unlock(A) pthread_mutex_unlock((A))
#define rwlock_destroy(A) pthread_mutex_destroy((A))
#elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
@@ -492,6 +494,8 @@ int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
#define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
#define rw_rdlock(A) pthread_rwlock_rdlock(A)
#define rw_wrlock(A) pthread_rwlock_wrlock(A)
#define rw_tryrdlock(A) pthread_mutex_tryrdlock((A))
#define rw_trywrlock(A) pthread_mutex_trywrlock((A))
#define rw_unlock(A) pthread_rwlock_unlock(A)
#define rwlock_destroy(A) pthread_rwlock_destroy(A)
#elif defined(HAVE_RWLOCK_INIT)
@@ -512,6 +516,8 @@ typedef struct _my_rw_lock_t {
#define rw_lock_t my_rw_lock_t
#define rw_rdlock(A) my_rw_rdlock((A))
#define rw_wrlock(A) my_rw_wrlock((A))
#define rw_tryrdlock(A) my_rw_tryrdlock((A))
#define rw_trywrlock(A) my_rw_trywrlock((A))
#define rw_unlock(A) my_rw_unlock((A))
#define rwlock_destroy(A) my_rwlock_destroy((A))

+0 −7
Original line number Diff line number Diff line
@@ -750,13 +750,6 @@ byte *my_compress_alloc(const byte *packet, ulong *len, ulong *complen);
ulong checksum(const byte *mem, uint count);
uint my_bit_log2(ulong value);

#if defined(SAFE_MUTEX) && !defined(DBUG_OFF)
#define DBUG_ASSERT_LOCK(lock) DBUG_ASSERT((lock)->count == 1 && \
				   (lock)->thread == pthread_self())
#else
#define DBUG_ASSERT_LOCK(lock)
#endif

#if defined(_MSC_VER) && !defined(__WIN__)
extern void sleep(int sec);
#endif
+51 −20
Original line number Diff line number Diff line
@@ -19,10 +19,12 @@
#include "mysys_priv.h"
#include <my_pthread.h>
#if defined(THREAD) && !defined(HAVE_PTHREAD_RWLOCK_RDLOCK) && !defined(HAVE_RWLOCK_INIT)
#include <errno.h>

/*
 * Source base from Sun Microsystems SPILT, simplified
 * for MySQL use -- Joshua Chamas
  Source base from Sun Microsystems SPILT, simplified for MySQL use
  -- Joshua Chamas
  Some cleanup and additional code by Monty
*/

/*
@@ -80,7 +82,6 @@ int my_rwlock_destroy(rw_lock_t *rwp)
  pthread_mutex_destroy( &rwp->lock );
  pthread_cond_destroy( &rwp->readers );
  pthread_cond_destroy( &rwp->writers );

  return(0);
}

@@ -95,10 +96,25 @@ int my_rw_rdlock(rw_lock_t *rwp)

  rwp->state++;
  pthread_mutex_unlock(&rwp->lock);

  return(0);
}

int my_rw_tryrdlock(rw_lock_t *rwp)
{
  int res;
  pthread_mutex_lock(&rwp->lock);
  if ((rwp->state < 0 ) || rwp->waiters)
    res= EBUSY;					/* Can't get lock */
  else
  {
    res=0;
    rwp->state++;
  }
  pthread_mutex_unlock(&rwp->lock);
  return(res);
}


int my_rw_wrlock(rw_lock_t *rwp)
{
  pthread_mutex_lock(&rwp->lock);
@@ -107,20 +123,36 @@ int my_rw_wrlock(rw_lock_t *rwp)
  while (rwp->state)
    pthread_cond_wait(&rwp->writers, &rwp->lock);
  rwp->state	= -1;
  --rwp->waiters;
  rwp->waiters--;
  pthread_mutex_unlock(&rwp->lock);

  return(0);
}


int my_rw_trywrlock(rw_lock_t *rwp)
{
  int res;
  pthread_mutex_lock(&rwp->lock);
  if (rwp->state)
    res= EBUSY;					/* Can't get lock */    
  else
  {
    res=0;
    rwp->state	= -1;
  }
  pthread_mutex_unlock(&rwp->lock);
  return(res);
}


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);

  if ( rwp->state == -1 ) {	/* writer releasing	*/
  if (rwp->state == -1)		/* writer releasing */
  {
    rwp->state= 0;		/* mark as available */

    if ( rwp->waiters )		/* writers queued */
@@ -135,7 +167,6 @@ int my_rw_unlock(rw_lock_t *rwp)
  }

  pthread_mutex_unlock( &rwp->lock );

  return(0);
}

+4 −4
Original line number Diff line number Diff line
@@ -888,15 +888,15 @@ bool MYSQL_LOG::write(THD *thd,enum enum_server_command command,

bool MYSQL_LOG::write(Log_event* event_info)
{
  /* In most cases this is only called if 'is_open()' is true */
  bool error=0;
  bool should_rotate = 0;
  
  if (!inited)					// Can't use mutex if not init
    return 0;
  VOID(pthread_mutex_lock(&LOCK_log));
  /* In most cases this is only called if 'is_open()' is true */
  if (is_open())
  {
    bool should_rotate = 0;
    THD *thd=event_info->thd;
    const char* db = event_info->get_db();
#ifdef USING_TRANSACTIONS    
@@ -985,9 +985,9 @@ bool MYSQL_LOG::write(Log_event* event_info)
    }
    if (file == &log_file)
      signal_update();
  }
    if (should_rotate)
      new_file(1); // inside mutex
  }
  VOID(pthread_mutex_unlock(&LOCK_log));
  return error;
}
+7 −4
Original line number Diff line number Diff line
@@ -1022,16 +1022,19 @@ char* sql_ex_info::init(char* buf,char* buf_end,bool use_new_format)
#ifndef MYSQL_CLIENT
Load_log_event::Load_log_event(THD* thd, sql_exchange* ex,
			       const char* db_arg, const char* table_name_arg,
		 List<Item>& fields_arg, enum enum_duplicates handle_dup)
			       List<Item>& fields_arg,
			       enum enum_duplicates handle_dup)
  :Log_event(thd),thread_id(thd->thread_id), num_fields(0),fields(0),
  field_lens(0),field_block_len(0), table_name(table_name_arg),
  field_lens(0),field_block_len(0),
  table_name(table_name_arg ? table_name_arg : ""),
  db(db_arg), fname(ex->file_name)
{
  time_t end_time;
  time(&end_time);
  exec_time = (ulong) (end_time  - thd->start_time);
  db_len = (db) ? (uint32) strlen(db) : 0;
  table_name_len = (table_name) ? (uint32) strlen(table_name) : 0;
  /* db can never be a zero pointer in 4.0 */
  db_len = (uint32) strlen(db);
  table_name_len = (uint32) strlen(table_name);
  fname_len = (fname) ? (uint) strlen(fname) : 0;
  sql_ex.field_term = (char*) ex->field_term->ptr();
  sql_ex.field_term_len = (uint8) ex->field_term->length();
Loading