Skip to content

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

License

Notifications You must be signed in to change notification settings

space-code/atomic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

47 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

atomic

License Swift Compatibility Platform Compatibility CI

Description

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

Features

πŸ”’ Thread-Safe - Automatic synchronization for concurrent access
⚑ Simple API - Just add @Atomic to any property
🎯 Type-Safe - Works with any Swift type
πŸ“± Cross-Platform - Works on iOS, macOS, tvOS, watchOS, and visionOS
⚑ Lightweight - Minimal footprint with zero dependencies
πŸ§ͺ Well Tested - Comprehensive test coverage

Table of Contents

Requirements

Platform Minimum Version
iOS 13.0+
macOS 10.15+
tvOS 13.0+
watchOS 6.0+
visionOS 1.0+
Xcode 15.3+
Swift 5.10+

Installation

Swift Package Manager

Add the following dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/space-code/atomic.git", from: "1.1.1")
]

Or add it through Xcode:

  1. File > Add Package Dependencies
  2. Enter package URL: https://github.com/space-code/atomic.git
  3. Select version requirements

Quick Start

import Atomic

@Atomic var counter = 0

// Thread-safe increment from multiple threads
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
    counter += 1
}

print("Final count: \(counter)") // Always 1000

Usage

Basic Usage

Simply add the @Atomic property wrapper to any property that needs thread-safe access:

import Atomic

class UserSession {
    @Atomic var user: User?
    @Atomic var loginAttempts = 0
    
    func login(username: String, password: String) async throws {
        // Thread-safe write
        _loginAttempts.write { $0 += 1 }
        
        // Perform authentication
        let authenticatedUser = try await authenticate(username, password)
        
        // Thread-safe write with new value
        _user.write(authenticatedUser)
    }
    
    func getCurrentUsername() -> String? {
        // Thread-safe read
        _user.read { $0?.username }
    }
}

Thread-Safe Collections

import Atomic

class DataCache {
    @Atomic private var cache: [String: Data] = [:]
    
    func store(_ data: Data, forKey key: String) {
        _cache.write { cache in
            cache[key] = data
        }
    }
    
    func retrieve(forKey key: String) -> Data? {
        _cache.read { $0[key] }
    }
    
    func removeExpired(before date: Date) {
        _cache.write { cache in
            cache = cache.filter { $0.value.timestamp > date }
        }
    }
}

Communication

Contributing

We love contributions! Please feel free to help out with this project. If you see something that could be made better or want a new feature, open up an issue or send a Pull Request.

Development Setup

Bootstrap the development environment:

mise install

Author

Nikita Vasilev

License

Atomic is released under the MIT license. See LICENSE for details.


⬆ back to top

Made with ❀️ by space-code

About

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •