npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

needlethreads

v1.1.4

Published

Needlethreads is a light-weight thread implementation, written from scratch in C. It is very easy to use, highly optimized and it does not in any way use Posix Threads. It is based on a combination of an internal heap allocated with mmap(2) plus Inter Pro

Downloads

37

Readme

needlethreads

by @dr-Jonas-Birch

Don't use threads. Use needles.

Needlethreads is a light-weight thread implementation, written from scratch in C. It is very easy to use, highly optimized and it does not in any way use Posix Threads. It is based on a combination of an internal heap allocated with mmap(2) plus Inter Process Communication (IPC) between forks.

Features

  • Local variables
  • Global variables, shared by the threads (the "needles") with automatic mutex protection
  • Multi-CPU core support (by default spawn_multiple() creates as many needles as you have CPU cores)
  • You may also use spawn_single() to use as many needles as you'd like. Remember to call awaitneedles() when you're done so you don't create zombie processes. This is done automatically when using spawn_multiple()

Installation

make
sudo make install

If it succeeds to compile, but fails to install. Make sure that the Makefile contains the right library directory.

API

ninit(int32 size)

Initializes the library and allocates the shared heap. Size is in bytes but you can give kb(20) as an argument to allocate 20 KB. You may also use mb() to the same extent, but for megabytes.

let(variablename)

Declares a variable which is shared between forks. All variables are 64 bit in size (plus 8 additional bits for the mutex locks). You may use all alpha-numeric characters as well as the prime symbol '

You define your variables using let() in the main program before you allocate the needles. Then you use get() and put() (see below) in the needles' program code.

spawn_single(int16 _nid, int16 core, cb callback)

  • NID is a Needle ID which must be unique and non zero.
  • Core is what CPU core to bind the needle to. If you have four cores, this value should be 0, 1, 2 or 3.
  • The callback is the needle's main() function. The signature is void _callback(int16 _nid)
  • ALWAYS call awaitneedles() without arguments when you are done spawning the needles using this single-function.

spawn_multiple(cb callback)

This function spawns four needles if you have four CPU cores. It handles the awaiting automatically.

get(variablename)

Returns the value of the shared variable as an unsigned long integer. If two needles try to access the same variable at the same time the mutex lock kicks in automatically and the second process waits for the first. This works the same for reads and writes.

put(variablename, value)

Changes the value of the shared variable to a value of the form unsigned long integer. The mutex lock applies (see get() above).

nuninit()

Un-initializes the library. Make sure that all needles are dead before you issue this function call.

Example program

#include <needle.h>

extern Memspace Heap;
extern int16 nid;
extern int16 numneedles;

void _callback(int16 _nid) {
    unsigned long int n;

    n = (unsigned long int)get(myvar);
    n++;
    put(myvar, n);

    printf("[%d] Thread end\n", (unsigned int)nid);
    fflush(stdout);

    return;
}

int main(int argc, char *argv[]) {
    int16 i;

    ninit(kb(20));
    let(myvar);

    printf("Spawning needlethreads...\n");
    fflush(stdout);

    for (i=1; i<11; i++)
        spawn_single(i, (i % 4), &_callback);
    awaitneedles();

    printf("\nAll threads terminated. Value of myvar is:\n");
    printf("%d\n", (unsigned int)((unsigned long int)get(myvar)));
    fflush(stdout);

    nuninit();

    return 0;
}

Example program compilation

gcc example.c -o example -lneedle

or:

make example

Contact

Do you have questions or do you wish to maintain this library? Contact me at [email protected]. Please do also check out my popular coding Youtube channel, @dr-Jonas-Birch