Skip to content

Instantly share code, notes, and snippets.

@msg555
msg555 / less_unfair_scheduling.cpp
Created February 21, 2022 02:44
Analog of https://gist.github.com/msg555/dd491078cf10dbabbe7b1cd142644910 in C++. Still not that fair but doesn't permanently starve threads.
View less_unfair_scheduling.cpp
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
@msg555
msg555 / unfair_queue.py
Created February 21, 2022 02:17
Example of unfair scheduling using queue.Queue in CPython
View unfair_queue.py
from threading import Thread, get_ident
import time
from queue import Queue
THREAD_COUNT = 10
POOL_GET_TIMEOUT = 10.0
POOL_SIZE = 2
pool = Queue()
for i in range(POOL_SIZE):
@msg555
msg555 / unfiar_scheduling.py
Created February 21, 2022 00:59
Example of unfair scheduling in CPython's condition variable implementation
View unfiar_scheduling.py
from threading import Condition, Thread
import time
# This is a sample implementation of something like an object pool. It uses
# a condition variable to ensure that at most `counter` threads are allowed
# to execute at a time.
THREAD_COUNT = 10
counter = 1
cnd = Condition()
@msg555
msg555 / replay.php
Created February 13, 2021 21:13
Replay read/write routines for dustkid
View replay.php
<?php
function _substr($data, $pos, $len) {
if (0 <= $pos && $pos + $len <= strlen($data) && $pos <= $pos + $len) {
return substr($data, $pos, $len);
}
return null;
}
function _unpack_byte($data, $pos) {
$str = _substr($data, $pos, 1);
@msg555
msg555 / subframe_callback.as
Created January 21, 2020 03:44
Sample script using the subframe callback API.
View subframe_callback.as
class script : callback_base {
scene@ g;
array<dustman@> players;
script() {
@g = get_scene();
}
void step(int num_entities) {
int nc = num_cameras();
@msg555
msg555 / 16.cpp
Created December 17, 2019 04:04
16 dumb
View 16.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int mexp(int v, int e, int MOD) {
int ret = 1;
if (e == 0) return ret;
for(int i = 31 - __builtin_clz(e); i >= 0; i--) {
@msg555
msg555 / 10-integral.cpp
Created December 11, 2019 21:52
Float free solution to Advent Day 10
View 10-integral.cpp
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
@msg555
msg555 / intcode.py
Last active December 8, 2019 07:46
Solution to day 7
View intcode.py
import abc
import asyncio
import itertools
import sys
class Instruction(metaclass=abc.ABCMeta):
PARAMS = 0
def __init__(self, comp, offset, mode):
View dusttrackerpro.cpp
class script {
scene@ g;
textfield@ txt;
int init_filth;
int init_filth_block;
int init_enemy;
script() {
@g = get_scene();
@msg555
msg555 / sounddemo.cpp
Created October 20, 2017 18:46
An example of how to embed and use custom sounds with dustscripting.
View sounddemo.cpp
const string EMBED_sound1 = "test1.ogg";
class script {
scene@ g;
script() {
@g = get_scene();
}
void build_sounds(message@ msg) {