CSP inspired data structures and concurrency control for the Swift programming language.

Overview

Solver7CSP

CSP inspired data structures and concurrency control for the Swift programming language.

Communicating Sequential Processes, Wikipedia-CSP, patterns can be found in languages like Go, Erlang, Haskell and Clojure, but are not natively present in the Swift programming language. Swift uses Grand Central Dispatch, Apple GCD, for solving concurrency problems. However, if one would like a different, or perhaps better separation of specialized producers and consumers, CSP may be a way to go. This shifts the focus towards the composition of independently executing processes (writers/readers or producers/consumers) that coordinate via messages transferred between channels. Some examples where CSP may be a better abstraction are stream processing and time series analysis. One often combines multiple streams to perform an operation like perhaps addition:

    let x = stream1.read()
    let y = stream2.read()
    if x.key == y.key {
       z = x + y
       output.write(z)
    }

In CSP, the fundamental concept is a Channel. The Channel is a synchronization point for multiple threads of control, including both writers and readers. The Channel is a rendezvous point for two or more threads wishing to communicate. The simplest set up is:

Writer --> Channel <-- Reader 

where Channel has a single item buffer where a Writer.write blocks after filling the Channel to capacity until a Reader.read clears the Channel. This concept can be extended to allow buffered Channels whereupon the Writer.write blocks when the buffered Channel remains at capacity.

The underlying locking mechanisms should be efficient in the context of multiple Writers and Readers as well

Writer 1                                   Reader 1
Writer 2                                   Reader 2
Writer ...         -->  Channel  <--       Reader ...
Writer N1                                  Reader N2


Writer X {
  while true {
      let msg = buildMsg()
      channel.write(msg)
  }
}
  
Reader Y {
   while true {
       let msg = channel.read()!
       doSomething(msg)
   }
}

When a Channel is Selectable it can be used by a Selector over multiple selectables, channels and timers.

     SelectableChannel 1
     SelectableChannel ...      <-- Selector  
     SelectableChannel N1
     Timer 1 ... Timer N2
     
     
     while true {
        let s = selector.select()
        s.handle()
     }

This implementation provides two Channels and a Timeout along with a FairSelector that support multiple writers and readers.

public class NonSelectableChannel
   
    : Channel
public class SelectableChannel
    
     : NonSelectableChannel
     
      , Selectable
public class Timeout
      
       : Selectable

public protocol Selector 
public class FairSelector: Selector

      
     
    
   

The Channel instances may be created as per the examples or with a ChannelFactory. Extend the ChannelFactory as necessary to support convenient initialization.

The hopefully efficient locking mechanisms that back the implementations are useful by themselves. They utilize the Swift Atomics package to allow wait free locks when conditions / environment permit.

public protocol Lock
open class ReentrantLock: Lock 
public final class FairLock: ReentrantLock 
public final class NonFairLock: ReentrantLock

let lock = NonFairLock(10) 
let sumReady = lock.createCondition()
var sum = 0 

// Thread A 
do {
 lock.lock 
 defer {
  lock.unlock()
 }
 sumReady.doWait()
 assertEquals(77, sum) 
}

// Thread B
do {
  lock.lock()
  defer {
    lock.unlock()
  }
  sum += 77
  sumReady.doNotify()
}

These locks are built on top of lower level, OS level, Mutexes and Conditions in addition to the Swift Atomics.

class Mutex {
 private var mutex: pthread_mutex_t
}

class UpDown {  
  private var mutex: pthread_mutex_t
  private var condition: pthread_cond_t
}  

Similarly, there are Semaphores and Latches that are built upon the underlying Channels, Lock, Mutexes and UpDown.

public class Semaphore
public class CountdownLatch 
You might also like...
GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps.

GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps. It was written from scr

Simple server APIs in Swift

Simple server APIs in Swift

 🪶 Feather is a modern Swift-based content management system powered by Vapor 4.
🪶 Feather is a modern Swift-based content management system powered by Vapor 4.

Feather CMS 🪶 🪶 Feather is a modern Swift-based content management system powered by Vapor 4. 💬 Click to join the chat on Discord. Requirements To

A dockerized microservice written in Swift using Vapor.

price-calculation-service-swift This is an example project for a university project. It uses Vapor to serve a microservice written in Swift. The point

Swift HTTP server using the pre-fork worker model

Curassow Curassow is a Swift Nest HTTP Server. It uses the pre-fork worker model and it's similar to Python's Gunicorn and Ruby's Unicorn. It exposes

💧 A server-side Swift HTTP web framework.
💧 A server-side Swift HTTP web framework.

Vapor is an HTTP web framework for Swift. It provides a beautifully expressive and easy-to-use foundation for your next website, API, or cloud project

Sinatra-like DSL for developing web apps in Swift

Swiftra Swiftra is a library that provides DSLs like Sinatra. System Requirements DEVELOPMENT-SNAPSHOT-2016-02-08-a Example See swiftra-example. impor

libuv base Swift web HTTP server framework

Notice Trevi now open a Trevi Community. Yoseob/Trevi project split up into respective Trevi, lime, middlewares and sys packages at our community. If

A lightweight library for writing HTTP web servers with Swift

Taylor Disclaimer: Not actively working on it anymore. You can check out some alternatives Swift 2.0 required. Working with Xcode 7.1. Disclaimer: It

Owner
null
Tiny http server engine written in Swift programming language.

What is Swifter? Tiny http server engine written in Swift programming language. Branches * stable - lands on CocoaPods and others. Supports the latest

null 3.6k Jan 5, 2023
A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X

IMPORTANT! We don't see any way how to make web development as great as Ruby on Rails or Django with a very static nature of current Swift. We hope th

Saulius Grigaitis 2k Dec 5, 2022
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

Perfect: Server-Side Swift 简体中文 Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

PerfectlySoft Inc. 13.9k Dec 29, 2022
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

Zewo 1.9k Dec 22, 2022
A minimal, fast and unopinionated web framework for Swift

![Fire Image] (http://i.imgur.com/1qR6Nl4.png) Blackfire An extremely fast Swift web framework ?? Getting Started If you're familiar with express.js t

Elliott Minns 908 Dec 2, 2022
HTTP Implementation for Swift on Linux and Mac OS X

Swift HTTP Server Simple HTTP implementation for Swift using POSIX socket API. Running on Mac OS X and Linux. For Mac users: You can install new Swift

Huy 451 Jul 28, 2022
A Swift web framework and HTTP server.

A Swift Web Framework and HTTP Server Summary Kitura is a web framework and web server that is created for web services written in Swift. For more inf

Kitura 7.6k Dec 27, 2022
A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework

Serverside non-blocking IO in Swift Ask questions in our Slack channel! Lightning (formerly Edge) Node Lightning is an HTTP Server and TCP Client/Serv

SkyLab 316 Oct 6, 2022
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation <h5 align="right"><a href="http://demo.swiftexpress.io/">Live ?? server running Demo <img src="https://cdn0.iconfinder.com/data/icons/

Crossroad Labs 850 Dec 2, 2022
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Alsey Coleman Miller 258 Oct 6, 2022