Skip to content

Instantly share code, notes, and snippets.

@hgomersall
hgomersall / tokio_semaphore_test.rs
Created March 29, 2022 11:48
A quick a dirty test on how long a tokio semaphore takes to add and acquire permits (as a pair)
use tokio::sync::Semaphore;
use std::sync::Arc;
use std::time::Instant;
const LOOPS: usize = 1000000;
async fn twiddle(semaphore1: Arc<Semaphore>, semaphore2: Arc<Semaphore>) {
for _ in 0..LOOPS {
semaphore1.add_permits(1);
fn transfer(&mut self, value_in: u8)
-> Result<u8, nb::Error<T::Error>>
{
// We always have to do a complete transfer - writing out and reading
// in. If we don't do the read, then the read buffer overflows and
// we can't use it when we need it.
self.spi.send(value_in)?;
Ok(self.spi.read()?)
}
use std::mem;
use std::sync::Arc;
use futures::task::{Context, Poll};
use tokio::sync::{Mutex, OwnedMutexGuard};
use tower_service::Service;
use tokio_util::sync::ReusableBoxFuture;
pub struct CloneableService<S> {
inner: Arc<Mutex<S>>,
mutex_fut: ReusableBoxFuture<OwnedMutexGuard<S>>,
@hgomersall
hgomersall / empty_aligned.py
Created January 9, 2020 15:48
Simple pure python function to return an empty n-byte-aligned array.
import numpy as np
def empty_aligned(shape, dtype='float64', order='C', n=None):
'''empty_aligned(shape, dtype='float64', order='C', n=None)
Function that returns an empty numpy array that is n-byte aligned,
where ``n`` is determined by inspecting the CPU if it is not
provided.
The alignment is given by the final optional argument, ``n``. If ``n`` is
not set then the default alignment is used.
The rest of the arguments are as per :func:`numpy.empty`.
@hgomersall
hgomersall / packet.h
Last active November 23, 2018 13:33
Simple networking
#ifndef CURIOUS_NETWORKING_H
#define CURIOUS_NETWORKING_H
static const size_t packet_len = 1395;
static const char packet[] = {
0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, 0x2c, 0x20, 0x34, 0x2c, 0x20,
0x35, 0x2c, 0x20, 0x36, 0x2c, 0x20, 0x37, 0x2c, 0x20, 0x38, 0x2c, 0x20,
0x39, 0x2c, 0x20, 0x31, 0x30, 0x2c, 0x20, 0x31, 0x31, 0x2c, 0x20, 0x31,
0x32, 0x2c, 0x20, 0x31, 0x33, 0x2c, 0x20, 0x31, 0x34, 0x2c, 0x20, 0x31,
0x35, 0x2c, 0x20, 0x31, 0x36, 0x2c, 0x20, 0x31, 0x37, 0x2c, 0x20, 0x31,
@hgomersall
hgomersall / rr_arbiter.py
Created May 16, 2018 17:52
A quick and dirty round robin arbiter in myhdl for demonstrating myhdls meta-programming capability. It's not tested, but shows the principles.
from myhdl import *
@block
def grant_selector(request, grant, request_bit):
if request_bit == 0:
@always_comb
def selector():
grant.next = request[request_bit]
from myhdl import *
class Interface():
def __init__(self):
self.data = Signal(False)
class HDLClass1(object):
@block
@hgomersall
hgomersall / problem_with_multiple_converts.py
Last active June 15, 2016 16:28
An example of conversion problems when a signal is used in more than one conversion.
from myhdl import *
@block
def block1(clock, input_signal, output_signal):
@always(clock.posedge)
def driver():
output_signal.next = input_signal
return driver
from myhdl import *
import myhdl
class Interface(object):
def __init__(self):
self.bar = Signal(intbv(0)[4:])
@block
def block1(out_interface, foo):
from myhdl import *
class Interface(object):
def __init__(self):
self.foo = Signal(intbv(0)[20:])
self.bar = Signal(intbv(0)[20:])
@block
def Foo(a, b, c, d, e, clock):