{"id":13489248,"url":"https://github.com/tidwall/Safe","last_synced_at":"2025-03-28T04:30:58.965Z","repository":{"id":34405174,"uuid":"38333842","full_name":"tidwall/Safe","owner":"tidwall","description":"Modern Concurrency and Synchronization for Swift.","archived":true,"fork":false,"pushed_at":"2019-06-18T23:55:52.000Z","size":555,"stargazers_count":417,"open_issues_count":0,"forks_count":26,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-03-14T18:34:57.162Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tidwall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-30T21:23:33.000Z","updated_at":"2024-02-13T01:00:03.000Z","dependencies_parsed_at":"2022-08-17T21:20:06.360Z","dependency_job_id":null,"html_url":"https://github.com/tidwall/Safe","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2FSafe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2FSafe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2FSafe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2FSafe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidwall","download_url":"https://codeload.github.com/tidwall/Safe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245970381,"owners_count":20702398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T19:00:21.189Z","updated_at":"2025-03-28T04:30:58.624Z","avatar_url":"https://github.com/tidwall.png","language":"Swift","funding_links":[],"categories":["Libs","Concurrency","Swift"],"sub_categories":["Events"],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"http://tidwall.github.io/Safe/head.png\" width=\"424\" height=\"155\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://tidwall.github.io/Safe/test-results.txt\"\u003e\u003cimg src=\"https://tidwall.github.io/Safe/build.png\" alt=\"\" width=\"93\" height=\"20\" border=\"0\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://tidwall.github.io/Safe/docs/\"\u003e\u003cimg src=\"https://tidwall.github.io/Safe/docs.png?check\" alt=\"\" width=\"65\" height=\"20\" border=\"0\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://developer.apple.com/swift/\"\u003e\u003cimg src=\"https://img.shields.io/badge/Swift-2.2-orange.svg?style=flat-square\" alt=\"Swift 2.2\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://swift.org/\"\u003e\u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20iOS%20%7C%20Linux%20-lightgray.svg?style=flat-square\" alt=\"Platforms OS X | iOS | Linux\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n##Features\n\n- Simple `Atomic\u003cT\u003e` class for numbers and strings.\n- Uncomplicated `dispatch` keyword for firing off background routines.\n- Awesome `Chan\u003cT\u003e` for concurrent communication.\n- Useful sync APIs. `Mutex`, `Cond`, `Once`, `WaitGroup`\n\n\n## Atomic\n\n#### Types\n`Int`, `Int8`, `Int16`, `Int32`, `Int64`, `UInt`, `UInt8`, `UInt16`, `UInt32`, `UInt64`, `Float`, `Double`, `Bool`, `String`\n\n#### Operators\n`==`, `!=`, `\u0026\u0026`, `||`, `\u003c=`, `\u003e=`, `\u003e`, `\u003c`, `!`  \n`+`, `-`, `*`, `/`, `%`, `\u003c\u003c`, `\u003e\u003e`, `^`, `\u0026`, `\u0026+`, `\u0026-`, `\u0026*`, `++`, `--`, `+=`, `-=`, `*=`, `/=`, `%=`, `+=`, `\u003c\u003c=`, `\u003e\u003e=`, `^=`, `\u0026=`\n\n\n```swift\nvar anum = IntA(100)      // IntA is an alias for Atomic\u003cInt\u003e.\nanum += 15                // Adds a value atomically.\nlet res = anum % 4        // Modulo operation atomically.\nprint(\"\\(anum) \\(res)\")   // prints '115 3'.\n```\n\n## Dispatch\n\nSafe adds an uncomplicated method for dispatching routines. \n\n```swift\ndispatch {\n    print(\"Background\")\n}\nprint(\"Foreground\")\n```\n\n## Channels\n\nA new `Chan\u003cT\u003e` class provides a clean and simple model for concurrently sharing objects. `Chan\u003cT\u003e` is modeled after [Go channels](https://golang.org/doc/effective_go.html#channels).\n\n[Sharing Memory by Communicating](http://blog.golang.org/share-memory-by-communicating)\n\n#### Example\n```swift\nlet jobs = Chan\u003cInt\u003e(5)  // buffered channel\nlet done = Chan\u003cBool\u003e()  // unbuffered channel\n\ndispatch {\n    for ;; {\n        if let j = \u003c-jobs {\n            print(\"received job \\(j)\")\n        } else {\n            print(\"received all jobs\")\n            done \u003c- true\n            return\n        }\n    }\n}\n\nfor var j = 1; j \u003c= 3; j++ {\n    jobs \u003c- j\n    print(\"sent job \\(j)\")\n}\njobs.close()\nprint(\"sent all jobs\")\n\n\u003c-done\n```\n\n#### Iterate\n\nA channel can also be iterated through.\n\n```swift\nwhile let j = \u003c-jobs {\n    print(\"received job \\(j)\")\n}\nprint(\"received all jobs\")\n```\n\n#### Select\n\nThe `_select` keyword is a multiway communications multiplexer that works on multiple channels.  `_select`, `_case`, and `_default` start with underscores so that they do not conflict with the `select`, `case`, and `default` syscall and keywords. *When a `_select` encounters multiple channels with data, the chosen `_case` is selected at random*\n\n```swift\nlet jobs1 = Chan\u003cInt\u003e()\nlet jobs2 = Chan\u003cInt\u003e()\n\ndispatch {\n    for ;; {\n        _select {\n            _case(jobs1){ j in\n                print(\"received 1: \\(j)\")\n            }\n            _case(jobs2){ j in\n                print(\"received 2: \\(j)\")\n            }\n        }\n    }\n}\n\nfor var j = 1; ; j++ {\n    jobs1 \u003c- (j * 1000)\n    jobs2 \u003c- (j * 2000)\n    NSThread.sleepForTimeInterval(1)\n}\n```\n\n#### Select with Default\n\nA `_select` can contain a single `_default` for non-blocking operations. \n\n```swift\n_select {\n    _case(jobs1){ j in\n        print(\"received 1: \\(j)\")\n    }\n    _case(jobs2){ j in\n        print(\"received 2: \\(j)\")\n    }\n    _default {\n        print(\"channels not ready\")\n    }\n}\n```\n\n## Mutex, Cond, Once, WaitGroup\n\nIncredibly useful sync APIs.\n\n#### Mutex\n\n```swift\nlet m = Mutex()\nm.lock()           \nm.unlock()         \nm.lock {\n    // this block is locked\n}\n```\n#### Cond\n```swift\nlet c = Cond(Mutex())\nc.wait()                // wait for signal.\nc.wait(0.25)            // wait for signal or 250ms to pass.\nc.signal()              // signal to one wait.\nc.broadcast()           // signal to all waits.\n```\n#### Once\n```swift\nfunc f(){\n    print(\"hey there\")\n}\n\nlet o = Once()\no.doit(f)               // runs once\no.doit(f)               // noop: cannot run twice\n```\n#### WaitGroup\n```swift\nlet dosomething : (NSTimeInterval, WaitGroup)-\u003e() = { (delay, wg) in\n    NSThread.sleepForTimeInterval(delay)\n    print(\"Function in background, duration: \\(delay)\")\n    wg.done()\n}\nlet wg = WaitGroup()\nwg.add(1)\ndispatch { dosomething(0.40, wg) }\nwg.add(1)\ndispatch { dosomething(0.30, wg) }\nwg.add(1)\ndispatch { dosomething(0.15, wg) }\nwg.add(1)\ndispatch { dosomething(0.60, wg) }\nwg.wait()\nprint(\"done\")\n```\n\n##Installation (iOS and OS X)\n\n### [Carthage]\n\n[Carthage]: https://github.com/Carthage/Carthage\n\nAdd the following to your Cartfile:\n\n```\ngithub \"tidwall/Safe\"\n```\n\nThen run `carthage update`.\n\nFollow the current instructions in [Carthage's README][carthage-installation]\nfor up to date installation instructions.\n\n[carthage-installation]: https://github.com/Carthage/Carthage#adding-frameworks-to-an-application\n\nThe `import Safe` directive is required in order to access Safe features.\n\n### [CocoaPods]\n\n[CocoaPods]: http://cocoapods.org\n\nAdd the following to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html):\n\n```ruby\nuse_frameworks!\npod 'Safe'\n```\n\nThen run `pod install` with CocoaPods 0.36 or newer.\n\nThe `import Safe` directive is required in order to access Safe features.\n\n### [SPM]\n\n[SPM]: https://swift.org/package-manager/\n\nAdd the following to your `Package.swift`\n\n```swift\n    dependencies: [\n        ...\n        .Package(url: \"https://github.com/tidwall/Safe\", majorVersion: 1, minor: 2)\n    ]\n```\n\nThen run `swift build`.\n\nThe `import Safe` directive is required in order to access Safe features.\n\n### Manually\n\nCopy the `Source/*.swift` file into your project.  \n\nThere is no need for `import Safe` when manually installing.\n\n## Contact\nJosh Baker [@tidwall](http://twitter.com/tidwall)\n\n## License\n\nThe Safe source code is available under the MIT License.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidwall%2FSafe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidwall%2FSafe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidwall%2FSafe/lists"}