Skip to content

Commit 40c0d3d

Browse files
authored
RWLock for callbacks (#227)
1 parent 81d0802 commit 40c0d3d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

binding.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,9 @@ int llama_predict(void* params_ptr, void* state_pr, char* result, bool debug) {
479479

480480
for (auto id : embd) {
481481
const std::string token_str = llama_token_to_piece(ctx, id);
482-
printf("%s", token_str.c_str());
482+
if (debug) {
483+
printf("%s", token_str.c_str());
484+
}
483485

484486
if (embd.size() > 1) {
485487
input_tokens.push_back(id);

llama.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ func (l *LLama) Predict(text string, opts ...PredictOption) (string, error) {
332332
return res, nil
333333
}
334334

335-
// tokenize has an interesting return property: negative lengths (potentially) have meaning. Therefore, return the length seperate from the slice and error - all three can be used together
335+
// tokenize has an interesting return property: negative lengths (potentially) have meaning.
336+
// Therefore, return the length seperate from the slice and error - all three can be used together
336337
func (l *LLama) TokenizeString(text string, opts ...PredictOption) (int32, []int32, error) {
337338
po := NewPredictOptions(opts...)
338339

@@ -396,14 +397,14 @@ func (l *LLama) SetTokenCallback(callback func(token string) bool) {
396397
}
397398

398399
var (
399-
m sync.Mutex
400+
m sync.RWMutex
400401
callbacks = map[uintptr]func(string) bool{}
401402
)
402403

403404
//export tokenCallback
404405
func tokenCallback(statePtr unsafe.Pointer, token *C.char) bool {
405-
m.Lock()
406-
defer m.Unlock()
406+
m.RLock()
407+
defer m.RUnlock()
407408

408409
if callback, ok := callbacks[uintptr(statePtr)]; ok {
409410
return callback(C.GoString(token))

0 commit comments

Comments
 (0)