Skip to content

Commit 0a7b074

Browse files
committed
Pre-Vista leveldb::port::InitOnce implementation
1 parent 31a2b09 commit 0a7b074

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

port/port_win.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,8 @@ AtomicPointer::AtomicPointer(void* v) {
123123
Release_Store(v);
124124
}
125125

126-
BOOL CALLBACK InitHandleFunction (PINIT_ONCE InitOnce, PVOID func, PVOID *lpContext) {
127-
((void (*)())func)();
128-
return true;
129-
}
130-
131126
void InitOnce(OnceType* once, void (*initializer)()) {
132-
InitOnceExecuteOnce((PINIT_ONCE)once, InitHandleFunction, initializer, NULL);
127+
once->InitOnce(initializer);
133128
}
134129

135130
void* AtomicPointer::Acquire_Load() const {

port/port_win.h

+20-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,26 @@ class CondVar {
9393

9494
};
9595

96-
typedef void* OnceType;
97-
#define LEVELDB_ONCE_INIT 0
96+
class OnceType {
97+
public:
98+
// OnceType() : init_(false) {}
99+
OnceType(const OnceType &once) : init_(once.init_) {}
100+
OnceType(bool f) : init_(f) {}
101+
void InitOnce(void (*initializer)()) {
102+
mutex_.Lock();
103+
if (!init_) {
104+
init_ = true;
105+
initializer();
106+
}
107+
mutex_.Unlock();
108+
}
109+
110+
private:
111+
bool init_;
112+
Mutex mutex_;
113+
};
114+
115+
#define LEVELDB_ONCE_INIT false
98116
extern void InitOnce(port::OnceType*, void (*initializer)());
99117

100118
// Storage for a lock-free pointer

0 commit comments

Comments
 (0)