Skip to content

Instantly share code, notes, and snippets.

View deadtrickster's full-sized avatar
🤷‍♂️
🍄

Iliia Khaprov deadtrickster

🤷‍♂️
🍄
View GitHub Profile
@deadtrickster
deadtrickster / rwlock.lisp
Last active January 29, 2024 12:58
Read-Write Lock with writer-preference in Common Lisp
;; https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem
(defstruct rwlock
(rlock (bt:make-lock))
(wlock (bt:make-lock))
(rtrylock (bt:make-lock))
(resource (bt:make-lock))
(rcount 0)
(wcount 0))
@deadtrickster
deadtrickster / springer-free-maths-books.md
Created December 28, 2015 23:43 — forked from bishboria/springer-free-maths-books.md
Springer have made a bunch of maths books available for free, here are the direct links
@deadtrickster
deadtrickster / links
Last active August 22, 2016 18:39
Elixir SuperMacro

Keybase proof

I hereby claim:

  • I am deadtrickster on github.
  • I am deadtrickster (https://keybase.io/deadtrickster) on keybase.
  • I have a public key whose fingerprint is 8838 3471 5B97 B18A 7B12 F958 76A9 29E1 8553 C165

To claim this, I am signing this object:

Isolated test

Before:

7> benchmark:run(100, 1*1000*1000).
{1190.6,100000000}

After:

@deadtrickster
deadtrickster / io-loop.lisp
Last active October 8, 2016 12:29
io-loop.lisp
(let* ((mailbox (sb-concurrency:make-mailbox))
(event-base (make-instance 'iolib:event-base))
(control-fd (eventfd:eventfd.new 0))
(thread (bt:make-thread (lambda ()
(iolib:set-io-handler event-base
control-fd
:read (lambda (fd e ex)
(declare (ignorable fd e ex))
(log:debug (eventfd.read control-fd))))
(loop (iolib:event-dispatch event-base :one-shot t))))))
(defun receive-message (mailbox &key timeout)
"Removes the oldest message from MAILBOX and returns it as the primary
value, and a secondary value of T. If MAILBOX is empty waits until a message
arrives.
If TIMEOUT is provided, and no message arrives within the specified interval,
returns primary and secondary value of NIL."
(tagbody
;; Disable interrupts for keeping semaphore count in sync with
;; #msgs in the mailbox.
(sb-sys:without-interrupts
(with-pinned-objects (queue me)
(setf (waitqueue-token queue) me)
(release-mutex mutex)
;; Now we go to sleep using futex-wait. If anyone else
;; manages to grab MUTEX and call CONDITION-NOTIFY during
;; this comment, it will change the token, and so futex-wait
;; returns immediately instead of sleeping. Ergo, no lost
;; wakeup. We may get spurious wakeups, but that's ok.
(setf status
(case (allow-with-interrupts
/* stop all other threads by sending them SIG_STOP_FOR_GC */
for(p=all_threads; p; p=p->next) {
gc_assert(p->os_thread != 0);
FSHOW_SIGNAL((stderr,"/gc_stop_the_world: thread=%lu, state=%x\n",
p->os_thread, thread_state(p)));
if((p!=th) && ((thread_state(p)==STATE_RUNNING))) {
FSHOW_SIGNAL((stderr,"/gc_stop_the_world: suspending thread %lu\n",
p->os_thread));
/* We already hold all_thread_lock, P can become DEAD but
* cannot exit, ergo it's safe to use pthread_kill. */
(ql:quickload :iolib)
(ql:quickload :eventfd)
(setf control-fd (eventfd:eventfd.new 0))
(setf event-base (make-instance 'iolib:event-base))
(setf thread (bt:make-thread (lambda ()
(iolib:set-io-handler event-base
control-fd
:read (lambda (fd e ex)
(declare (ignorable fd e ex)))