<commit_before>#lang racket/base
(require racket/class
         "interfaces.rkt"
         "common.rkt")
(provide kill-safe-connection
         virtual-connection
         connection-pool
         connection-pool?
         connection-pool-lease)

;; manager% implements kill-safe manager thread w/ request channel
(define manager%
  (class object%
    ;; other-evt : (-> evt)
    ;; generates other evt to sync on besides req-channel, eg timeouts
    (init-field (other-evt (lambda () never-evt))
                (alt-enabled? (lambda () #t)))
    (super-new)

    (define req-channel (make-channel))
    (define alt-req-channel (make-channel))

    (define mthread
      (thread/suspend-to-kill
       (lambda ()
         (let loop ()
           (sync (wrap-evt req-channel (lambda (p) (p)))
                 (if (alt-enabled?)
                     (wrap-evt alt-req-channel (lambda (p) (p)))
                     never-evt)
                 (other-evt))
           (loop)))))

    (define/public (call proc)
      (call* proc req-channel #f))
    (define/public (alt-call-evt proc)
      (call* proc alt-req-channel #t))

    (define/private (call* proc chan as-evt?)
      (thread-resume mthread (current-thread))
      (let* ([result #f]
             [sema (make-semaphore 0)]
             [proc (lambda ()
                     (set! result
                           (with-handlers ([(lambda (e) #t)
                                            (lambda (e) (cons 'exn e))])
                             (cons 'values (call-with-values proc list))))
                     (semaphore-post sema))]
             [handler
              (lambda (_evt)
                (semaphore-wait sema)
                (case (car result)
                  ((values) (apply values (cdr result)))
                  ((exn) (raise (cdr result)))))])
        (if as-evt?
            (wrap-evt (channel-put-evt chan proc) handler)
            (begin (channel-put chan proc)
                   (handler #f)))))))

;; ----

;; Kill-safe wrapper

;; Note: wrapper protects against kill-thread, but not from
;; custodian-shutdown of ports, etc.

(define kill-safe-connection%
  (class* object% (connection<%>)
    (init-private connection)

    (define mgr (new manager%))<commit_msg>db: use channel instead of semaphore + result variable<commit_after>#lang racket/base
(require racket/class
         "interfaces.rkt"
         "common.rkt")
(provide kill-safe-connection
         virtual-connection
         connection-pool
         connection-pool?
         connection-pool-lease)

;; manager% implements kill-safe manager thread w/ request channel
(define manager%
  (class object%
    ;; other-evt : (-> evt)
    ;; generates other evt to sync on besides req-channel, eg timeouts
    (init-field (other-evt (lambda () never-evt))
                (alt-enabled? (lambda () #t)))
    (super-new)

    (define req-channel (make-channel))
    (define alt-req-channel (make-channel))

    (define mthread
      (thread/suspend-to-kill
       (lambda ()
         (let loop ()
           (sync (wrap-evt req-channel (lambda (p) (p)))
                 (if (alt-enabled?)
                     (wrap-evt alt-req-channel (lambda (p) (p)))
                     never-evt)
                 (other-evt))
           (loop)))))

    (define/public (call proc)
      (call* proc req-channel #f))
    (define/public (alt-call-evt proc)
      (call* proc alt-req-channel #t))

    (define/private (call* proc chan as-evt?)
      (thread-resume mthread (current-thread))
      (let* ([return-channel (make-channel)]
             [proc (lambda ()
                     (channel-put return-channel
                                  (with-handlers ([(lambda (e) #t)
                                                   (lambda (e) (cons 'exn e))])
                                    (cons 'values (call-with-values proc list)))))]
             [handler
              (lambda (_evt)
                (let ([result (channel-get return-channel)])
                  (case (car result)
                    ((values) (apply values (cdr result)))
                    ((exn) (raise (cdr result))))))])
        (if as-evt?
            (wrap-evt (channel-put-evt chan proc) handler)
            (begin (channel-put chan proc)
                   (handler #f)))))))

;; ----

;; Kill-safe wrapper

;; Note: wrapper protects against kill-thread, but not from
;; custodian-shutdown of ports, etc.

(define kill-safe-connection%
  (class* object% (connection<%>)
    (init-private connection)

    (define mgr (new manager%))
{'fraction_non_alphanumeric': 0.13621408203553412, 'fraction_numerical': 0.00021934634788330776, 'mean_word_length': 1.6823529411764706, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640581', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1297, 'n_words': 353}