Discussion:
[gambit-list] thread-cells?
Dimitris Vyzovitis
2018-03-10 13:45:59 UTC
Permalink
Marc,

Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only gets
initialized if accessed.

They are incredibly useful for per thread caches, and probably a lot of
other things.

For instance, I have this common pattern that i see a lot:

(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))

(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))

and I would like to eliminate both the lock and the weak table.

I can add it and make a pr, but I wanted to consult with you first.

-- vyzo
Dimitris Vyzovitis
2018-03-10 14:16:10 UTC
Permalink
Also, we don't have to follow the plt interface -- we can just call them
thread-local-variables :)

-- vyzo
Post by Dimitris Vyzovitis
Marc,
Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only
gets initialized if accessed.
They are incredibly useful for per thread caches, and probably a lot of
other things.
(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))
(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))
and I would like to eliminate both the lock and the weak table.
I can add it and make a pr, but I wanted to consult with you first.
-- vyzo
Dimitris Vyzovitis
2018-03-10 14:21:26 UTC
Permalink
A possible interface for thread local variables:

(thread-local-set! key val)
(thread-local-ref key #!optional (default (macro-absent-obj))) ; when
unbound returns default or raises if absent

and thread-cells can be implemented on top with a struct and a gensym.

-- vyzo
Post by Dimitris Vyzovitis
Also, we don't have to follow the plt interface -- we can just call them
thread-local-variables :)
-- vyzo
Post by Dimitris Vyzovitis
Marc,
Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only
gets initialized if accessed.
They are incredibly useful for per thread caches, and probably a lot of
other things.
(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))
(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))
and I would like to eliminate both the lock and the weak table.
I can add it and make a pr, but I wanted to consult with you first.
-- vyzo
Marc Feeley
2018-03-12 16:34:48 UTC
Permalink
Extensions to Gambit threads can be done currently using the define-type-of-thread special form. For example:

(define-type-of-thread mythread
constructor: construct-mythread
errptr)

(define (make-mythread thunk)
(thread-init!
(construct-mythread #f)
thunk))

(define t
(make-mythread
(lambda ()
(pp (mythread-errptr (current-thread))))))

(mythread-errptr-set! t 'foo)

(thread-start! t)

(thread-join! t)

So I don’t see the need for adding this feature to the basic Gambit threads.

Marc
Post by Dimitris Vyzovitis
Marc,
Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only gets initialized if accessed.
They are incredibly useful for per thread caches, and probably a lot of other things.
(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))
(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))
and I would like to eliminate both the lock and the weak table.
I can add it and make a pr, but I wanted to consult with you first.
-- vyzo
Dimitris Vyzovitis
2018-03-12 18:44:42 UTC
Permalink
Ok, I hadn't thought of that.

-- vyzo
Post by Marc Feeley
Extensions to Gambit threads can be done currently using the
(define-type-of-thread mythread
constructor: construct-mythread
errptr)
(define (make-mythread thunk)
(thread-init!
(construct-mythread #f)
thunk))
(define t
(make-mythread
(lambda ()
(pp (mythread-errptr (current-thread))))))
(mythread-errptr-set! t 'foo)
(thread-start! t)
(thread-join! t)
So I don’t see the need for adding this feature to the basic Gambit
threads.
Marc
Post by Dimitris Vyzovitis
Marc,
Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only
gets initialized if accessed.
Post by Dimitris Vyzovitis
They are incredibly useful for per thread caches, and probably a lot of
other things.
Post by Dimitris Vyzovitis
(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))
(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))
and I would like to eliminate both the lock and the weak table.
I can add it and make a pr, but I wanted to consult with you first.
-- vyzo
Adam
2018-03-13 00:44:13 UTC
Permalink
Interesting. Great addition to thread-specific.
Post by Marc Feeley
Extensions to Gambit threads can be done currently using the
(define-type-of-thread mythread
constructor: construct-mythread
errptr)
(define (make-mythread thunk)
(thread-init!
(construct-mythread #f)
thunk))
(define t
(make-mythread
(lambda ()
(pp (mythread-errptr (current-thread))))))
(mythread-errptr-set! t 'foo)
(thread-start! t)
(thread-join! t)
So I don’t see the need for adding this feature to the basic Gambit
threads.
Marc
Post by Dimitris Vyzovitis
Marc,
Could we add thread-cells?
They can be easily implemented with a per-thread hash-table, that only
gets initialized if accessed.
Post by Dimitris Vyzovitis
They are incredibly useful for per thread caches, and probably a lot of
other things.
Post by Dimitris Vyzovitis
(def errptr-cache
(make-hash-table-eq weak-keys: #t))
(def errptr-cache-mutex
(make-mutex))
(def (get-errptr)
(with-lock errptr-cache-mutex
(lambda ()
(cond
((hash-get errptr-cache (current-thread))
=> (lambda (errptr)
(errptr_clear errptr)
errptr))
(else
(let (errptr (check-ptr get-errptr (make_errptr)))
(hash-put! errptr-cache (current-thread) errptr)
errptr))))))
and I would like to eliminate both the lock and the weak table.
I can add it and make a pr, but I wanted to consult with you first.
-- vyzo
_______________________________________________
Gambit-list mailing list
https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Loading...