Susumu Yata
null+****@clear*****
Thu Feb 18 12:15:52 JST 2016
Susumu Yata 2016-02-18 12:15:52 +0900 (Thu, 18 Feb 2016) New Revision: e550b2594c34989e51a8577a66d343c40f0bf577 https://github.com/groonga/grnci/commit/e550b2594c34989e51a8577a66d343c40f0bf577 Message: Move variables and functions for Groonga init/fin to grn.go GitHub: #24 Added files: grn.go Modified files: grnci.go Added: grn.go (+64 -0) 100644 =================================================================== --- /dev/null +++ grn.go 2016-02-18 12:15:52 +0900 (0c595d3) @@ -0,0 +1,64 @@ +package grnci + +// #cgo pkg-config: groonga +// #include <groonga.h> +// #include <stdlib.h> +// #include "grnci.h" +import "C" + +import ( + "fmt" + "math" + "sync" +) + +// This source file provides variables and functions to initialize and finalize +// Groonga. +// +// C.grn_init initializes Groonga and C.grn_fin finalizes Groonga. +// Note that C.grn_init() must not be called if Groonga is already initialized. +// +// Grnci automatically initializes Groonga when it creates a new DB instance. +// To achieve this, Grnci uses a reference count. + +// grnCnt is a reference count for Groonga. +// grnInit increments grnCnt and grnFin decrements grnCnt. +var grnCnt uint32 + +// grnCntMutex is a mutex for grnCnt. +var grnCntMutex sync.Mutex + +// grnInit increments grnCnt and initializes Groonga if grnCnt changes from 0 +// to 1. +func grnInit() error { + grnCntMutex.Lock() + defer grnCntMutex.Unlock() + if grnCnt == math.MaxUint32 { + return fmt.Errorf("grnCnt overflow") + } + if grnCnt == 0 { + if rc := C.grn_init(); rc != C.GRN_SUCCESS { + return fmt.Errorf("grn_init() failed: rc = %s", rc) + } + C.grnci_init_thread_limit() + } + grnCnt++ + return nil +} + +// grnFin decrements grnCnt and initializes Groonga if grnCnt changes from 1 +// to 0. +func grnFin() error { + grnCntMutex.Lock() + defer grnCntMutex.Unlock() + if grnCnt == 0 { + return fmt.Errorf("grnCnt underflow") + } + grnCnt-- + if grnCnt == 0 { + if rc := C.grn_fin(); rc != C.GRN_SUCCESS { + return fmt.Errorf("grn_fin() failed: rc = %s", rc) + } + } + return nil +} Modified: grnci.go (+3 -50) =================================================================== --- grnci.go 2016-02-16 11:36:36 +0900 (9d7e423) +++ grnci.go 2016-02-18 12:15:52 +0900 (b192dfc) @@ -16,7 +16,6 @@ import ( "bytes" "encoding/json" "fmt" - "math" "reflect" "strconv" "strings" @@ -367,52 +366,6 @@ func parseColumnNames(s string) ([]string, error) { } // -// Library management -// - -// grnCnt is a reference count of the Groonga library. -// Init() increments grnCnt and Fin() decrements grnCnt. -var grnCnt uint32 - -// grnCntMutex is a mutex for grnCnt. -var grnCntMutex sync.Mutex - -// refLib() increments grnCnt. -// The Groonga library is initialized if grnCnt changes from 0 to 1. -func refLib() error { - grnCntMutex.Lock() - defer grnCntMutex.Unlock() - if grnCnt == math.MaxUint32 { - return fmt.Errorf("grnCnt overflow") - } - if grnCnt == 0 { - if rc := C.grn_init(); rc != C.GRN_SUCCESS { - return fmt.Errorf("grn_init() failed: rc = %s", rc) - } - C.grnci_init_thread_limit() - } - grnCnt++ - return nil -} - -// unrefLib() decrements grnCnt. -// The Groonga library is finalized if grnCnt changes from 1 to 0. -func unrefLib() error { - grnCntMutex.Lock() - defer grnCntMutex.Unlock() - if grnCnt == 0 { - return fmt.Errorf("grnCnt underflow") - } - grnCnt-- - if grnCnt == 0 { - if rc := C.grn_fin(); rc != C.GRN_SUCCESS { - return fmt.Errorf("grn_fin() failed: rc = %s", rc) - } - } - return nil -} - -// // DB handle // @@ -440,13 +393,13 @@ type DB struct { // newDB() creates an instance of DB. // The instance must be finalized by DB.fin(). func newDB() (*DB, error) { - if err := refLib(); err != nil { + if err := grnInit(); err != nil { return nil, err } var db DB db.ctx = C.grn_ctx_open(C.int(0)) if db.ctx == nil { - unrefLib() + grnFin() return nil, fmt.Errorf("grn_ctx_open() failed") } return &db, nil @@ -478,7 +431,7 @@ func (db *DB) fin() error { } rc := C.grn_ctx_close(db.ctx) db.ctx = nil - unrefLib() + grnFin() if rc != C.GRN_SUCCESS { return fmt.Errorf("grn_ctx_close() failed: rc = %s", rc) } -------------- next part -------------- HTML����������������������������...Download