diff options
author | unknown <SergeyV@selena.> | 2005-09-20 20:03:51 +0400 |
---|---|---|
committer | unknown <SergeyV@selena.> | 2005-09-20 20:03:51 +0400 |
commit | ac604f715d7e8a2c290c9a0ca7ba092db1557505 (patch) | |
tree | 87c2638dc10db190d8c592a063b0ffcbcd673212 /mysys/my_conio.c | |
parent | 627bf43d9ff9c4b1b5cde1463cf5d03cf5e92c3d (diff) | |
download | mariadb-git-ac604f715d7e8a2c290c9a0ca7ba092db1557505.tar.gz |
Small update for lock functions used in my_cgets()
Diffstat (limited to 'mysys/my_conio.c')
-rw-r--r-- | mysys/my_conio.c | 111 |
1 files changed, 91 insertions, 20 deletions
diff --git a/mysys/my_conio.c b/mysys/my_conio.c index 2bf2c1cf1c0..e381f9f23ef 100644 --- a/mysys/my_conio.c +++ b/mysys/my_conio.c @@ -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* my_cgets(char *buffer, unsigned long clen, unsigned long* plen) 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 @@ char* my_cgets(char *buffer, unsigned long clen, unsigned long* plen) SetConsoleMode((HANDLE)my_coninpfh, state); /* unlock the console */ - pthread_auto_mutex_free(my_conio_mutex); + pthread_auto_mutex_free(my_conio_cs); return result; } |