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
Afficher les réponses par date
Also, we don't have to follow the plt interface -- we can just call them thread-local-variables :) -- vyzo On Sat, Mar 10, 2018 at 3:45 PM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
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
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 On Sat, Mar 10, 2018 at 4:16 PM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
Also, we don't have to follow the plt interface -- we can just call them thread-local-variables :)
-- vyzo
On Sat, Mar 10, 2018 at 3:45 PM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
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
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
On Mar 10, 2018, at 8:45 AM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
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
Ok, I hadn't thought of that. -- vyzo On Mon, Mar 12, 2018 at 6:34 PM, Marc Feeley <feeley@iro.umontreal.ca> wrote:
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
On Mar 10, 2018, at 8:45 AM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
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
Interesting. Great addition to thread-specific. 2018-03-13 0:34 GMT+08:00 Marc Feeley <feeley@iro.umontreal.ca>:
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
On Mar 10, 2018, at 8:45 AM, Dimitris Vyzovitis <vyzo@hackzen.org> wrote:
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
_______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
participants (3)
-
Adam -
Dimitris Vyzovitis -
Marc Feeley