win32: use a mutex to gurantee single instance app.

We found a corner case that the pc power is shut-down.
in that case lock file won't be removed and Enventor won't be
launched forever.

So use a mutex to recover this scenario additionally.
I tested all the scenarios that I can imagine,
and now it perfectly works fine!
This commit is contained in:
Hermet Park 2016-08-26 00:18:39 +09:00
parent a0b9d4e911
commit caa2a39d65
1 changed files with 23 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
static HANDLE hMutex = INVALID_HANDLE_VALUE;
#endif #endif
typedef struct app_s typedef struct app_s
@ -949,14 +950,27 @@ enventor_lock_create(void)
//Is there any other Enventor instance? //Is there any other Enventor instance?
if (ecore_file_exists(buf)) if (ecore_file_exists(buf))
{ {
#ifdef _WIN32
hMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, ENVENTOR_NAME);
if (hMutex != INVALID_HANDLE_VALUE)
{
fprintf(stdout, "Enventor program is already running!\n\n"
"If you are really stuck in launching enventor due to "
"this,\nTry removing a file \"%s\"\n", buf);
return EINA_FALSE;
}
#else
fprintf(stdout, "Enventor program is already running!\n\n" fprintf(stdout, "Enventor program is already running!\n\n"
"If you are really stuck in launching enventor due to " "If you are really stuck in launching enventor due to "
"this,\nTry removing a file \"%s\"\n", buf); "this,\nTry removing a file \"%s\"\n", buf);
return EINA_FALSE; return EINA_FALSE;
#endif
} }
//Ok, this is an unique instance! //Ok, this is an unique instance!
#ifdef _WIN32 #ifdef _WIN32
hMutex = CreateMutex(NULL, FALSE, ENVENTOR_NAME);
HANDLE handle = CreateFile(buf, GENERIC_READ, NULL, NULL, CREATE_NEW, HANDLE handle = CreateFile(buf, GENERIC_READ, NULL, NULL, CREATE_NEW,
FILE_FLAG_DELETE_ON_CLOSE, 0); FILE_FLAG_DELETE_ON_CLOSE, 0);
if (INVALID_HANDLE_VALUE == handle) if (INVALID_HANDLE_VALUE == handle)
@ -983,10 +997,16 @@ enventor_lock_create(void)
static void static void
enventor_lock_remove() enventor_lock_remove()
{ {
#ifndef _WIN32
//You are not the owner of the lock. //You are not the owner of the lock.
if (!own_lock) return; if (!own_lock) return;
#ifdef _WIN32
if (INVALID_HANDLE_VALUE != hMutex)
{
Closehandle(hMutex);
hMutex = INVALID_HANDLE_VALUE;
}
#else
//Tempoary Folder //Tempoary Folder
const char *tmpdir = eina_environment_tmp_get(); const char *tmpdir = eina_environment_tmp_get();
if (!tmpdir) if (!tmpdir)
@ -1012,8 +1032,6 @@ enventor_lock_remove()
} }
ecore_file_remove(buf); ecore_file_remove(buf);
#else
(void) 0;
#endif #endif
} }