List:Internals« Previous MessageNext Message »
From:sergeyv Date:September 20 2005 4:04pm
Subject:bk commit into 4.1 tree (SergeyV:1.2420)
View as plain text  
Below is the list of changes that have just been committed into a local
4.1 repository of sergeyv. When sergeyv does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.2420 05/09/20 20:03:51 SergeyV@selena. +1 -0
  Small update for lock functions used in my_cgets()

  mysys/my_conio.c
    1.2 05/09/20 20:03:44 SergeyV@selena. +91 -20
    Small update for lock functions used in my_cgets()

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	SergeyV
# Host:	selena.
# Root:	H:/MYSQL/src/#12929-mysql-4.1

--- 1.1/mysys/my_conio.c	2005-09-16 01:56:09 +04:00
+++ 1.2/mysys/my_conio.c	2005-09-20 20:03:44 +04:00
@@ -18,23 +18,89 @@
 #include "mysys_priv.h"
 
 #ifdef __WIN__
-static int my_coninpfh= 0;     /* console input */
+
+static HANDLE my_coninpfh= 0;     /* console input */
+
+/*
+  functions my_pthread_auto_mutex_lock & my_pthread_auto_mutex_free
+  are experimental at this moment, they are intended to bring
+  ability of protecting code sections without necessity to explicitly
+  initialize synchronization object in one of threads
+
+  if found useful they are to be exported in mysys
+*/
+
+/*
+  int my_pthread_auto_mutex_lock(HANDLE* ph, const char* name, 
+                                 int id, int time)
+
+  NOTES
+    creates a mutex with given name and tries to lock it time msec.
+    mutex name is appended with id to allow system wide or process wide
+    locks. Handle to created mutex returned in ph argument.
+
+  RETURN
+    0	              thread owns mutex
+    <>0	            error
+
+*/
+static
+int my_pthread_auto_mutex_lock(HANDLE* ph, const char* name, int id, int time)
+{
+  int res;
+  char tname[FN_REFLEN];
+  
+  sprintf(tname, "%s-%08X", name, id);
+  
+  *ph= CreateMutex(NULL, FALSE, tname);
+  if (*ph == NULL)
+    return GetLastError();
+
+  res= WaitForSingleObject(*ph, time);
+  
+  if (res == WAIT_TIMEOUT)
+    return ERROR_SEM_TIMEOUT;
+
+  if (res == WAIT_FAILED)
+    return GetLastError();
+
+  return 0;
+}
+
+/*
+  int my_pthread_auto_mutex_free(HANDLE* ph)
+
+
+  NOTES
+    releases a mutex.
+
+  RETURN
+    0	              thread released mutex
+    <>0	            error
+
+*/
+static
+int my_pthread_auto_mutex_free(HANDLE* ph)
+{
+  if (*ph)
+  {
+    ReleaseMutex(*ph);
+    CloseHandle(*ph);
+    *ph= NULL;
+  }
+
+  return 0;
+}
+
 
 #define pthread_auto_mutex_decl(name)                           \
-  HANDLE __h##name= NULL;                                       \
-  char   __p##name[sizeof(#name)+16];
+  HANDLE __h##name= NULL;
 
 #define pthread_auto_mutex_lock(name, proc, time)               \
-  sprintf(__p##name, "%s-%08X", #name, (proc));                 \
-  __h##name= CreateMutex(NULL, FALSE, __p##name);               \
-  WaitForSingleObject(__h##name, (time));
+  my_pthread_auto_mutex_lock(&__h##name, #name, (proc), (time))
 
 #define pthread_auto_mutex_free(name)                           \
-  if (__h##name)                                                \
-  {                                                             \
-    ReleaseMutex(__h##name);                                    \
-    CloseHandle(__h##name);                                     \
-  }
+  my_pthread_auto_mutex_free(&__h##name)
 
 
 /*
@@ -62,24 +128,29 @@
   char *result;
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   
-  pthread_auto_mutex_decl(my_conio_mutex);
+  pthread_auto_mutex_decl(my_conio_cs);
  
-  /* lock the console */
-  pthread_auto_mutex_lock(my_conio_mutex, GetCurrentProcessId(), INFINITE); 
+  /* lock the console for the current process*/
+  if (pthread_auto_mutex_lock(my_conio_cs, GetCurrentProcessId(), INFINITE))
+  {
+    /* can not lock console */
+    pthread_auto_mutex_free(my_conio_cs);  
+    return NULL;
+  }
 
   /* init console input */
   if (my_coninpfh == 0)
   {
     /* same handle will be used until process termination */
-    my_coninpfh= (int)CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
-                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
-                                 NULL, OPEN_EXISTING, 0, NULL);
+    my_coninpfh= CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
+                            FILE_SHARE_READ | FILE_SHARE_WRITE,
+                            NULL, OPEN_EXISTING, 0, NULL);
   }
 
-  if (my_coninpfh == -1) 
+  if (my_coninpfh == INVALID_HANDLE_VALUE) 
   {
     /* unlock the console */
-    pthread_auto_mutex_free(my_conio_mutex);  
+    pthread_auto_mutex_free(my_conio_cs);  
     return(NULL);
   }
 
@@ -138,7 +209,7 @@
 
   SetConsoleMode((HANDLE)my_coninpfh, state);
   /* unlock the console */
-  pthread_auto_mutex_free(my_conio_mutex);  
+  pthread_auto_mutex_free(my_conio_cs);  
 
   return result;
 }
Thread
bk commit into 4.1 tree (SergeyV:1.2420)sergeyv20 Sep