Skip to content

Instantly share code, notes, and snippets.

View osa1's full-sized avatar
🌴
On vacation

Ömer Sinan Ağacan osa1

🌴
On vacation
View GitHub Profile
// clang -Wall main.c -o main -DNONBLOCK
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
/// Set `stdin` to non-blocking mode.
pub fn set_stdin_nonblocking() {
use nix::fcntl::{fcntl, FcntlArg, OFlag};
let mut stdin_flags: OFlag = match fcntl(libc::STDIN_FILENO, FcntlArg::F_GETFL) {
Err(err) => {
panic!("Unable to read stdin flags: {:?}", err);
}
Ok(flags) => match OFlag::from_bits(flags) {
@osa1
osa1 / ssa.rs
Last active June 1, 2020 07:36
#![allow(dead_code)]
// This module implements the SSA construction algorithm described in
//
// Simple and Efficient Construction of Static Single Assignment Form
// (https://pp.info.uni-karlsruhe.de/uploads/publikationen/braun13cc.pdf)
use cranelift_entity::{entity_impl, PrimaryMap, SecondaryMap};
use std::collections::HashMap;
" Make CTRL-T work correctly with goto-definition.
setlocal tagfunc=CocTagFunc
nmap <leader>gre <Plug>(coc-references)
nmap <leader>grn <Plug>(coc-rename)
" nmap <leader>ge <Plug>(coc-diagnostic-info)
nmap <leader>ge :<C-u>CocList diagnostics<CR>
nmap <leader>gp <Plug>(coc-diagnostic-prev)
nmap <leader>gn <Plug>(coc-diagnostic-next)
@osa1
osa1 / gc.c
Created January 19, 2020 13:58
// $ gcc -fPIC -shared -o libgc.so gc.c
// $ LD_PRELOAD=$(pwd)/libgc.so ghci
#include <stdlib.h>
void GarbageCollect() {
exit(1);
}
void hs_init() {

Spawning async tasks in GTK apps

I'm trying to spawn async tasks in a GTK app using GLib's [MainContext::spawn][1]. Questions:

  • I'm not sure how to get MainContext of a GTK [Application][2]. I think Application should already have a MainContext, and I'm not sure if creating a new one is safe.

  • In a task spawned by MainContext::spawn I'm not sure which async I/O function/traits I can use. I think I can use AsyncRead/AsyncWrite from futures but that's not documented.

  • In a task I'm not sure how to spawn more tasks. Should I pass a copy of MainContext to my tasks and use it to spawn more tasks via the spawn method?

# Sample TOML configuration file for building Rust.
#
# To configure rustbuild, copy this file to the directory from which you will be
# running the build, and name it config.toml.
#
# All options are commented out by default in this file, and they're commented
# out with their default values. The build system by default looks for
# `config.toml` in the current directory of a build for build configuration, but
# a custom configuration file can also be specified with `--config` to the build
# system.
[package]
name = "blocking_test"
version = "0.1.0"
authors = ["Ömer Sinan Ağacan <omeragacan@gmail.com>"]
edition = "2018"
[dependencies]
futures-preview = { version = "0.3.0-alpha.18", features = ["async-await", "nightly"] }
futures-util-preview = "0.3.0-alpha.18"
tokio = { git = "https://github.com/tokio-rs/tokio.git", features = ["timer"] }
@osa1
osa1 / Cargo.toml
Last active September 21, 2019 16:56
[package]
name = "delay_test"
version = "0.1.0"
authors = ["Ömer Sinan Ağacan <omeragacan@gmail.com>"]
edition = "2018"
[dependencies]
tokio = { git = "https://github.com/tokio-rs/tokio.git", features = ["timer"] }
futures-preview = { version = "0.3.0-alpha.18", features = ["async-await", "nightly"] }
futures-util-preview = "0.3.0-alpha.18"
@osa1
osa1 / main.rs
Last active January 4, 2024 11:21
listbox-dnd
// A drag-and-drop example. You can drag the list items using the icon on the left, and drop them
// to other icons to move the list item. Originally listbox-dnd.c in GTK+ repository. Ported to
// gtk-rs by Ömer Sinan Ağacan <omeragacan@gmail.com>.
extern crate cairo;
extern crate gdk;
extern crate gtk;
use gdk::{Atom, DragAction, DragContext, ModifierType, Screen};
use gtk::*;