{"id":30670563,"url":"https://github.com/slavamuravey/atomics-sync","last_synced_at":"2025-09-01T02:12:03.615Z","repository":{"id":305206845,"uuid":"1022220685","full_name":"slavamuravey/atomics-sync","owner":"slavamuravey","description":"Advanced synchronization primitives for JavaScript 🚦","archived":false,"fork":false,"pushed_at":"2025-07-20T12:02:27.000Z","size":326,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-09T01:47:29.809Z","etag":null,"topics":["concurrency","multithreading","mutex","semaphore","shared-memory","synchronization","webworker","worker-threads"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/slavamuravey.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-18T16:56:35.000Z","updated_at":"2025-07-22T06:15:03.000Z","dependencies_parsed_at":"2025-07-18T23:13:37.772Z","dependency_job_id":null,"html_url":"https://github.com/slavamuravey/atomics-sync","commit_stats":null,"previous_names":["slavamuravey/atomics-sync"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/slavamuravey/atomics-sync","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavamuravey%2Fatomics-sync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavamuravey%2Fatomics-sync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavamuravey%2Fatomics-sync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavamuravey%2Fatomics-sync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slavamuravey","download_url":"https://codeload.github.com/slavamuravey/atomics-sync/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavamuravey%2Fatomics-sync/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273064986,"owners_count":25039267,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["concurrency","multithreading","mutex","semaphore","shared-memory","synchronization","webworker","worker-threads"],"created_at":"2025-09-01T02:12:02.961Z","updated_at":"2025-09-01T02:12:03.614Z","avatar_url":"https://github.com/slavamuravey.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Atomics Sync\n\nAtomics Sync is lightweight library providing thread-safe synchronization primitives for JavaScript environments \nwith shared memory support (Web Workers, Node.js worker_threads). \nImplements essential concurrency control mechanisms using SharedArrayBuffer and Atomics API.\n\n### Features\n\n- [Mutex](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/Mutex.md) - Mutual exclusion lock for critical sections\n- [SpinLock](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/SpinLock.md) - Low-level busy-wait lock for very short operations\n- [Semaphore](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/Semaphore.md) - Counting semaphore for resource management\n- [Condition](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/Condition.md) - Condition variables for thread signaling\n- [Barrier](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/Barrier.md) - Synchronization point for multiple threads\n- [Once](https://github.com/slavamuravey/atomics-sync/blob/main/docs/classes/Once.md) - One-time initialization primitive\n\n**Important**: For browsers, your server must send these headers:\n```\nCross-Origin-Opener-Policy: same-origin\nCross-Origin-Embedder-Policy: require-corp\n```\n\n### Installation\n\n```shell\nnpm install atomics-sync\n```\n\n### Why This Library?\n\nModern JavaScript applications increasingly use:\n- Web Workers for parallel processing\n- SharedArrayBuffer for shared memory\n- CPU-intensive tasks (WASM, WebGL, etc.)\n\nThese primitives help coordinate work between threads while preventing:\n- Race conditions\n- Data corruption\n- Deadlocks\n\n### Usage Examples\n\n**Important**: There is no reliable way for a thread to know its own ID automatically in JavaScript environments.\nThe parent/main thread must explicitly assign and pass a unique thread ID to each worker thread it creates.\n\n#### Mutex\n\nInit `mutex` to work safely with `shared` data:\n```javascript\nconst shared = new Int32Array(\n  new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT)\n);\nconst mutex = Mutex.init();\n```\n\nPass `mutex` and `shared` to threads. Remember about `threadId`:\n```javascript\nconst worker = new Worker(\"./worker.js\", {\n  workerData: { threadId, shared, mutex }\n});\n```\n\nWithin thread use lock/unlock methods to wrap critical section:\n```javascript\ntry {\n  Mutex.lock(mutex, threadId);\n  // work with shared data here\n} finally {\n  Mutex.unlock(mutex, threadId);\n}\n```\n\nSee [full example](https://github.com/slavamuravey/atomics-sync/blob/main/example/mutex.mjs).\n\n#### Semaphore\n\nHere's a practical example demonstrating how to use a semaphore to make one thread \nwait for another thread to complete certain actions:\n\nInit semaphore:\n```javascript\nconst sem = Semaphore.init(0);\n```\n\nOne thread creates another thread and must wait some initialization actions within it:\n\n```javascript\nnew Worker(\"./worker.js\", { workerData: { sem } });\nSemaphore.wait(sem);\n// continue execution\n// ...\n```\n\nCreated thread performs necessary operations and notify parent thread:\n\n```javascript\n// ...\ninitSomeImportantThings();\nSemaphore.post(sem);\n```\n\nSee [full example](https://github.com/slavamuravey/atomics-sync/blob/main/example/semaphore.mjs).\n\n#### Condition\n\nUsing a condition variable, we can make one thread wait for a change in a shared variable (protected by a mutex) before proceeding with its operation.\n\nInit condition variable and mutex, allocate shared variable:\n```javascript\nconst cond = Condition.init();\nconst mtx = Mutex.init();\nconst shared = new Int32Array(\n  new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT)\n);\nshared[0] = -1;\n```\n\nOne thread produces value:\n```javascript\nMutex.lock(mtx, threadId);\nshared[0] = Math.floor(Math.random() * 10);\nCondition.signal(cond);\nMutex.unlock(mtx, threadId);\n```\n\nAnother thread consumes the value and makes some work with it:\n```javascript\nMutex.lock(mtx, threadId);\n\nwhile (shared[0] \u003c 0) {\n  Condition.wait(cond, mtx, threadId);\n}\n\nshared[0] *= 10;\nMutex.unlock(mtx, threadId);\n```\n\nSee [full example](https://github.com/slavamuravey/atomics-sync/blob/main/example/condition.mjs).\n\n#### SpinLock\n\nA spinlock provides an interface nearly identical to a mutex (lock()/unlock()), \nbut is optimized for very short wait times where spinning (busy-waiting) is more efficient than thread suspension.\n\n#### Barrier\n\nA barrier synchronizes multiple threads at a specific execution point.\n\nIn this example, we launch 10 threads that execute at variable speeds and create a barrier with a count of 5.\n\n```javascript\n// main.js\n\nconst barrier = Barrier.init(5);\n\nfor (let i = 0; i \u003c 10; i++) {\n  const threadId = i + 1;\n  const worker = new Worker(\"./worker.js\", {\n    workerData: { threadId, barrier }\n  });\n}\n\n// worker.js\n\nsetTimeout(() =\u003e {\n  // ...\n  Barrier.wait(barrier, threadId);\n  // ...\n}, threadId * 100);\n```\n\nThe first 5 threads to reach the barrier will block and wait.\n\nOnce the 5th thread arrives, the barrier releases all waiting threads.\n\nThe remaining 5 threads then proceed through the barrier in the same way.\n\nSee [full example](https://github.com/slavamuravey/atomics-sync/blob/main/example/barrier.mjs).\n\n#### Once\n\nA Once primitive ensures one-time initialization in concurrent environments.\n\nInit once value:\n\n```javascript\nconst once = Once.init();\n```\n\nPass it into some threads:\n\n```javascript\nconst worker = new Worker(\"./worker.js\", {\n  workerData: { once }\n});\n```\n\nWithin thread:\n\n```javascript\nOnce.execute(once, () =\u003e {\n  // some logic that should be executed only once\n});\n```\n\nSee [full example](https://github.com/slavamuravey/atomics-sync/blob/main/example/once.mjs).\n\n### Documentation\n\nFor complete API reference, see\n[API documentation](https://github.com/slavamuravey/atomics-sync/blob/main/docs/README.md).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslavamuravey%2Fatomics-sync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslavamuravey%2Fatomics-sync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslavamuravey%2Fatomics-sync/lists"}