[Groonga-commit] groonga/groonga at bcc24d2 [master] cache persistent: support opening from multi processes on Windows

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Wed Apr 12 14:20:08 JST 2017


Kouhei Sutou	2017-04-12 14:20:08 +0900 (Wed, 12 Apr 2017)

  New Revision: bcc24d24c77bf562847997f54cd4bfcc83c3866b
  https://github.com/groonga/groonga/commit/bcc24d24c77bf562847997f54cd4bfcc83c3866b

  Message:
    cache persistent: support opening from multi processes on Windows
    
    It will work...

  Modified files:
    lib/file_lock.c
    lib/grn_file_lock.h

  Modified: lib/file_lock.c (+44 -6)
===================================================================
--- lib/file_lock.c    2017-04-12 11:43:18 +0900 (3e4d486)
+++ lib/file_lock.c    2017-04-12 14:20:08 +0900 (696c3e9)
@@ -20,8 +20,22 @@
 #include "grn_ctx.h"
 
 #include <sys/stat.h>
-#include <sys/types.h>
-#include <fcntl.h>
+
+#ifdef WIN32
+# include <io.h>
+# include <share.h>
+#else /* WIN32 */
+# include <sys/types.h>
+# include <fcntl.h>
+#endif /* WIN32 */
+
+#ifdef WIN32
+# define GRN_FILE_LOCK_IS_INVALID(file_lock)       \
+  ((file_lock)->handle == INVALID_HANDLE_VALUE)
+#else /* WIN32 */
+# define GRN_FILE_LOCK_IS_INVALID(file_lock)    \
+  ((file_lock)->fd == -1)
+#endif /* WIN32 */
 
 void
 grn_file_lock_init(grn_ctx *ctx,
@@ -29,7 +43,11 @@ grn_file_lock_init(grn_ctx *ctx,
                    const char *path)
 {
   file_lock->path = path;
+#ifdef WIN32
+  file_lock->handle = INVALID_HANDLE_VALUE;
+#else /* WIN32 */
   file_lock->fd = -1;
+#endif /* WIN32 */
 }
 
 grn_bool
@@ -46,14 +64,24 @@ grn_file_lock_acquire(grn_ctx *ctx,
   }
 
   for (i = 0; i < n_lock_tries; i++) {
+#ifdef WIN32
+    file_lock->handle = CreateFile(file_lock->path,
+                                   GENERIC_READ | GENERIC_WRITE,
+                                   0,
+                                   NULL,
+                                   CREATE_ALWAYS,
+                                   FILE_ATTRIBUTE_NORMAL,
+                                   NULL);
+#else /* WIN32 */
     file_lock->fd = open(file_lock->path, O_CREAT | O_EXCL);
-    if (file_lock->fd != -1) {
+#endif
+    if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) {
       break;
     }
     grn_nanosleep(GRN_LOCK_WAIT_TIME_NANOSECOND);
   }
 
-  if (file_lock->fd == -1) {
+  if (GRN_FILE_LOCK_IS_INVALID(file_lock)) {
     ERR(GRN_NO_LOCKS_AVAILABLE,
         "%s failed to acquire lock: <%s>",
         error_message_tag, file_lock->path);
@@ -66,18 +94,28 @@ grn_file_lock_acquire(grn_ctx *ctx,
 void
 grn_file_lock_release(grn_ctx *ctx, grn_file_lock *file_lock)
 {
-  if (file_lock->fd == -1) {
+  if (GRN_FILE_LOCK_IS_INVALID(file_lock)) {
     return;
   }
 
+#ifdef WIN32
+  CloseHandle(file_lock->handle);
+  DeleteFile(file_lock->path);
+
+  file_lock->handle = INVALID_HANDLE_VALUE;
+#else /* WIN32 */
   close(file_lock->fd);
   unlink(file_lock->path);
+
+  file_lock->fd = -1;
+#endif /* WIN32 */
+  file_lock->path = NULL;
 }
 
 void
 grn_file_lock_fin(grn_ctx *ctx, grn_file_lock *file_lock)
 {
-  if (file_lock->fd != -1) {
+  if (!GRN_FILE_LOCK_IS_INVALID(file_lock)) {
     grn_file_lock_release(ctx, file_lock);
   }
 }

  Modified: lib/grn_file_lock.h (+4 -0)
===================================================================
--- lib/grn_file_lock.h    2017-04-12 11:43:18 +0900 (c99876f)
+++ lib/grn_file_lock.h    2017-04-12 14:20:08 +0900 (43ab576)
@@ -26,7 +26,11 @@ extern "C" {
 
 typedef struct {
   const char *path;
+#ifdef WIN32
+  HANDLE handle;
+#else /* WIN32 */
   int fd;
+#endif /* WIN32 */
 } grn_file_lock;
 
 void grn_file_lock_init(grn_ctx *ctx,
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る