Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mariadb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
mariadb
Commits
ac604f71
Commit
ac604f71
authored
Sep 20, 2005
by
unknown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small update for lock functions used in my_cgets()
parent
627bf43d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
20 deletions
+91
-20
mysys/my_conio.c
mysys/my_conio.c
+91
-20
No files found.
mysys/my_conio.c
View file @
ac604f71
...
@@ -18,23 +18,89 @@
...
@@ -18,23 +18,89 @@
#include "mysys_priv.h"
#include "mysys_priv.h"
#ifdef __WIN__
#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) \
#define pthread_auto_mutex_decl(name) \
HANDLE __h##name= NULL; \
HANDLE __h##name= NULL;
char __p##name[sizeof(#name)+16];
#define pthread_auto_mutex_lock(name, proc, time) \
#define pthread_auto_mutex_lock(name, proc, time) \
sprintf(__p##name, "%s-%08X", #name, (proc)); \
my_pthread_auto_mutex_lock(&__h##name, #name, (proc), (time))
__h##name= CreateMutex(NULL, FALSE, __p##name); \
WaitForSingleObject(__h##name, (time));
#define pthread_auto_mutex_free(name) \
#define pthread_auto_mutex_free(name) \
if (__h##name) \
my_pthread_auto_mutex_free(&__h##name)
{ \
ReleaseMutex(__h##name); \
CloseHandle(__h##name); \
}
/*
/*
...
@@ -62,24 +128,29 @@ char* my_cgets(char *buffer, unsigned long clen, unsigned long* plen)
...
@@ -62,24 +128,29 @@ char* my_cgets(char *buffer, unsigned long clen, unsigned long* plen)
char
*
result
;
char
*
result
;
CONSOLE_SCREEN_BUFFER_INFO
csbi
;
CONSOLE_SCREEN_BUFFER_INFO
csbi
;
pthread_auto_mutex_decl
(
my_conio_
mutex
);
pthread_auto_mutex_decl
(
my_conio_
cs
);
/* lock the console */
/* lock the console for the current process*/
pthread_auto_mutex_lock
(
my_conio_mutex
,
GetCurrentProcessId
(),
INFINITE
);
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 */
/* init console input */
if
(
my_coninpfh
==
0
)
if
(
my_coninpfh
==
0
)
{
{
/* same handle will be used until process termination */
/* same handle will be used until process termination */
my_coninpfh
=
(
int
)
CreateFile
(
"CONIN$"
,
GENERIC_READ
|
GENERIC_WRITE
,
my_coninpfh
=
CreateFile
(
"CONIN$"
,
GENERIC_READ
|
GENERIC_WRITE
,
FILE_SHARE_READ
|
FILE_SHARE_WRITE
,
FILE_SHARE_READ
|
FILE_SHARE_WRITE
,
NULL
,
OPEN_EXISTING
,
0
,
NULL
);
NULL
,
OPEN_EXISTING
,
0
,
NULL
);
}
}
if
(
my_coninpfh
==
-
1
)
if
(
my_coninpfh
==
INVALID_HANDLE_VALUE
)
{
{
/* unlock the console */
/* unlock the console */
pthread_auto_mutex_free
(
my_conio_
mutex
);
pthread_auto_mutex_free
(
my_conio_
cs
);
return
(
NULL
);
return
(
NULL
);
}
}
...
@@ -138,7 +209,7 @@ char* my_cgets(char *buffer, unsigned long clen, unsigned long* plen)
...
@@ -138,7 +209,7 @@ char* my_cgets(char *buffer, unsigned long clen, unsigned long* plen)
SetConsoleMode
((
HANDLE
)
my_coninpfh
,
state
);
SetConsoleMode
((
HANDLE
)
my_coninpfh
,
state
);
/* unlock the console */
/* unlock the console */
pthread_auto_mutex_free
(
my_conio_
mutex
);
pthread_auto_mutex_free
(
my_conio_
cs
);
return
result
;
return
result
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment